I just needed help with a tricky situation in listview click listner. The thing is that I have populated a listview with custom adapters from remote servers. My listview contains 4 elements 2 images and 2 textview so the listview is loading perfectly from database but the issue is that I want listview to work only if the certain conditions match. Have a look at my code:
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;
}
}
So here, status is one of the textviews and it has two type of outputs. One is "play on" and other is "play off" so I want to open new activity only when it has populated play on in that peculiar row. Els,e there should be a toast only, but it is not working for me. Any help would be nice, thanks.
my json is like this
{"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"}]}
In your case 0 you have "Play on" with a capital P, but equals takes the case into account by default, could it be your issue?
Edit : Ok I think I got it You define the onItemClickListener not once but for each and every item, so that at the end of list population, your listener is based on the status of the last item of your list You should define onItemClickListener only once, and you will have clicked item with view and position arguments
For instance you may want to store the list of all your items as a field, and in your onItemClickListener you will get clicked item from the position, and thus get its status
With the view parameter, you should be able to access the fields' values through findViewById
Edit 2: What I meant is the following : there is only one onItemClickListener that needs to be defined and onItemClick method will provide the position and the view of clicked item. That means that each time user will click any item of the list, the listener will be called, but there is not one different listener for each different row.
With that in mind, in the onItemClick, with the view and/or the position, you can access the status textview, get its value and then do all the stuff you need to do with your toast and intent. I hope it's clearer than before
Edit 3: That should do your thing : in your listener, before your switch, TextView currentStatus=(TextView) convertView.findViewById(R.id.status);
And then you replace all the references to status inside your listener by currentStatus. And you will need to define the listener only once outside of your getView method, even outside your adapter, rather where your listView is defined and your adapter is added to it