我做了一个活动,获取jsonarray并将其放入列表中。接下来,我将一个AutoCompleteTextView分配给一个字段女巫,用于捕捉我使用的json对象。Json解析自动完成
在我的jsonarray其3个对象:ItemID,ItemDescription和ItemVolume。我的AutoCompleteTextView字段包含jsonObject:ItemDescription。
当我在onItemClickListner中选择itemDescription时,我需要获取itemID。
继承人的代码
ArrayAdapter<String> adp = new ArrayAdapter<String>(shoppingcart.this,
android.R.layout.simple_dropdown_item_1line, responseList);
autocomplete.setAdapter(adp);
autocomplete.setThreshold(1);
private class GetContacts extends AsyncTask<String, String, JSONObject> {
private ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(shoppingcart.this);
pDialog.setMessage("Getting Data ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected JSONObject doInBackground(String... args) {
JSONParser jParser = new JSONParser();
// Getting JSON from URL
JSONObject json = jParser.getJSONFromUrl(urlget);
return json;
}
@Override
protected void onPostExecute(JSONObject json) {
pDialog.dismiss();
autocomplete.setThreshold(1);
try {
// Getting JSON Array from URL
AllItems = json.getJSONArray(TAG_GETALL);
for(int i = 0; i < AllItems.length(); i++) {
JSONObject c = AllItems.getJSONObject(i);
// Storing JSON item in a Variable
ID = c.getString(TAG_ID);
Name = c.getString(TAG_NAME);
Pris = c.getString(TAG_PRICE);
// Adding value HashMap key => value
responseList.add(Name);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
编辑:
这是我所做的零部件清单。
HashMap<String,Integer> ItemAndID= new HashMap<String,Integer>();
List<String> responseList = new ArrayList<String>();
@Override
protected void onPostExecute(JSONObject json) {
pDialog.dismiss();
try {
AllItems = json.getJSONArray(TAG_GETALL);
for(int i = 0; i < AllItems.length(); i++) {
JSONObject c = AllItems.getJSONObject(i);
// Storing JSON item in a Variable
ID = c.getInt(TAG_ID);
Name = c.getString(TAG_NAME);
Pris = c.getInt(TAG_PRICE);
// Adding value HashMap key => value
responseList.add(Name);
ItemAndID.put(Name, ID);
}
对于的OnClick我这样做。
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String selectedName = (String) parent.getItemAtPosition(position);
// if you used a HashMap
ResultID = ItemAndID.get(selectedName);
}
伟大的回答,请参阅我的文章的结果。 – Jack
谢谢!伟大的:) –