0

I have a layout for my list row with some text and a layout which is supposed to emulate a button. The problem I'm facing is I will always get the latest data in the cursor, meaning if I click the layout on row one I should get grp id 1 and grp name ONE, but I'm getting grp id 3, grp name THREE regardless of which layout I click on in the listview. What's the way to fix this? My code:

In the getView method

ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            holder.text2 = (TextView) vi.findViewById(R.id.text2);
            holder.layout1 = (LinearLayout)vi.findViewById(R.id.info);
            vi.setTag(holder);
        } else {
            holder = (ViewHolder) vi.getTag();
        }

        String grpCount = c.getString(c
                .getColumnIndex(TestDbAdapter.KEY_GRP_COUNT));

        holder.text2.setText("Channels: " + grpCount);

        holder.layout1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                System.out.println(c.getString(c.getColumnIndex(TestDbAdapter.KEY_GRP_NAME)));
                System.out.println(c.getString(c.getColumnIndex(TestDbAdapter.KEY_GRP_ID)));
            }
        });

        return vi;

1 Answer 1

1

Just check it, I think it should get fix like this:

 String grpCount = c.getString(c
                .getColumnIndex(TestDbAdapter.KEY_GRP_COUNT));
final String grpName = c.getString(c.getColumnIndex(TestDbAdapter.KEY_GRP_NAME));
final String grpId = c.getString(c.getColumnIndex(TestDbAdapter.KEY_GRP_ID));

        holder.text2.setText("Channels: " + grpCount);

        holder.layout1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                System.out.println(grpName);
                System.out.println(grpId);
            }
        });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.