-1

I have a listview with background image for each item. My question is how to select multiple items in the list and how to change their background. With this code i am able to select only 1 item at a time. Below is my xml for the listview.

main.xml

<ListView android:layout_height="350dp" android:id="@id/ListView01" android:layout_width="fill_parent"></ListView>

itemrow.xml

<?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="fill_parent" android:background="@layout/list_bg"
  android:orientation="vertical">

<TextView android:id="@+id/name"        
    android:textSize="16sp"   android:paddingLeft="25dp"       
    android:textStyle="bold"  android:text="hello"
    android:textColor="#FFFFFF" android:gravity="center_vertical"
    android:layout_width="fill_parent" android:layout_height="wrap_content"/>

</LinearLayout>

list_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_window_focused="false" android:state_selected="true" 
  android:drawable="@android:color/transparent" /> 
<item 
 android:state_selected="false"
    android:state_pressed="false" 
    android:drawable="@drawable/cellbgnew" />
<item android:state_pressed="true" 
    android:drawable="@drawable/cellbghover" />
<item android:state_selected="true"
 android:state_pressed="false" 
    android:drawable="@drawable/cellbghover" />
</selector>
Vamshi
  • 1,495
  • 1
  • 15
  • 31

5 Answers5

1

Its possible with ListView.CHOICE_MODE_MULTIPLE

please check the example available at below link, it may help you.

Android ListView Multiple Choice Example

New Answer:

How can you select multiple items in a ListView ? We can select only one item at a time. Its possible to check multiple items in a ListView with CHOICE_MODE_MULTIPLE, for same thing i given link to Example.

If you need to select multiple items in a ListView its not possible.

One more question also available with same requirement please check it., It may help you. Selecting multiple items in ListView

Community
  • 1
  • 1
Yugandhar Babu
  • 10,311
  • 9
  • 42
  • 67
1

Ok, find the below code for a sample activity.:

public class SampleActivity extends Activity {
    private String[] arrItems={"A", "B", "C", "D", "E"};
    private boolean[] arrState={false, false, false, false, false};
    private ListView lv;
    private ArrAdapter adapter;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        lv=(ListView) findViewById(R.id.listItems);
        adapter=new ArrAdapter(this);
        lv.setAdapter(adapter);
        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position,
                    long id) {
                // TODO Auto-generated method stub
                arrState[position]=!arrState[position];
                adapter.notifyDataSetChanged();
            }
        });
    }

    private class ArrAdapter extends ArrayAdapter<String>
    {
        Context mContext=null;
        public ArrAdapter(Context context) {
            super(context, R.layout.row);
            // TODO Auto-generated constructor stub
            mContext=context;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return arrItems.length;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            View vi=convertView;
            ViewHolder holder=null;
            if(vi==null)
            {
                vi=LayoutInflater.from(mContext).inflate(R.layout.row, parent, false);
                holder=new ViewHolder();
                holder.mTxt=(TextView) vi.findViewById(R.id.lbl);
                vi.setTag(holder);
            }
            else
                holder=(ViewHolder) vi.getTag();
            holder.mTxt.setText(arrItems[position]);
            if(arrState[position])
                vi.setBackgroundColor(Color.BLUE);
            else
                vi.setBackgroundColor(Color.WHITE);
            return super.getView(position, convertView, parent);
        }


        private class ViewHolder
        {
            TextView mTxt=null;
        }


    }

    private void clear()
    {
        Arrays.fill(arrState, false);
        adapter.notifyDataSetChanged();
    }
jeet
  • 29,001
  • 6
  • 52
  • 53
0

You can use ListView.setChoiceMode(ListView.MULTIPLE_CHOICE_MODE) to achieve this. or can create your own layout.

jeet
  • 29,001
  • 6
  • 52
  • 53
0

Please follow following strategy, simplest of all:

Create a boolean array having size of list. set initial values of all the elements of array to false. in GetView setBackground method pass background value according to value in array on that position. setItemClickListener on list and having body arr[position]=!arr[position], and reload list by calling adapter.notifyDatasetInvalidated() method.

jeet
  • 29,001
  • 6
  • 52
  • 53
  • I dont have a sample right now, but I think it is simple logic, and anyone can implement it in 5-10 mins. – jeet Jan 06 '12 at 04:54
  • I am new to android and i am not able to implement in 5 -10 mins. Can you please give 15 min time to solve this issue. – Vamshi Jan 06 '12 at 08:46
0

I dont think it is possible to me to write full code, but I am trying my best to solve your problem:

public class MainActivity extends Activity { void onCreate() { setContentView(R.layout.main);

} }

jeet
  • 29,001
  • 6
  • 52
  • 53
  • i am talking about displaying the listview with multi selection. Not to creating an activity, i know very well basics, see my xml also in my question – Vamshi Jan 06 '12 at 08:54