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 6926247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:53:36+00:00 2026-05-27T10:53:36+00:00

I found here and here similar issues, but the problem is slightly different. In

  • 0

I found here and here similar issues, but the problem is slightly different.

In a ListView, I try to put an adapter (extends from base Adapter) which contents checkbox.

List view layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    style="@style/mainLayout"
 >

    <Button
        android:id="@+id/close_cat"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="buttonClick"
        android:text="@string/back" />

    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</LinearLayout>

XML of list’s elements:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <TableLayout
        android:id="@+id/blocCheck"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="25px"
        android:layout_marginTop="25px"
        android:background="@color/black" >

        <TableRow
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:baselineAligned="true"
            android:paddingLeft="4sp" >

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_weight="1"
                android:orientation="horizontal"
                android:paddingLeft="20sp" >

                <ImageView
                    android:id="@+id/iv_cat"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

                <TextView
                    android:id="@+id/category"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="left"
                    android:layout_marginLeft="15dip"
                    android:text="tfgldkjhglf"
                    android:textSize="20sp" />
            </LinearLayout>

            <CheckBox
                android:id="@+id/check_cat"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:layout_marginRight="10sp"
                android:onClick="MyHandler" />
        </TableRow>
    </TableLayout>

</LinearLayout>

I set adapter in my view :

list = getListView();
list.setAdapter(new CatAdapter(this, listCat));

My adapter:

public class CatAdapter extends BaseAdapter {

    private LayoutInflater  mInflater;
    private List<CatAdapterObject> mListAppInfo;
    private Context mContext;


    /**
     * Constructor
     * @param context
     * @param data
     * @param resource
     * @param from
     * @param to
     */
    public CatAdapter (Context context, List<CatAdapterObject> list){   
        mContext = context;
        mListAppInfo = list;
        mInflater = LayoutInflater.from(mContext);      
    }

    /**
     * number of elements
     */
    @Override
    public int getCount() {
        return mListAppInfo.size();
    }

    /**
     * get an item in the list
     */
    @Override
    public Object getItem (int position){
        return mListAppInfo.get(position).getId();
    }

    /**
     * get id of the selected item
     */
    @Override
    public long getItemId(int position) {
        return mListAppInfo.get(position).getId();
    }

    /**
     * get the view
     */
    @Override
    public View getView (int position, View convertView, ViewGroup parent){

        LinearLayout layoutItem;

        if (convertView == null) {
            layoutItem = (LinearLayout) mInflater.inflate(R.layout.category,    parent, false);
        } else {
            layoutItem = (LinearLayout) convertView;
        }

        // get the current category
        CatAdapterObject entry =  mListAppInfo.get(position);

        //view setting
        TextView category = (TextView) layoutItem.findViewById(R.id.category);
        CheckBox cb         = (CheckBox) layoutItem.findViewById(R.id.check_cat);
        ImageView iv_cat    = (ImageView) layoutItem.findViewById(R.id.iv_cat);

        //elements setting
        category.setText(entry.getName());

        //checkbox
        cb.setChecked(entry.isChecked());
        cb.setTag(position);

        //picture
        Bitmap icon = Utils.getResizedBitmap(BitmapFactory.decodeResource(mContext.getResources(),entry.getPict()),45,45,true);     
        iv_cat.setImageBitmap(icon);

        return layoutItem;

    }
}

When activity is lauched, list is displayed perfectly. I can scroll up/down without any problem. However, when I check one of the checkboxes and scroll down, box which was checked “loose” its check mark and the background color of the latest box in the list changed.

When checking a box, below method is called (In my activity file):

public void MyHandler(View v) {

        //クリックされたcheckbox
        cb = (CheckBox) v;

        Log.d("CHECKBOX","before :"+Utils.implode(",",this.cat_id_list));

        //get position in the list
        Integer position = Integer.parseInt(cb.getTag().toString());

        //instance of the view (list's child)
        View o = list.getChildAt(position).findViewById(R.id.blocCheck);

        // check if box is checked
        if (cb.isChecked()) { 
            o.setBackgroundResource(R.color.pink);

            this.cat_id_list.add(position.toString());

        // 背景色を替え、リストから削除
        } else { 

            o.setBackgroundResource(R.color.black);

            Collections.sort(this.cat_id_list);
            int index = Collections.binarySearch(this.cat_id_list,position.toString());
            this.cat_id_list.remove(index);

        }

        Log.d("CHECKBOX","after :"+Utils.implode(",",this.cat_id_list));
    }

There are 8 elements in my list but when I check the “mChildrenCount” of the list, there are only 5 elements.

I just want to save in the “this.cat_id_list” id of elements wich were checked and change background color of selected item.

I would appreciate your help !

  • 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-27T10:53:37+00:00Added an answer on May 27, 2026 at 10:53 am

    In your getView you have to something like this (this code is give you an idea how it will work in your case):

    ListView lv = ((ListActivity)context).getListView();
    // Containing all check states
    SparseBooleanArray sba = lv.getCheckedItemPositions();
    
    CheckBox cb = (CheckBox) convertView.findViewById(R.id.yourcheckbox);
    
    cb.setChecked(false);
    
    if(sba != null)
      if(sba.get(position))
         cb.setChecked(true);
    

    That said this what you need on the Activity side.

    You have make your ListView to multi-selection mode by android:choiceMode in xml or using setChoiceMode. You check box should be non-clickable and non-focusable.

    You have to remove your onClick listener on buttons. Whatever you doing in onClick of button, you have to add that logic to the onListItemClick of ListActivtiy.

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

Sidebar

Related Questions

I found some posts with similar issues, but this is something different. I upgraded
I have searched for similar issues but I found nothing... I have a problem
I can't resolve this issue, I found a similar question here but: setting the
I've found a similar question here , but I'm looking for more general solutions.
I found a few questions similar to this one here on SO, but none
This is my first time posting -- I found similar issues but not anything
I've been searching all day and found several similar issues, but none have resolved
Before anyone jumps on me, I have found a similar issue here , but
For the record: I found a similar question here but I have to elaborate
I found this on Google, click here , which someone asked a similar question,

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.