2016-03-16 110 views
0

我做了一个活动,获取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); 



     } 

回答

1

See this example你是不是节能项目Id任何地方。如果你不想创建一个新的对象,你可以创建一个新的列表来保存ItemIds,或者你可以像[“name1”:“id1”,“name2”:“id2”]一样创建一个HaspMap<String, String>,然后根据什么您使用保存项目Id

setOnItemClickListener(new OnItemClickListener() { 
    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {    
     String selectedName = (String) parent.getItemAtPosition(position); 
     // if you used a HashMap 
     String ID = exampleHashMap.get(selectedName); 
    } 
} 
+0

伟大的回答,请参阅我的文章的结果。 – Jack

+0

谢谢!伟大的:) –

0

创建一个新的名单像responseListItemId和剥它的旁边responseList:

List<Integer> responseListItemId = new ArrayList<Integer>(); 
..... 
@Override 
protected void onPostExecute(JSONObject json) { 

    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); 
    responseListItemId.add(ID); 

} 

现在从responseList获得描述的指标值,并用它在responseListItemId得到你的itemID。