autocompletetextview setOnItemClickListener时如何获取原始项json数组ID

时间:2019-12-10 09:50:28

标签: android arrays json android-arrayadapter autocompletetextview

我在Android活动中有一个自动完成文本视图,该视图从json数组中的arrayadapter获取数据。当有人在自动完成文本视图中选择一个选项时,我想获取原始的JSON数组ID。我正在获取所选的字符串,但我希望实际的ID而不是所选的pos ID 这是我的代码:

private void LoadSearchData() {

    RequestQueue requestQueue=Volley.newRequestQueue(getApplicationContext());
    StringRequest stringRequest=new StringRequest(Request.Method.GET, url_class.Search_Dist, new Response.Listener<String>() {

        @Override

        public void onResponse(String response) {

            try {

                JSONObject jsonObject=new JSONObject(response);
                JSONArray jsonArray=jsonObject.getJSONArray("place_info");
                data_list=new String[jsonArray.length()];
                for (int i=0; i < jsonArray.length(); i++) {
                    JSONObject jsonObject1=jsonArray.getJSONObject(i);
                    String dis_name=jsonObject1.getString("store_name" );
                    String dis_id=jsonObject1.getString("store_id");
                       Toast.makeText(getApplicationContext(),"District name="+dis_name,Toast.LENGTH_SHORT).show();
                       district_name.add(dis_name);
                     ///  district_whole.add(dis_name+"-"+dis_id);
                       data_list[i]=dis_name+"-"+dis_id;
                }
                //spinner_search.setAdapter(new ArrayAdapter<String>(Dashboard.this, android.R.layout.simple_spinner_dropdown_item, district_name));

                autoCompleteTextView.setAdapter(new ArrayAdapter<String>(Dashboard.this, android.R.layout.simple_spinner_dropdown_item, district_name));


            } catch (JSONException e) {
                e.printStackTrace();
            }

        }

    }, new Response.ErrorListener() {

        @Override

        public void onErrorResponse(VolleyError error) {
            MDToast mdToast=MDToast.makeText(getApplicationContext(), "Something went wrong!!", Toast.LENGTH_SHORT, MDToast.TYPE_WARNING);
            mdToast.show();
            error.printStackTrace();

        }

    });

    int socketTimeout=30000;

    RetryPolicy policy=new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);

    stringRequest.setRetryPolicy(policy);

    requestQueue.add(stringRequest);


}

OnSelectItemlistner:

autoCompleteTextView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            String place_name=autoCompleteTextView.getText().toString();
            int txt_indx =i;

            //Toast.makeText(getApplicationContext(),"ID="+i,Toast.LENGTH_SHORT).show();
           // Toast.makeText(getApplicationContext(),"ID="+i,Toast.LENGTH_SHORT).show();
            GetstockInfo(place_name);
            textViewDist.setText(place_name);
        }
    });

3 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

首先,不建议这样做。但是,如果您想尝试

if(data_list.length > i) {
    String id = data_list[i].split("-")[1];
}

建议:您应该将自定义adapter与自定义object配合使用

自定义模型:

class Store {
    String name;
    String id;

    // getter - setter
}

自定义适配器:

class StoreAdapter extends ArrayAdapter<Store> {

}

答案 2 :(得分:0)

创建一个与您的json响应中的数据匹配的POJO

public class Store {
    private final String storeId;
    private final String storeName;

    public Store(String storeId, String storeName) {
        this.storeId = storeId;
        this.storeName = storeName;
    }

    public String getStoreId() {
        return storeId;
    }

    public String getStoreName() {
        return storeName;
    } 
}

在您的响应方法中构建商店列表:

private final List<Store> stores = new ArrayList<>();

...

public void onResponse(String response) {

        try {

            JSONObject jsonObject=new JSONObject(response);
            JSONArray jsonArray=jsonObject.getJSONArray("place_info");
            for (int i=0; i < jsonArray.length(); i++) {
                JSONObject jsonObject1=jsonArray.getJSONObject(i);
                final String dis_name=jsonObject1.getString("store_name" );
                final String dis_id=jsonObject1.getString("store_id");

                stores.add(new Store(dis_id, dis_name));
            }

            // Create your custom adapter here and pass it "List<Store> stores"
            autoCompleteTextView.setAdapter(stores);

        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

您还需要为Spinner创建一个自定义适配器类。有很多关于自定义适配器的教程。