[英]Custom listview click listener not working perfectly
我只需要在listview click listner中遇到棘手情况的帮助。 事实是,我已经在列表视图中填充了来自远程服务器的自定义适配器。 我的列表视图包含4个元素2个图像和2个文本视图,因此该列表视图可以完美地从数据库加载,但是问题是我希望列表视图仅在某些条件匹配时才能工作。 看一下我的代码:
if (convertView == null) {
convertView = inflater.inflate(resource, null);
}
ImageView team1_photo,team2_photo;
TextView tournament;
team1_photo = (ImageView) convertView.findViewById(R.id.team1);
team2_photo = (ImageView) convertView.findViewById(R.id.team2);
tournament=(TextView) convertView.findViewById(R.id.tournament);
status=(TextView) convertView.findViewById(R.id.status);
ImageLoader.getInstance().displayImage(live_conveyerList.get(position).getTeam1_photo(), team1_photo);
ImageLoader.getInstance().displayImage(live_conveyerList.get(position).getTeam2_photo(), team2_photo);
tournament.setText(live_conveyerList.get(position).getTournament());
status.setText(live_conveyerList.get(position).getStatus());
liveListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 0:
if(status.getText().equals("play on"))
{
Toast.makeText(getActivity(),"Result will be declared shortly",Toast.LENGTH_LONG).show();
}
else {
Intent fix1 = new Intent(getActivity(), Leaderboard.class);
startActivity(fix1);
}
break;
case 1:
if(status.getText().equals("play on"))
{
Toast.makeText(getActivity(), "Result will be declared shortly", Toast.LENGTH_LONG).show();
}
else{
Intent fix2 = new Intent(getActivity(), Leaderboard_1.class);
startActivity(fix2);
}
break;
default:
}
getActivity().overridePendingTransition(R.anim.activity_in, R.anim.activity_out);
}
}
);
return convertView;
}
}
因此,这里的状态是文本视图之一,它具有两种类型的输出。 一个是“正在播放”,另一个是“正在播放”,所以我只想在新活动已填充了该行中的特殊行的情况下打开新活动。 埃尔斯,只应该敬酒,但它对我没有用。 任何帮助都很好,谢谢。
我的json就是这样
{"list":[{"tournament":"Xavier college League","team1_photo":"http:\/\/thehostels.in\/judgement_files\/images\/India.png","team2_photo":"http:\/\/thehostels.in\/judgement_files\/images\/Australia.png","status":"play on"},{"tournament":"ITM college League","team1_photo":"http:\/\/thehostels.in\/judgement_files\/images\/India.png","team2_photo":"http:\/\/thehostels.in\/judgement_files\/images\/Australia.png","status":"play off"}]}
在您的情况下,您0的“下注”为大写P,但默认情况下,等于会考虑大小写,这可能是您的问题吗?
编辑:好的,我想我明白了。您不仅要定义onItemClickListener,而且要为每个项目定义一次,以便在列表填充的末尾,您的侦听器基于列表的最后一项的状态。您应该仅定义一次onItemClickListener ,您将单击带有视图和位置参数的项目
例如,您可能希望将所有项目的列表存储为字段,在onItemClickListener中,您将从该位置获得被单击的项目,从而获得其状态
使用view参数,您应该能够通过findViewById访问字段的值
编辑2:我的意思是:仅需要定义一个onItemClickListener,onItemClick方法将提供所单击项目的位置和视图。 这意味着用户每次单击列表中的任何项目时,都会调用该侦听器,但是每行不同的侦听器没有一个。
考虑到这一点,在onItemClick中,使用视图和/或位置,您可以访问状态textview,获取其值,然后执行所有需要做的与敬酒和意图有关的工作。 我希望比以前更清楚
编辑3:那应该做你的事情:在您的侦听器中,在切换之前,TextView currentStatus =(TextView)convertView.findViewById(R.id.status);
然后,用currentStatus替换侦听器中对状态的所有引用。 而且,您只需在getView方法之外(甚至在适配器之外)定义一次侦听器,而无需定义listView并将适配器添加到其中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.