Zach M.
Zach M.

Reputation: 1194

Android: Saving items selected in a multiple choice list view

I am trying to understand where to save the selected list view items from this code. With a dialog box you have an "ok/cancel" button option, is this possible with lists? Ideally I will be storing data from four different lists into a database on submit. In the below picture I would like to save the first three items into a database, or even an array just to start.

package com.example.lifebyfourlists;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends ListActivity{
    String [] seven = { 
            "Dark Leafy Greens" , 
            "Nuts", 
            "Carrots",
            "Green Tea", 
            "Whole Grains", 
            "Fruits"};

    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        ListView lstView = getListView();
        lstView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        lstView.setTextFilterEnabled(true);
        setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_checked, seven));
    }

    public void onListItemClick(ListView parent, View v, int position, long id){
        Toast.makeText(this, "You have selected " + seven[position], Toast.LENGTH_SHORT).show();
    }
}

Screen shot

Upvotes: 0

Views: 3198

Answers (1)

Sam
Sam

Reputation: 86948

I am trying to understand where to save the selected list view items from this code. With a dialog box you have an "ok/cancel" button option, is this possible with lists?

You can use getCheckedItemIds() or you can write a custom Adapter to track the selected rows.

Upvotes: 1

Related Questions