I am trying to show all the question in a custom adapter. I have set the arrows for upvote and downvote as well. The question is well displayed in custom form. The problem is that when I click the upvote button of any of the elements in the list, the upvote functionality of last item is done. I need that the up vote to be done to that specific question whose button is clicked. To check it, I kept a toast in upvote button so that it displays the question of that element, but the result was it showed only of last item.
Code is given here
public class CustomForumAdapter extends ArrayAdapter {
List<Question> mList;
Context mContext;
LayoutInflater inflater;
public CustomForumAdapter(Context context, List<Question> list) {
super(context, R.layout.design_forum, list);
mContext = context;
mList = list;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
inflater = LayoutInflater.from(mContext);
Holder holder = null;
if (convertView == null) {
holder=new Holder();
convertView = inflater.inflate(R.layout.design_forum, parent, false);
holder.upVote = (ImageButton) convertView.findViewById(R.id.upVote);
holder.downVote = (ImageButton) convertView.findViewById(R.id.downVote);
holder.timeAsked = (TextView) convertView.findViewById(R.id.timeAsked);
holder.asker = (TextView) convertView.findViewById(R.id.asker);
holder.mainAnswer = (TextView) convertView.findViewById(R.id.mainAnswer);
holder.mainQuestion = (TextView) convertView.findViewById(R.id.mainQuestion);
holder.rating = (TextView) convertView.findViewById(R.id.rating);
convertView.setTag(holder);
} else {
holder = (Holder) convertView.getTag();
}
holder.upVote.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.v("adsl", "upvote");
}
});
holder.downVote.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
holder.mainQuestion.setText(mList.get(position).getMainQuestion());
holder.mainAnswer.setText(mList.get(position).getMainAnswer());
holder.asker.setText(mList.get(position).getAsker());
holder.timeAsked.setText(mList.get(position).getTime());
holder.rating.setText(mList.get(position).getRating() + "");
return convertView;
}
class Holder {
private ImageButton upVote, downVote;
private TextView timeAsked, asker, mainQuestion, mainAnswer, rating;
}
@Override
public int getCount() {
return mList.size();
}
@Override
public Question getItem(int position) {
return mList.get(position);
}
@Override
public long getItemId(int position) {
return super.getItemId(position);
}
@Override
public int getPosition(Object item) {
return super.getPosition(item);
}
Updated: I kept the part of actual rating there. I also kept the new onclick listener. But bro can you please briefly tell what are you trying to say. (My question model has mainQuestion, mainAnswer, asker, time and rating)
second update: I again edited the question and used view holder this time. The last thing that is remaining is the individual button click of a invidual item. What should I do further?