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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:09:15+00:00 2026-06-09T19:09:15+00:00

I want to have a listview with textview + check box. I was able

  • 0

I want to have a listview with textview + check box. I was able to create the listview. I can capture listview item select. However when I try to capture check box select, unselect I get a null pointer exception. How to write the setOnCheckedChangeListener() for the check box..

public class LocationActivity extends Activity{



@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setContentView(R.layout.locationmain);

    ListView listview = (ListView) findViewById(R.id.listView1);

    String selectQuery = "SELECT  * FROM " +     DatabaseHandler.TABLE_LOCATIONLABLES;
    SQLiteDatabase db = new DatabaseHandler(this).getReadableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    final List<String> locLables = new ArrayList<String>();

    if(cursor != null){
        if (cursor.moveToFirst()) {
            do {

                locLables.add(cursor.getString(1));

            } while (cursor.moveToNext());
        }
    }

    //String[] locLables = new String[] {"Home","University","Office"};
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.locationmain_entry,R.id.textView12, locLables);

    //cb gives a null pointer exception
    CheckBox cb = (CheckBox) listview.findViewById(R.id.checkBox12);

    cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // TODO Auto-generated method stub
            if(isChecked){
                System.out.println("selected");
            }else if(!isChecked){
                System.out.println("not selected");
            }

        }
    });     
    listview.setAdapter(adapter);


    listview.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), locLables.get(arg2), Toast.LENGTH_SHORT).show();
        }
    });
}
  • 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-06-09T19:09:17+00:00Added an answer on June 9, 2026 at 7:09 pm

    You can maintain seperate adpater class so that you can achive this easily….

     public class bsAdapter extends BaseAdapter
     {
       Activity cntx;
       public bsAdapter(Activity context)
       {
         // TODO Auto-generated constructor stub
         this.cntx=context;
    
       }
    
       public int getCount()
       {
         // TODO Auto-generated method stub
         return listview_arr.length;
       }
    
      public Object getItem(int position)
      {
         // TODO Auto-generated method stub
         return listview_arr[position];
      }
    
      public long getItemId(int position)
      {
         // TODO Auto-generated method stub
         return listview_array.length;
      }
    
      public View getView(final int position, View convertView, ViewGroup parent)
      {
         View row=null;
    
        LayoutInflater inflater=cntx.getLayoutInflater();
        row=inflater.inflate(R.layout.search_list_item, null);
    
        TextView tv=(TextView)row.findViewById(R.id.title);
        CheckBox cb=(CheckBox)row.findViewById(R.id.cb01);
    
    
        tv.setText(listview_arr[position]);
    
        cb.setOnClickListener(new OnClickListener(){
    
        public void onClick(View v) {
    
        if(cb.ischecked)
        {
           //ur code
        }
    
        else //ur code
    
        }
      });
    
      return row;
      }
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a listview whose rows has one textview and a check box. I
I have a ListView where each item is a TextView . I want to
I have a ListView that is populated using an XML file. However, I want
I have a ListView, and I want that when I press an item, go
I have a ListView, and I want to customize the layout of each item
I have a listView, and ech item of listView has textView. I have set
i have this listview each listview item has a textview that have background ,
I have a listview ,each item is a textView with property autoLink=web|email.Link will work
I have a custom listview in which one textview and one checked box is
I have a custom adapter for my ListView which has multiple TextViews. I want

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.