简体   繁体   中英

Android - Listview Selected Item

I made a listview and I wanted to make an user input a choice. How can I do this? Atm I only managed to create the ListView and display the data.

Is there an easy way to get the value of a selected item of the ListView and use it later? Something like a ListView RadioButton.

XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.tiagosilva.amob_android.TubeDataArchive" >

<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/lv_tubeData"
    android:choiceMode="singleChoice">
</ListView>

Listview:

public class TubeDataArchive extends Fragment {


public TubeDataArchive() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_tube_data_archive, container, false);

    ListView tubeDataList = (ListView) view.findViewById(R.id.lv_tubeData);

    //load tube data
    SharedPreferences settings = getActivity().getSharedPreferences("PREFS",0);
    String tubeDataString = settings.getString("tubeData", "");
    String[] tubeDataSplit = tubeDataString.split("\n");
    List<String> tubeDataItems = new ArrayList<>();

    for(int i=0; i<tubeDataSplit.length;i++)
    {
        tubeDataItems.add(tubeDataSplit[i]);
    }

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
            android.R.layout.simple_list_item_1, android.R.id.text1, tubeDataItems);

    // Assign adapter to ListView
    tubeDataList.setAdapter(adapter);

    return view;
}

 }

Is there an easy way to get the value of a selected item of the ListView and use it later?

Add onItemClickListener after the for loop.

tubeDataList.setOnItemClickListener(new OnItemClickListener() {
       @Override
         public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            // do whatever you want
          Log.d("############","Items " + tubeDataSplit[arg2] );
     }
 });

Something like a ListView RadioButton

If you want to have a listview with a radio button , then you need to create a custom listview layout.

Add ItemClickListener to your listview

tubeDataList.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {
        Toast.makeText(getapplicationcontext(), tubeDataItems.get(id), 
   Toast.LENGTH_LONG).show();
    }

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM