Listview with radiobutton to use of single selection in listview item.you can not multiple item to selected.it can use single selection and change your radio button color in selection mode.when you can select item to display message show to fire click event in list item to perticular selection position to get item.
Listview to use of custom layout or build in layout.custom layout to use of design in xml file layout and build in layout to already desing available. In this example to use of build in layout of single selection item layout.
MainActivity.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <ListView android:id="@+id/list_view" android:layout_width="match_parent" android:layout_height="match_parent"></ListView> </RelativeLayout>
MainActivity.java
package com.example.bhaumik.listviewradiobutton; import android.graphics.Color; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.CheckedTextView; import android.widget.ListView; import android.widget.Toast; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { ListView listView; ArrayList<String> list; ArrayAdapter adapter; String[] version = {"Aestro","Blender","CupCake","Donut","Eclair","Froyo","GingerBread","HoneyComb","IceCream Sandwich", "Jelly Bean","Kitkat","Lolipop","Marshmallow","Nought","Oreo"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = findViewById(R.id.list_view); list = new ArrayList<>(); for (int i = 0;i<version.length;i++){ list.add(version[i]); } listView.setChoiceMode(listView.CHOICE_MODE_SINGLE); adapter = new ArrayAdapter(MainActivity.this,android.R.layout.simple_list_item_single_choice,list); listView.setAdapter(adapter); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { Toast.makeText(MainActivity.this, "Selected -> " + version[i], Toast.LENGTH_SHORT).show(); } }); } }
0 comments:
Post a Comment