我在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);
}
});
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创建一个自定义适配器类。有很多有关自定义适配器的教程。