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

  • SEARCH
  • Home
  • 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 7077631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:26:51+00:00 2026-05-28T06:26:51+00:00

I am developing Listview where Multiple selection is possible,now the problem is when i

  • 0

I am developing Listview where Multiple selection is possible,now the problem is when i scroll down or up the selection of items get lost and selection made on any other item except the selected one.

Below is my Activity.

  public class ContactPickerActivity extends Activity {

    private ArrayList<Contact> arr = new ArrayList<Contact>();
    private Context context;
    private ListView list;
    private ContactArrayAdapter adapter;
    private String strName,strNumber;
    private View view;  
    public static ArrayList<Boolean> arrBoolean = new ArrayList<Boolean>();

    @Override
    public void onCreate(Bundle savedInstance) {
        super.onCreate(savedInstance);
        setContentView(R.layout.contact_picker);
        ProgressDialog pd  = new ProgressDialog(this);
        list = (ListView)findViewById(R.id.contactList);
        arr = new ArrayList<Contact>();
        context = ContactPickerActivity.this;
        arr =  displayContacts();
        Log.i("ContactPicker", "Completed Displaying Contact list ::: " + arr.size());
        adapter = new ContactArrayAdapter(this,arr);
        list.setAdapter(adapter);
//              list.setAdapter(new ArrayAdapter<String>(this,
//                      android.R.layout.simple_list_item_multiple_choice, listContent));
        list.setOnItemClickListener(ContactSelectedListener);
        Log.i("Boolaean >>> ", arrBoolean.size() + "");


    }

    private OnItemClickListener ContactSelectedListener = new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View arg1, int arg2,long arg3) {
            Log.i("ListViewTest","Item Click");
            SparseBooleanArray checked = list.getCheckedItemPositions();
            for (int i = 0; i < arr.size(); i++) {
                Log.i("ListViewTest", arr.get(i)+ ": " + checked.get(i));
            }
        }
    };

    private ArrayList<Contact> displayContacts() {

        ContentResolver cr = getContentResolver();
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
        Contact contact;
        if (cur.getCount() > 0) {
            while (cur.moveToNext()) {
                contact = new Contact();
                String id = cur.getString(cur.getColumnIndex(People._ID));
                String name = cur.getString(cur.getColumnIndex(People.DISPLAY_NAME));
                contact.setName(name);
                arr.add(contact);
            }
        }
        return arr;
    }
}
********* Adapter *****

public class ContactArrayAdapter extends BaseAdapter {

//  private final List<Contact> list;
    private Context context;    

    private LayoutInflater mInflater;
    private List<Contact> list; 

     public ContactArrayAdapter(Context context,ArrayList<Contact> arrPublicData) {

        this.mInflater = LayoutInflater.from(context);
        this.list = arrPublicData;
    }

    public int getCount() {
        return list.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        final ViewHolder viewHolder;
        if (view == null) {         
            view = mInflater.inflate(R.layout.multiselect_row, null);
            viewHolder = new ViewHolder();
            viewHolder.text = (TextView) view.findViewById(R.id.txtItem);
            viewHolder.checkbox = (CheckBox) view.findViewById(R.id.chkItem);

            viewHolder.checkbox
            .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                    Contact element = (Contact) viewHolder.checkbox.getTag();
                    element.setSelected(buttonView.isChecked());
                    ContactPickerActivity.arrBoolean.add(buttonView.isChecked());
                    Log.i("Boolaean 123 >>> ", buttonView.isChecked() + "");
                }
            });
            view.setTag(viewHolder);
            viewHolder.checkbox.setTag(list.get(position));
        } else {
            viewHolder = (ViewHolder) view.getTag();
            ((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));
        }
        ViewHolder holder = (ViewHolder) view.getTag();
        holder.text.setText(list.get(position).getName());
        holder.checkbox.setChecked(list.get(position).isSelected());        
        return view;
    }

    static class ViewHolder {
    protected TextView text;
    protected CheckBox checkbox;
}

