0

anyone please tell me, how can i get selected from listview.? my single item row from listview contains a text followed by a radiobutton.

Code: 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:orientation="vertical" >

    <ListView
        android:id="@+id/listview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:choiceMode="singleChoice"
    />

</LinearLayout>

and activity:

public class MainActivity extends Activity {
    String[] countries = new String[] {
        "India",
        "Pakistan",
        "Sri Lanka",
        "China",
        "Bangladesh",
        "Nepal",
        "Afghanistan",
        "North Korea",
        "South Korea",
        "Japan"
    };

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // Getting object reference to listview of main.xml
        ListView listView = (ListView) findViewById(R.id.listview);

        // Instantiating array adapter to populate the listView
        // The layout android.R.layout.simple_list_item_single_choice creates radio button for each listview item
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice,countries);

        listView.setAdapter(adapter);

    }
}

the code working fine..
but my questions are
1) how to set default selection ?
2) get selected item ?

varsha valanju
  • 801
  • 1
  • 9
  • 27

2 Answers2

1

I got my desired output by this way:

answer for 1)how to set default selection:

int defaultselected=0,idnoreid=0;
listView.setItemChecked(defaultselected, true);
listView.performItemClick(listView.getSelectedView(), defaultselected, idnoreid);

answer for 2)get selected item:

    @Override
    public void onItemClick(AdapterView<?> adapterView, View v, int i, long l)   
    {  
       newLabel = ((TextView)v).getText().toString();  
    }
varsha valanju
  • 801
  • 1
  • 9
  • 27
0

1) You can set default using setSelection. For example in your code, put it after you set the adapter.

listView.setAdapter(adapter);
listview.setSelection(0); //default is India

2) You can get your selected country from listview using getSelectedItem(). For example in your code if you have save method then

private void save() {
    String selectedCountry = (String) listview.getSelectedItem();
    //TODO
}
Randyka Yudhistira
  • 3,612
  • 1
  • 26
  • 41
  • in the documentation, it says "Sets the currently selected item. If in touch mode, the item will not be selected but it will still be positioned appropriately. If the specified selection position is less than 0, then the item at position 0 will be selected.", so you cannot see in your screen that this item is selected, but in your save method you will get your default item. – Randyka Yudhistira Oct 27 '16 at 02:29