2012-11-01 62 views
0

我有一个ListView充气JSONObjects。它工作正常list.setOnItemClickListener。 我使用EditText添加了搜索工具,并在运行时搜索JSONArray中的Items(包含JSONObjects)。 但是在搜索后,当我点击ListView项时,它显示了JSONArray中的原始位置。 例如,如果我有这些项目的ListView。android ListView onClick获取JSONObject

Africa 
Asia 
Europe 
North America 
South America 

[ListView中的项目的位置是从0 - 4,即非洲= 0,南美= 4]

现在,如果我使用的EditText场的搜索栏,并在其中键入America ,它现在会显示我有两个项目的列表视图。

North America 
South America 

现在的ListView对运行时间两个项目,如果我点击North America它会告诉我的位置[0] JSONArray,这本来是Africa,所以,而不是显示我的信息/数据的约North America它显示我我点击Africa

我怎样才能得到ListView的JSONObject单击但位置ID?

/*填充的ListView的onCreate()*/

public void populateCityList() { 
    List<CityRowItem> rowItems; 
    rowItems = new ArrayList<CityRowItem>(); 
    try { 
     jArray = readJSONFile(); 
     if (jArray != null) { 

      for (int i = 0; i < jArray.length(); i++) { 
       JSONObject j1 = jArray.getJSONObject(i); 
       CityRowItem item = new CityRowItem(R.drawable.grain, 
         j1.getString("city_name_eng"), 
         j1.getString("city_name_alias"), R.drawable.city); 
       rowItems.add(item); 
      } 

      CityListAdapter myadapter = new CityListAdapter(
        GetCityManual.this, R.layout.list_item_city, rowItems); 
      list.setAdapter(null); 
      list.setAdapter(myadapter); 
        } 
    } 
    catch (JSONException) { 
     ; 
    } 
} 

/*这是的onClick监听*/

list.setOnItemClickListener(new OnItemClickListener() { 
    public void onItemClick(AdapterView<?> parent, View view, 
      int position, long id) { 
     try { 
      JSONObject j1 = jArray.getJSONObject(position);     
      String CityName = jsonObj.getString("city_name_eng") 
        .toString(); 
      Intent myIntent = new Intent(GetCityManual.this, 
        GetCategory.class); 
      myIntent.putExtra("objCity", CityName); 
     } 
     catch (JSONException e) 
     { 
      ; 
     } 
    } 
} 

/*这一段代码反映了使用的EditText搜索的功能*/

txtSearch.addTextChangedListener(new TextWatcher() { 

    public void onTextChanged(CharSequence s, int start, int before, 
      int count) { 
     // TODO Auto-generated method stub 

    } 

    public void beforeTextChanged(CharSequence s, int start, int count, 
      int after) { 

    } 

    public void afterTextChanged(Editable s) { 
     List<CityRowItem> rowItems; 
     rowItems = new ArrayList<CityRowItem>(); 
     if (jArray != null) { 
      try { 
       for (int i = 0; i < jArray.length(); i++) { 
        JSONObject j1 = jArray.getJSONObject(i); 
        if (j1.getString("city_name_eng").toLowerCase() 
          .contains(s.toString().toLowerCase())) { 
         CityRowItem item = new CityRowItem(
           R.drawable.grain, j1 
             .getString("city_name_eng"), j1 
             .getString("city_name_alias"),R.drawable.city); 
         rowItems.add(item); 
        } 
        CityListAdapter myadapter = new CityListAdapter(
          GetCityManual.this, 
          R.layout.list_item_city, rowItems); 
        list.setAdapter(null); 
        list.setAdapter(myadapter); 
       } 
      } 
      } catch (Exception _e) { 
       ; 
      } 
} 
} 
+1

希望你现在感觉好点:) – Zeeshan

+0

我想你应该让我们看看更多的代码。 – Misca

+0

代码在那里。 帮助,将不胜感激。谢谢。 – Zeeshan

回答

0

如果您从实际列表适配器中选择了位置,您更改了您的 列表适配器,所以你从那里得到数字。您应保留原始位置 其他

0

您的onItemClick会显示您单击的列表的当前位置。 你可以在你的课堂上有一个List<CityRowItem> CurrentRowItems;,并且在init和afterTextChanged中保留对该列表的引用this.CurrentRowItems = rowItems;。 当点击事件发生时,只有CityRowItem city = this.CurrentRowItems.get(position);。 另外我建议只解码json文件一次,然后使用列表工作。

+0

谢谢。但是onClick,我必须将JSONObject传递给Intent。我如何用CityRowItem城市做到这一点? – Zeeshan

+0

好吧,然后只是添加我的变量从你解析的地方,以便当你有一个点击,你会得到CityRowItem city = this.CurrentRowItems.get(position);然后发送jArray.getJSONObject(city.i);的JSONObject。 – Misca

+0

'TextView checkTV =(TextView)list.getChildAt(position).findViewById(R.id.cityTitle);'? 我仍然收到NullException。 如果我能得到该特定点击项目的文本,我也可以执行搜索。 – Zeeshan