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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:00:27+00:00 2026-05-24T09:00:27+00:00

Hi i am using the following code to generate a custom listview with checkbox.how

  • 0

Hi i am using the following code to generate a custom listview with checkbox.how can i get the index of the checked rows when the user clicks on the button?

package com.CustomListView;

import android.app.Activity;
import android.os.Bundle;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class CustomListViewActivity extends Activity {
    ListView mListview;

    Button btn;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mListview=(ListView)findViewById(R.id.listview);
        mListview.setAdapter(new mCustomList(this));


        btn =(Button) findViewById(R.id.button1);
        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub


            }
        });


    }
    public class mCustomList extends BaseAdapter{
        private Context mContext;

        public mCustomList(Context c){
            mContext = c;
        }
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return COUNTRIES.length;
        }

        @Override
        public Object getItem(int arg0) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public long getItemId(int arg0) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public View getView(int position, View converView, ViewGroup parent) {
            View List;
            if(converView==null){
                List=new View(mContext);
                LayoutInflater mLayoutinflater=getLayoutInflater();
                List=mLayoutinflater.inflate(R.layout.mylist, parent, false);
            }
            else{
                List = (View)converView;
            }
            ImageView imageView = (ImageView)List.findViewById(R.id.imgview);
            TextView textView = (TextView)List.findViewById(R.id.text);
            CheckBox chkbox=(CheckBox)List.findViewById(R.id.chkbox);
            textView.setText(COUNTRIES[position]);

            // TODO Auto-generated method stub
            return List;
        }

    }

    static final String[] COUNTRIES = new String[] {
        "Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra",
        "Angola", "Anguilla", "Antarctica", "Antigua and Barbuda", "Argentina",

    };
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<ListView
android:id="@+id/listview"
android:layout_width="fill_parent"
android:layout_height="300dp"
android:scrollbars="none"
></ListView>
<Button android:text="Button" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>


</LinearLayout>

mylist.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
>
<TextView android:id="@+id/text" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textColor="#099900" />
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:scaleType="center"
android:id="@+id/imgview" android:src="@drawable/icon" android:layout_centerInParent="true" />
<CheckBox android:id="@+id/chkbox" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:checked="true" android:layout_alignParentRight="true" android:layout_marginRight="10dip" />
</RelativeLayout>
  • 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-24T09:00:29+00:00Added an answer on May 24, 2026 at 9:00 am

    first change your getView because findViewById is called for every view which is wrong.
    you have a boolean array with false set for every item in the list. Than at every change in checked state of your check box, you change the corresponding boolean to true/false(checked/unchecked). Than

     if(converView==null){
                List=new View(mContext);
                LayoutInflater mLayoutinflater=getLayoutInflater();
                List=mLayoutinflater.inflate(R.layout.mylist, parent, false);
                ImageView imageView = (ImageView)List.findViewById(R.id.imgview);
                TextView textView = (TextView)List.findViewById(R.id.text);
                CheckBox chkbox=(CheckBox)List.findViewById(R.id.chkbox);
                chkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    
                     @Override
                     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                       checkedArray[position] = isChecked;
                       //process your array. do something with indexes that correspond to a true value. 
                       } 
                 }
    
             }
             });
    
    } else{
        List = (View)converView;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using the following code to generate an encrypted token: var ticket =
Using the following code I get a nice formatted string: Request.QueryString.ToString Gives me something
I am following this tutorial, I'm using netbeans 6.5.1 http://www.netbeans.org/kb/docs/java/gui-db-custom.html When I get to
I am using the following code to generate and parse the XML. The method
I am using the following code: PHP: // Generate Guid function NewGuid() { $s
I am using a custom Alert dialog. If user go with negative button of
I am using following code to get List from xml file - public static
In my codeigniter controller function I am using the following code to generate my
I am using the following code to generate a text: for i in xrange(300):
I'm using the following code to generate an Excel file using Microsoft.Interop.Excel. The problem

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.