簡體   English   中英

autocompletetextview setOnItemClickListener 時如何獲取原始項目json數組id

[英]How to get the original item json array id when autocompletetextview setOnItemClickListener

我在 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);
        }
    });

首先,這是不推薦的。 但如果你想試試

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

建議:您應該使用帶有自定義object的自定義adapter來實現此object

定制型號:

class Store {
    String name;
    String id;

    // getter - setter
}

自定義適配器:

class StoreAdapter extends ArrayAdapter<Store> {

}

創建一個與 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;
    } 
}

在您的響應方法中構建 Store 的列表:

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 創建自定義適配器類。 有很多關於自定義適配器的教程。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM