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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T08:11:18+00:00 2026-05-28T08:11:18+00:00

I’ve been searching for a solution to this issue for a few days now.

  • 0

I’ve been searching for a solution to this issue for a few days now.

I’m trying to create a custom ListView list item that can be selectable for mass editing. This would be something similar to the Gmail application that lists the email in your inbox and allows you to select a set of them for an action (delete, move, etc.).

My requirements are:

  • Must have a customized look. The android.R.layout.simple_list_item_[checked|multiple_choice] layouts are not suited to what i’m looking for
  • I would like it to work with the given ListView#setChoiceMode and ListView#getCheckedItemPositions. Neither is the CheckedTextView. (An alternative approach to this would be fine)
  • I would like to make the checkboxes appear when user selects an “edit mode”
  • I would like to modify the menu options available similar to Android’s Selection Mode

I’ve tried to add my own checkbox to my view and set an OnClickListener for it to mark the list items as selected/unselected, but that is not quite working for me.

Any help is appreciated; even if it only gets me 90% of the way there.

Please keep in mind that I’m trying to get this to work on Android versions 2.1 and up.

Thank you

  • 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-28T08:11:19+00:00Added an answer on May 28, 2026 at 8:11 am

    So I believe I have resolved the main problem I was facing.

    I believe the problem I was having was that my view was not representing the state of the ListView. In lamens; I thought a checkbox was needed to set whether a list item was checked or not (this is the web developer in me). However, the ListView maintains its own state of whats checked and whats not, regardless if you have a view (CheckBox) to represent that or not.

    layout/list_item.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    
        <ProgressBar
            android:id="@+id/progressBar1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dip" />
    
        <LinearLayout
            android:id="@+id/linearLayout2"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical" >
    
            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Line 1"
                android:textAppearance="?android:attr/textAppearanceMedium" />
    
            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Line 2" />
        </LinearLayout>
    
        <CheckBox
            android:id="@+id/checkBox1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:clickable="false"
            android:focusable="false"
            android:visibility="gone" />
    
    </LinearLayout>
    

    menu/menu.xml

    <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android" >
    
        <item
            android:id="@+id/edit_menu_item"
            android:title="KAMEHAMEHA!!!!!"/>
    
    </menu>
    

    menu/edit.xml

    <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android" >
    
        <item
            android:id="@+id/commit_menu_item"
            android:title="Commit"/>
        <item
            android:id="@+id/cancel_menu_item"
            android:title="Cancel"/>
    
    </menu>
    

    ListViewTestActivity.java

    package com.loesak.listviewtest;
    
    import android.app.ListActivity;
    import android.content.Context;
    import android.os.Bundle;
    import android.util.Log;
    import android.util.SparseBooleanArray;
    import android.view.LayoutInflater;
    import android.view.Menu;
    import android.view.MenuInflater;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.CheckBox;
    import android.widget.ListView;
    import android.widget.TextView;
    import android.widget.Toast;
    
    public class ListViewTestActivity extends ListActivity {
        private static final String[] GENRES = new String[] {
            "Action", "Adventure", "Animation", "Children", "Comedy", "Documentary", "Drama",
            "Foreign", "History", "Independent", "Romance", "Sci-Fi", "Television", "Thriller"
        };
    
        private ListView listView = null;
        private MyArrayAdapter myArrayAdapter = null;
        private boolean editMode = false;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            this.listView = getListView();
            this.myArrayAdapter = new MyArrayAdapter(this, GENRES);
            this.setListAdapter(this.myArrayAdapter);
        }
    
        @Override
        public boolean onPrepareOptionsMenu(Menu menu) {
            menu.clear();
    
            MenuInflater inflater = this.getMenuInflater();
    
            if(!this.editMode) {
                inflater.inflate(R.menu.menu, menu);
            } else {
                inflater.inflate(R.menu.edit, menu);
            }
    
            return super.onPrepareOptionsMenu(menu);
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            if(item.getItemId() == R.id.edit_menu_item) {
                Toast.makeText(this, "entering edit mode", Toast.LENGTH_SHORT).show();
    
                this.editMode = true;
                this.listView.setItemsCanFocus(false);
                this.listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    
                this.myArrayAdapter.setEditMode(editMode);
            } else if(item.getItemId() == R.id.commit_menu_item 
                    || item.getItemId() == R.id.cancel_menu_item) {
                if(item.getItemId() == R.id.commit_menu_item) {
                    Toast.makeText(this, "committing changes", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(this, "leaving edit mode", Toast.LENGTH_SHORT).show();
                }
    
                this.editMode = false;
                this.listView.setItemsCanFocus(true);
                this.listView.setChoiceMode(ListView.CHOICE_MODE_NONE);
                this.listView.clearChoices();
    
                this.myArrayAdapter.setEditMode(editMode);
            }
    
            return super.onOptionsItemSelected(item);
        }
    
        @Override
        protected void onListItemClick(ListView l, View v, int position, long id) {
            if(!this.editMode) {
                Toast.makeText(this, "list item selected at position " + position, Toast.LENGTH_SHORT).show();
            } else {
                String str = "";
                SparseBooleanArray wtfit = l.getCheckedItemPositions();
                for(int i = 0; i < GENRES.length; i++) {
                    if(wtfit.get(i)) {
                        str += i + ", ";
                    }
                }
    
                Toast.makeText(this, "Selected item positions: " + str, Toast.LENGTH_SHORT).show();
            }
    
            super.onListItemClick(l, v, position, id);
        }
    
        class MyArrayAdapter extends ArrayAdapter<String> {
            private Context context;
            private String[] entires;
    
            private boolean editMode = false;
    
            public MyArrayAdapter(Context context, String[] entries) {
                super(context, R.layout.list_item, entries);
    
                this.context = context;
                this.entires = entries;
            }
    
            @Override
            public View getView(final int position, View view, ViewGroup parent) {
                final ListView listView = (ListView) parent;
                final ViewHolder viewHolder;
    
                if(view == null) {
                    LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    
                    view = layoutInflater.inflate(R.layout.list_item, null, true);
    
                    viewHolder = new ViewHolder();
                    viewHolder.text1 = (TextView) view.findViewById(R.id.textView1);
                    viewHolder.text2 = (TextView) view.findViewById(R.id.textView2);
                    viewHolder.checkbox1 = (CheckBox) view.findViewById(R.id.checkBox1);
    
                    view.setTag(viewHolder);
                } else {
                    viewHolder = (ViewHolder) view.getTag();
                }
    
                viewHolder.text1.setText(this.entires[position] + "_1");
                viewHolder.text2.setText(this.entires[position] + "_2");
                viewHolder.checkbox1.setChecked(listView.isItemChecked(position));
    
                if(this.editMode) {
                    viewHolder.checkbox1.setVisibility(View.VISIBLE);
                } else {
                    viewHolder.checkbox1.setVisibility(View.GONE);
                }
    
                return view;
            }
    
            public void setEditMode(boolean editMode) {
                this.editMode = editMode;
                this.notifyDataSetChanged();
            }
    
            class ViewHolder {
                TextView text1;
                TextView text2;
                CheckBox checkbox1;
            }
        }
    }
    

    To have the checkbox reflect the actual selected mode of the list item:

    viewHolder.checkbox1.setChecked(listView.isItemChecked(position));
    

    That’s it. Kind of obvious once you know how it all works. Too bad it took me this long to figure out. Sometimes I feel the android programming is a bit too complex at times.

    The only problem I have now is that the ListView items are always redrawn when a list item is clicked. This is wreaking havoc on my list items which contain asynchronously loaded images from file. Hopefully when I beef up the performance on that portion of my code, this will disappear.

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

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.