Jay
Jay

Reputation: 5084

Android ListView Adapter OnClick selects wrong Position

I went through all the answers and questions on StackOverflow regarding this but I can't seem to edit my code to rectify the issue.

Basically, I have a button that needs an OnClick listener set inside the ListView Adapter. Each row has a unique ID and the wrong id gets selected when I click on the button, which basically means its selecting the wrong position.

Here's my Adapter code:

public class FeedAdapter extends BaseAdapter {

    ArrayList<ActivityObject> actList = new ArrayList<ActivityObject>();
    String currActID;

    //More code

     public View getView(int position, View convertView, ViewGroup parent) {

        Holder holder;
        final ActivityObject act = actList.get(position);
        currActID = act.getId();

        if (convertView == null) {
             convertView = inflater.inflate(R.layout.feed_single_picture, parent, false);
             holder = new Holder();
             holder.comment = (ImageButton) convertView.findViewById(R.id.commentbutton);
             convertView.setTag(holder);
       } else {
             holder = (Holder) convertView.getTag();
       }

       holder.comment.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                //Log.d("Check",currActID);
                //Selects the Wrong ID
                Intent myIntent = new Intent(activity, CommentActivity.class);
                v.getContext().startActivity(myIntent);
             }
       });

       return convertView;
    }

What am I doing wrong guys? Can you point out the mistake please.

Upvotes: 0

Views: 1500

Answers (1)

Kosh
Kosh

Reputation: 6334

String currActID = act.getId(); 

instead of making it global.

why? because simply currActID will always equal the last position unless u scroll then it will again becomes the id of the scrolled last position.

Upvotes: 3

Related Questions