Android autocompletetextview setOnItemClickListener时如何获取原始项目json数组id
我在Android活动中有一个自动完成文本视图,该视图从json数组的arrayadapter获取数据。当有人在“自动完成”文本视图中选择一个选项时,我希望获得原始JSON数组id。我正在获取所选字符串,但我想要的是实际ID,而不是所选的pos ID 这是我的密码:Android autocompletetextview setOnItemClickListener时如何获取原始项目json数组id,android,arrays,json,android-arrayadapter,autocompletetextview,Android,Arrays,Json,Android Arrayadapter,Autocompletetextview,我在Android活动中有一个自动完成文本视图,该视图从json数组的arrayadapter获取数据。当有人在“自动完成”文本视图中选择一个选项时,我希望获得原始JSON数组id。我正在获取所选字符串,但我想要的是实际ID,而不是所选的pos ID 这是我的密码: private void LoadSearchData() { RequestQueue requestQueue=Volley.newRequestQueue(getApplicationContext());
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);
}
private void LoadSearchData(){
RequestQueue RequestQueue=Volley.newRequestQueue(getApplicationContext());
StringRequest StringRequest=新建StringRequest(Request.Method.GET,url\u class.Search\u Dist,new Response.Listener()){
@凌驾
公共void onResponse(字符串响应){
试一试{
JSONObject JSONObject=新JSONObject(响应);
JSONArray JSONArray=jsonObject.getJSONArray(“位置信息”);
data_list=新字符串[jsonArray.length()];
for(int i=0;i
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);
}
});
autoCompleteTextView.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
公共无效onItemClick(AdapterView AdapterView、View视图、int i、long l){
字符串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(地名);
textViewDist.setText(地名);
}
});
浏览此链接
首先,不建议这样做。但如果你想,那就试试吧
if(data_list.length > i) {
String id = data_list[i].split("-")[1];
}
建议:您应该使用自定义适配器
和自定义对象
来实现这一点
自定义模型:
自定义适配器:
class StoreAdapter扩展了ArrayAdapter{
}
创建一个与json响应中的数据匹配的类
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();
}
}
private final List stores=new ArrayList();
...
公共void onResponse(字符串响应){
试一试{
JSONObject JSONObject=新JSONObject(响应);
JSONArray JSONArray=jsonObject.getJSONArray(“位置信息”);
for(int i=0;i
您还需要为微调器创建自定义适配器类。有很多自定义适配器的教程。创建一个表示json响应中项目的类,将POJO列表传递给适配器。@ayanbhattacharjee,您检查过了吗?
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();
}
}