i m not able to get Items checked….
Any Help is appericiated…Thanx in Advance

  • 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-28T06:26:52+00:00Added an answer on May 28, 2026 at 6:26 am

    Problem is in your getView method, please correct this as follows:

        public class ContactPickerActivity extends Activity {
    
        private ArrayList<Contacts> arr = new ArrayList<Contacts>();
        private Context context;
        private ListView list;
        private ContactArrayAdapter adapter;
        private String strName,strNumber;
        private View view;  
        public static boolean[] arrBoolean = null;
    
        @Override
        public void onCreate(Bundle savedInstance) {
            super.onCreate(savedInstance);
            //setContentView(R.layout.contact_picker);
            ProgressDialog pd  = new ProgressDialog(this);
            list = (ListView)findViewById(R.id.contactList);
            arr = new ArrayList<Contact>();
            context = ContactPickerActivity.this;
            arr =  displayContacts();
            arrBoolean=new boolean[arr.size()];
            Arrays.fill(arrBoolean, false);
            Log.i("ContactPicker", "Completed Displaying Contact list ::: " + arr.size());
            adapter = new ContactArrayAdapter(this,arr);
            list.setAdapter(adapter);
    //              list.setAdapter(new ArrayAdapter<String>(this,
    //                      android.R.layout.simple_list_item_multiple_choice, listContent));
            list.setOnItemClickListener(ContactSelectedListener);
            Log.i("Boolaean >>> ", arrBoolean.size() + "");
    
    
        }
    
        private OnItemClickListener ContactSelectedListener = new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View arg1, int position,long arg3) {
                Log.i("ListViewTest","Item Click");
                arrBoolean[position]=!arrBoolean[position];
                adapter.notifyDataSetChanged();
            }
        };
    
        private ArrayList<Contacts> displayContacts() {
    
            ContentResolver cr = getContentResolver();
            Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
            Contacts contact;
            if (cur.getCount() > 0) {
                while (cur.moveToNext()) {
                    contact = new Contacts();
                    String id = cur.getString(cur.getColumnIndex(People._ID));
                    String name = cur.getString(cur.getColumnIndex(People.DISPLAY_NAME));
                    contact.setName(name);
                    arr.add(contact);
                }
            }
            return arr;
        }
    }
    //********* Adapter *****
    
    public class ContactArrayAdapter extends BaseAdapter {
    
    //  private final List<Contact> list;
        private Context context;    
    
        private LayoutInflater mInflater;
        private List<Contacts> list; 
    
         public ContactArrayAdapter(Context context,ArrayList<Contact> arrPublicData) {
    
            this.mInflater = LayoutInflater.from(context);
            this.list = arrPublicData;
        }
    
        public int getCount() {
            return list.size();
        }
    
        public Object getItem(int position) {
            return position;
        }
    
        public long getItemId(int position) {
            return position;
        }
    
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = convertView;
            final ViewHolder viewHolder;
            if (view == null) {         
                view = mInflater.inflate(R.layout.multiselect_row, null);
                viewHolder = new ViewHolder();
                viewHolder.text = (TextView) view.findViewById(R.id.txtItem);
                viewHolder.checkbox = (CheckBox) view.findViewById(R.id.chkItem);
    
    
                view.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) view.getTag();
            }
            ViewHolder holder = (ViewHolder) view.getTag();
            holder.text.setText(list.get(position).getName());
            holder.checkbox.setChecked(ContactPickerActivity.arrBoolean[position]);        
            return view;
        }
    
        static class ViewHolder {
        protected TextView text;
        protected CheckBox checkbox;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having this trouble when developing an endless listview..As I scroll down to
I am developing one Android application having the list view of items. Those listview
I'm having a problem with a user control I'm developing which contains a ListView
My problem: My ListView resets its scroll position to the top whenever I update
Developing a heavily XML-based Java-application, I recently encountered an interesting problem on Ubuntu Linux.
Developing a website and just trying to get back into the swing of (clever)
When developing a desktop application in .NET, is it possible to not require the
I'm developing a ListView in an Android Fragment. The class extends ListFragment. I tried
I am developing Android Application having the listview in an activity . I implemented
I have been developing Android application which has 3 ListView and one ContextMenu for

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.