0

I am developing an App for cricket. In that my aim is to select players for the particular team in ListView. Here I can able to select multiple players from the list. I am using simple Adapter with multiple choice Listview.

          adapter=new ArrayAdapter<String>(this,R.layout.custom_list_view,R.id.textView_color,playersName);
    lvview.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

And I am using checkedTextView for multiple selection. Below is my custom_list_view with CheckedTextView

       <CheckedTextView
   android:id="@+id/textView_color"
   android:layout_width="fill_parent"
   android:layout_height="?android:attr/listPreferredItemHeight"
   android:textAppearance="?android:attr/textAppearanceLarge"
   android:gravity="center_vertical"
   android:checkMark="?android:attr/listChoiceIndicatorMultiple"
   android:paddingLeft="6dip"
   android:paddingRight="6dip"
   android:textColor="#FFffFF"
   />

And now my problem is I want to change the color of listview when user select the particular player from the list. Its like to show the user which players are selected. To Differentiate from the unselected player I am changing the color of selected player to red.

    lvview.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View v, int position,long id) {
            // TODO Auto-generated method stub
            SparseBooleanArray checked = lvview.getCheckedItemPositions();      

            if(checked.get(position))
            {
                //chkTextView.setTextColor(Color.GREEN);

                counter_selected++;
                selectedCounterText.setText("" + counter_selected);                 
            }
            else
            {
                counter_selected--;
                selectedCounterText.setText("" + counter_selected);
            }
        }
    }); 

How to change the color of selected player from default color to Red. I am struggling to do that.. Please help me to find it out..

user1835052
  • 455
  • 1
  • 5
  • 11

4 Answers4

2

Got Answer by using the below code

In mainActivity adapter class

   adapter=new ArrayAdapter<String>(getApplicationContext(), R.layout.text_view,R.id.textView1,players);

    lvview.setAdapter(adapter);

main.xml look like below

<ListView
    android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="8.5"
    android:cacheColorHint="#00000000" 
     />

And my custom layout field is like below

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="23dp"
    android:text="TextView"
    android:textColor="#ffffff" />

<CheckBox
    android:id="@+id/checkBox1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/textView1"
    android:layout_alignBottom="@+id/textView1"
    android:layout_alignParentRight="true"
    android:focusable="false"
  android:focusableInTouchMode="false"
    />

And in MainActivity onitem click listener for ListView I called the custom layout view and the code is given below

            lvview.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> l, View v, int position,
                long id) {
            // TODO Auto-generated method stub
            SparseBooleanArray checked = lvview.getCheckedItemPositions();  
            checkedText=(CheckBox) v.findViewById(R.id.checkBox1);
            checkedList=(TextView) v.findViewById(R.id.textView1);
            if(checkedText.isChecked()==false)
            {
                counter_selected++;
                checkedText.setChecked(true);
                checkedList.setTextColor(Color.RED);
                selectedCounterText.setText("" + counter_selected);                 

            }
            else
            {
                counter_selected--;
                checkedText.setChecked(false);
                 checkedList.setTextColor(Color.WHITE);
                 selectedCounterText.setText("" + counter_selected);        
            }
        }
    });

And it solved my problem..

user1835052
  • 455
  • 1
  • 5
  • 11
1

you have to use statelist drawable to define background on events, and for easiness Here is the very similar question, that might help you. listview

Community
  • 1
  • 1
hemantsb
  • 2,049
  • 2
  • 20
  • 29
0

First , You can define a selector xml as follows

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@color/red" />
    <item android:drawable="@drawable/listitem_normal" />
</selector>

Then you can set the Selector for the ListItem using the android:listSelector="@drawable/listitem_selector" as follows :

<ListView 
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:listSelector="@drawable/listitem_selector"
   />
0

The problem is it is not possible to do it via simple adapter (which you have used).

You need to define custom row items. You need to define custom XML layouts for each list item which gets bound to the listview. Thereby, you need to make a customAdapter which has a getView() method. In it you can easily fetch the child TextView of the row and change its Properties.

Follow this tutorial.

halfer
  • 19,824
  • 17
  • 99
  • 186
Nezam
  • 4,122
  • 3
  • 32
  • 49
  • I know it is possible to do it in customAdapter. But I want to pass the selected items to another listview when user click add button... Then how I will do it in custom adapter – user1835052 Apr 04 '13 at 05:45
  • Thats a broad question but i can give you an idea.You just need to save a global variable in the `Activity` which gets overriden in the customAdapter code and then you fetch it in the back in the `Activity` eventListener See this answer:http://stackoverflow.com/a/8726923/1221203 Try some code and when you have encounter a problem ask a question with code you tried. If this solves your posted question mark it as an answer. – Nezam Apr 04 '13 at 05:49
  • got answer for my problem... c my answer below.. – user1835052 Apr 04 '13 at 09:50