我有一个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) {
;
}
}
}
希望你现在感觉好点:) – Zeeshan
我想你应该让我们看看更多的代码。 – Misca
代码在那里。 帮助,将不胜感激。谢谢。 – Zeeshan