android ListView onClick获取JSONObject
我有一个由JSONObject膨胀的列表视图。对于android ListView onClick获取JSONObject,android,json,android-listview,onclicklistener,Android,Json,Android Listview,Onclicklistener,我有一个由JSONObject膨胀的列表视图。对于list.setOnItemClickListener,它工作正常。 我使用EditText添加了搜索工具,在运行时它搜索JSONArray中的项(包含JSONObject)。 但是在搜索之后,当我单击ListView项时,它会显示JSONArray中的原始位置。 例如,如果我有这些项目的列表视图 Africa Asia Europe North America South America [列表视图中项目的位置为0-4,即非洲=0,南美=4]
list.setOnItemClickListener
,它工作正常。
我使用EditText添加了搜索工具,在运行时它搜索JSONArray中的项(包含JSONObject)。
但是在搜索之后,当我单击ListView项时,它会显示JSONArray中的原始位置。
例如,如果我有这些项目的列表视图
Africa
Asia
Europe
North America
South America
[列表视图中项目的位置为0-4,即非洲=0,南美=4]
现在,如果我使用EditText字段作为搜索栏,并在其中键入America
,它将显示包含2个项目的listview
North America
South America
现在,ListView在运行时有两个项目,如果我单击北美
,它会显示JSONArray的位置[0],原来是非洲
,因此,它不会显示有关北美
的信息/数据,而是显示我单击了非洲
如何从ListView中获取JSONObject,但单击位置id
/*填充ListView onCreate()*/
public void populateCityList(){
列出项目;
rowItems=新的ArrayList();
试一试{
jArray=readJSONFile();
if(jArray!=null){
for(int i=0;i
/*这是onClick Listener*/
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");
Intent myIntent = new Intent(GetCityManual.this,
GetCategory.class);
myIntent.putExtra("objCity", CityName);
}
catch (JSONException e)
{
;
}
}
}
list.setOnItemClickListener(新的OnItemClickListener(){
public void onItemClick(AdapterView父级、视图、,
内部位置,长id){
试一试{
JSONObject j1=jArray.getJSONObject(位置);
String CityName=jsonObj.getString(“城市名称”);
Intent myIntent=新Intent(GetCityManual.this,
GetCategory.class);
myIntent.putExtra(“对象”,城市名称);
}
捕获(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) {
;
}
}
}
txtSearch.addTextChangedListener(新的TextWatcher(){
public void onTextChanged(字符序列,int start,int before,
整数计数){
//TODO自动生成的方法存根
}
更改前的公共无效(字符序列、整数开始、整数计数、,
整数后){
}
公共无效后文本已更改(可编辑){
列出项目;
rowItems=新的ArrayList();
if(jArray!=null){
试一试{
for(int i=0;i
列表适配器从实际列表适配器中选择位置后,您更改了
列出适配器,这样你就可以从那里得到数字。你应该把初始位置放在某个地方
否则您的onItemClick将显示您单击的列表的当前位置。 您可以在类中以及在init和afterTextChanged中拥有一个
列表CurrentRowItems;
,并保留对该列表的引用this.CurrentRowItems=rowItems;
。
当点击事件发生时,CityRowItem city=this.CurrentRowItems.get(position);
。
另外,我建议只解码json文件一次,然后再处理列表。希望您现在感觉好些:)我想你应该让我们看看更多的代码。代码就在那里。我们将非常感谢你的帮助。谢谢。谢谢。但是一旦单击,我必须将JSONObject传递给Intent。我如何使用CityRowItem city?那么,只需将解析位置的for中的I变量添加到列表中,以便在单击时获得CityRowItem city=this.CurrentRowItems.get(position);然后发送jArray.getJSONObject(city.i);?我仍然收到NullException。如果我可以获取特定单击项的文本,我也可以执行搜索。那么您应该尝试TextView checkTV=(TextView)view.findViewById(R.id.Citytle);其中视图是您在回调中发送的单击视图
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) {
;
}
}
}