我怎样才能被选中" itemsid"来自Android中的微调器?

时间:2017-02-13 07:32:57

标签: android android-spinner android-adapter

当我从微调器中选择任何项目时,我得到位置,项目名称。当我点击任何项目时,我需要这个ItemId。我该怎么办?

我用过改装。我从webservice获得了一个数据列表。

List<Zone> zonelst=response.body().getZoneslist();

zonelst包含zid和zname。

我已将&#34; items&#34;(zname)加载到微调器中。当我点击任何项目(zname)时,我需要itemsid(zid)。

for(int i=0; i<zonelst.size(); i++){
//Storing names to string array
items[i] = zonelst.get(i).getZname();
itemsid[i]=zonelst.get(i).getZid();
}

MODEL:

    public class Zone {
    @SerializedName("ZID")
    private String zid;
    @SerializedName("ZName")
    private String zname;

    public Zone(String zid, String zname) {
        this.zid = zid;
        this.zname = zname;
    }

    public String getZid() {
        return zid;
    }

    public void setZid(String zid) {
        this.zid = zid;
    }

    public String getZname() {
        return zname;
    }

    public void setZname(String zname) {
        this.zname = zname;
    }
}

活动:

ZoneApiInterface apiService = OperatingApiClient.getClient()。create(ZoneApiInterface.class);

Call<ZoneApiResponse> call = apiService.getZoneInfoList();
call.enqueue(new Callback<ZoneApiResponse>() {
    @Override
    public void onResponse(Call<ZoneApiResponse> call, Response<ZoneApiResponse> response) {
        List<Zone> zonelst=response.body().getZoneslist();
        Log.d(TAG,"Number of Zone received1: "+zonelst.size());

        //*********DROPDOWN******************************************************

        //String array to store all the zone names
        String[] items = new String[zonelst.size()];
        String[] itemsid = new String[zonelst.size()];

        //Traversing through the whole list to get all the names
        for(int i=0; i<zonelst.size(); i++){
            //Storing names to string array
            items[i] = zonelst.get(i).getZname();
            itemsid[i]=zonelst.get(i).getZid();
        }


        final Spinner spnrZone=(Spinner)findViewById(R.id.spinnerZone);
        ArrayAdapter<String> adapter;
        adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.support_simple_spinner_dropdown_item, items); //getApplication()
        //setting adapter to spinner
        spnrZone.setAdapter(adapter);
        //Creating an array adapter for list view


        spnrZone.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                int Hold = spnrZone.getSelectedItemPosition()+1;
                Toast.makeText(getApplicationContext(), "Item Position is = "+Hold, Toast.LENGTH_SHORT).show();

                String N=parent.getItemAtPosition(position).toString();
                Toast.makeText(getApplicationContext(), "Item Name = "+N, Toast.LENGTH_SHORT).show();


            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    @Override
    public void onFailure(Call<ZoneApiResponse> call, Throwable t) {
        // Log error here since request failed
        Log.e(TAG,t.toString());

    }
});

2 个答案:

答案 0 :(得分:2)

如果您想使用原始代码,我建议this answer

但这不是优化的代码段。

获得响应后,将所有znames和zids复制到数组,这需要额外的空间和时间(For-loop)。

我建议采用另一种方法。

覆盖toString模型中的Zone方法。将其添加到您的Zone模型中:

@Override
public String toString() {
    return getZname();
}

在你的ACTIVITY:

Call<ZoneApiResponse> call = apiService.getZoneInfoList();
call.enqueue(new Callback<ZoneApiResponse>() {
@Override
public void onResponse(Call<ZoneApiResponse> call, Response<ZoneApiResponse> response) {
    List<Zone> zonelst=response.body().getZoneslist();
    Log.d(TAG,"Number of Zone received1: "+zonelst.size());

    //*********DROPDOWN******************************************************

    final Spinner spnrZone=(Spinner)findViewById(R.id.spinnerZone);
    ArrayAdapter<Zone> adapter;
    adapter = new ArrayAdapter<Zone>(getApplicationContext(), R.layout.simple_selectable_list_item,zonelst); //getApplication()
    //setting adapter to spinner
    spnrZone.setAdapter(adapter);
    //Creating an array adapter for list view


    spnrZone.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
               Zone z = (Zone) parent.getItemAtPosition(position);


            int Hold = spnrZone.getSelectedItemPosition()+1;
            Toast.makeText(getApplicationContext(), "Item Position is = "+Hold, Toast.LENGTH_SHORT).show();

            String N = z.getZname();
            String zid = z.getZid();

            Toast.makeText(getApplicationContext(), "Item Name = "+N+",Item ID = "+zid, Toast.LENGTH_SHORT).show();


        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });
}

@Override
public void onFailure(Call<ZoneApiResponse> call, Throwable t) {
    // Log error here since request failed
    Log.e(TAG,t.toString());

}
});

答案 1 :(得分:1)

spnrZone.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                int Hold = spnrZone.getSelectedItemPosition()+1;
                Toast.makeText(getApplicationContext(), "Item Position is = "+Hold, Toast.LENGTH_SHORT).show();

                String N=parent.getItemAtPosition(position).toString();
                Toast.makeText(getApplicationContext(), "Item Name = "+N, Toast.LENGTH_SHORT).show();

                String id=itemsid[position];  //simply get its id (String or int use whatev er its type)
            }

使用itemsid[position];