Karthik
Karthik

Reputation: 5033

How to use a custom row instead of the android.R.layout.simple_list_item_multiple_choice in Multple Choice row in android?

I am working on this code but I have white background due to the android.R.layout.simple_list_item_multiple_choice I am unable to change the text color so that it will be ligible in the row.How to change it for custom row xml,here is my code.

    public class AndroidListViewActivity extends Activity 
{           
    ListView myList;     
    Button getChoice;           
    String[] listContent = {"January","February","March","April","May","June","July","August","September","October","November", "December"};      
    /** Called when the activity is first created. */   
    @Override     
    public void onCreate(Bundle savedInstanceState) 
    {      
        super.onCreate(savedInstanceState);     
        setContentView(R.layout.main);       
        myList = (ListView)findViewById(R.id.list);       
        getChoice = (Button)findViewById(R.id.getchoice);             
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice,listContent); 
        myList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);      
        myList.setAdapter(adapter);               
        getChoice.setOnClickListener(new Button.OnClickListener(){        
            @Override          
            public void onClick(View v) 
            {              
                // TODO Auto-generated method stub                     
                String selected = "";                                 
                int cntChoice = myList.getCount();              
                SparseBooleanArray sparseBooleanArray = myList.getCheckedItemPositions();        
                for(int i = 0; i < cntChoice; i++)
                {                   
                    if(sparseBooleanArray.get(i)) 
                    {                      
                        selected += myList.getItemAtPosition(i).toString() + "\n";                    
                        }               
                    }                           
                Toast.makeText(AndroidListViewActivity.this, selected,Toast.LENGTH_LONG).show();         
                }
            });               
        }



}

Please help me on this with an example or snippet,I am struck here for a long time.Thanks

Upvotes: 1

Views: 3959

Answers (2)

Richa
Richa

Reputation: 3193

You need to create custom layout for displaying list rows. Refer ListView Examples

Upvotes: 0

mxk
mxk

Reputation: 43544

As you can see, ArrayAdapter takes a layout resource ID as the first argument. That means you can pass any layout there, including your own. The solution therefore is to write your own layout and pass it here.

If you want to know how the stock one's implemented, have a look here.

Alternatively, you could retrieve a reference to the view and change its background color manually (or even via a style if all these items in your app share the same background color).

By the way, it looks like you're in a list based activity; why not inherit from ListActivity?

Upvotes: 1

Related Questions