neha
neha

Reputation: 117

Android ListView with radio button

> I want to change the selected item's checkbox color of listview so that when user selects the item its color gets changed this is my code :

final ListView listView = (ListView)findViewById(R.id.lvcancelorder);                
//this is my listview
 ArrayAdapter<String> adapter =     
new ArrayAdapter<String(this,android.R.layout.simple_list_item_single_choice, countries); //this is the adapter

listView.setAdapter(adapter);
 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {  
 public void onItemClick(AdapterView<?> parent, View view, int position, long id) //this is the item click event
 {    
 selectedFromList = (listView.getItemAtPosition(position)).toString();   
    }});  //this is the selected item from the listview

How can I do this ? Please suggest something. I know to do this thing List view custom adapter but have no idea to do it.

Upvotes: 4

Views: 748

Answers (2)

Najeeb Idrees
Najeeb Idrees

Reputation: 453

It is recommended to create your own custom adapter extends from BaseAdapter or ArrayAdapter and on selection of item simply change background color of root layout view.

use following link to get know about how to create a listview with checkboxes. see here

Upvotes: 2

Burhanuddin Rashid
Burhanuddin Rashid

Reputation: 5370

You can try this:

//Your button to get selected list
getChoice = (Button)findViewById(R.id.getchoice);

        ArrayAdapter<string> adapter = new ArrayAdapter<string>(this, android.R.layout.simple_list_item_multiple_choice, countries);
        myList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

        myList.setAdapter(adapter);
        getChoice.setOnClickListener(new Button.OnClickListener(){


            @Override
            public void onClick(View v) {

                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(MainActivity.this, selected, Toast.LENGTH_LONG).show();

            }});

Upvotes: 1

Related Questions