Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7685441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:15:08+00:00 2026-05-31T19:15:08+00:00

I made a custom Listview (Without overriding getView() method) with each item in a

  • 0

I made a custom Listview (Without overriding getView() method) with each item in a Listview having a following Layout

contactlayout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="horizontal"
  android:layout_width="match_parent"
  android:layout_height="match_parent" android:weightSum="1">
    <CheckBox android:id="@+id/checkBox1" android:text="CheckBox" android:layout_width="134dp" android:layout_height="108dp" android:focusable="false"></CheckBox>
    <LinearLayout android:id="@+id/linearLayout1" android:layout_height="match_parent" android:orientation="vertical" android:layout_width="87dp" android:layout_weight="0.84" android:weightSum="1" >
        <TextView android:text="TextView" android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/name" android:layout_weight="0.03"></TextView>
        <TextView android:text="TextView" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/phone"></TextView>
        <TextView android:text="TextView" android:layout_height="wrap_content" android:layout_weight="0.03" android:layout_width="match_parent" android:id="@+id/contactid" android:visibility="invisible"></TextView>
    </LinearLayout>
</LinearLayout>

I am populating the Listview using a SimpleCursorAdapter in a following way…

Cursor c = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);

        String from[] = new String[]{ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,ContactsContract.CommonDataKinds.Phone.NUMBER,ContactsContract.CommonDataKinds.Phone.CONTACT_ID};
        int to[] = new int[]{R.id.name,R.id.phone,R.id.contactid};
        SimpleCursorAdapter s = new SimpleCursorAdapter(this,R.layout.contactlayout,c,from,to);
        lv.setAdapter(s);

On Click of a button I am reading the states of all the Checkboxes. The problem is, if I check one CheckBox several others down the line get automatically Checked. I know this is reusing of Views. How do I avoid it ?. I am not even overriding getView() method in this case, so I wonder if there is still any way to achieve what I want?

Answer

Finally I implemented what @sastraxi suggested…

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);

        final CheckBox checkBox = (CheckBox) view.findViewById(R.id.checkBox1);
        final TextView name = (TextView) view.findViewById(R.id.name);
        final TextView contactId = (TextView) view.findViewById(R.id.contactid);
        final int pos = position;
        checkBox.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                if(checkBox.isChecked())
                {
                    checkList.add(String.valueOf(pos));
                    nameList.add(name.getText().toString());
                    contactList.add(contactId.getText().toString());
                    Log.i("Chk added",String.valueOf(pos));
                }
                else
                {
                    checkList.remove(String.valueOf(pos));
                    nameList.remove(name.getText().toString());
                    contactList.remove(contactId.getText().toString());
                    Log.i("Un Chk removed",String.valueOf(pos));
                }
            }
        });

        if(checkList.contains(String.valueOf(pos)))
        {
            checkBox.setChecked(true);
        }
        else
        {
            checkBox.setChecked(false);
        }

        return view;
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T19:15:09+00:00Added an answer on May 31, 2026 at 7:15 pm

    Ah, I see what the problem is.

    Make a new class that extends SimpleCursorAdapter, say CheckboxSimpleCursorAdapter, and override getView as such:

    public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);
        CheckBox checkBox = (CheckBox) view.findViewById(R.id.CheckBox1);
        checkBox.setChecked(getIsThisListPositionChecked(position));
        return view;
    }
    

    As you’re not using a layout whose top-level View implements Checkable, you have to do everything yourself. That includes clearing state (in this case), as the default implementation re-used a View that was checked–as you correctly intuited.

    Edit: use this new code, and implement a protected boolean getIsThisListPositionChecked(int position) method that returns whether or not the item is currently checked (or something like that). I hope I’m being clear enough–you need to figure out if the item should be checked according to your model, and then set that when you create the View.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have made custom .xml layout for ListView. Now it looks: But it isn't
I created a ListView with a custom layout for each view. I have several
I have made a custom listView which which extends ListActivity and each row contains
I made custom downloader of IE using IDownloader Interface and URLDownloadToFile method. But URLDownloadToFile
I made a custom ant script to automatically create a jar file each time
I'm using some reflection to set up a ListView made up of custom views.
I am starting out on Android and trying to make a custom ListView layout.
I have made a custom ArrayAdapter for a ListView, in order to customize the
i have a ListView for which i have made a custom adapter which extends
I made a custom TObjectList descendant designed to hold subclasses of a base object

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.