Android ListView with radio button

> I want to change the selected item 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 in the form of a list of custom adapters, but I have no idea to do this.

+4
source share
2 answers

It is recommended that you create your own custom adapter that extends from BaseAdapter or ArrayAdapter, and when you select an item, just change the background color of the root layout.

Use the following link to learn how to create a checklist. see here

+2
source

You can try the following:

//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();

            }});
+1
source

Source: https://habr.com/ru/post/1660893/


All Articles