安卓ListView多选

3

如何获取多选列表视图数据。我有一个带有多项选择的列表视图,我想将所选项目存储在字符串数组中。有人能指导我如何将列表视图中所选的项目存储在字符串数组中吗。

SparseBooleanArray selectedItems = lv.getCheckedItemPositions();          
int id1 = lv.getCheckedItemPosition();        
Toast.makeText(getApplicationContext(), "" + id1, Toast.LENGTH_SHORT).show();

for (int i = 0; i < lv_arr.length; i++) {
    if (selectedItems.get(i)) {
        String[] getstring = (String) lv.getAdapter().getItem(
            selectedItems.keyAt(i));
        System.out.println(""+getstring));
    }
}

我只能编辑这么多...不确定show()在那里孤零零地挂着是在干什么。 - Brian Roach
那是一个提示信息...你能帮我学习如何将列表视图中选定的项目存储在字符串数组中吗? - RAAAAM
不要将 (String) lv.getAdapter().getItem(selectedItems.keyAt(i)); 分配给整个数组,而是将其分配给单个元素,例如 getstring[i] = (String) lv.getAdapter().getItem(selectedItems.keyAt(i)); - Mudassir
请注意,getstring不是一个好的数组名称,它听起来像一个方法名。使用一些描述其内容的名称,如roseArray、usedItems、newItems等。 - Mudassir
我尝试了但是出现了空指针异常。 - RAAAAM
3个回答

9

嘿,我使用String来保存列表中所有选中的项目。请参见下面的代码:

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;



public class ViewsActivity extends Activity 
{

    private ListView lView;
    private String lv_items[] = { "Android", "iPhone", "BlackBerry",
            "AndroidPeople", "J2ME", "Listview", "ArrayAdapter", "ListItem",
            "Us", "UK", "India" };
    private String my_sel_items;

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);
        my_sel_items=new String();

        lView = (ListView) findViewById(R.id.ListView01);

        lView.setAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_multiple_choice, lv_items));
        lView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

        lView.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView arg0, View arg1, int arg2,long arg3)
            {
                //List list = new ArrayList();
                my_sel_items=new String("Selected Items");
                SparseBooleanArray a = lView.getCheckedItemPositions();

                for(int i = 0; i < lv_items.length ; i++)
                {
                    if (a.valueAt(i))
                    {
                     /*
                        Long val = lView.getAdapter().getItemId(a.keyAt(i));
                        Log.v("MyData", "index=" + val.toString()
                             + "item value="+lView.getAdapter().getItem(i));
                        list.add(lView.getAdapter().getItemId((a.keyAt(i))));
                     */

                        my_sel_items = my_sel_items + "," 
                            + (String) lView.getAdapter().getItem(i);
                    }
                }
                Log.v("values",my_sel_items);
            }
        });
    }
}

2
在@Kartik的代码片段中,我在这一行得到了一个“数组下标越界异常”:

if (a.valueAt(i))

还有一些错位的索引。
我找到了这个例子,它完全符合你的要求。

0
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    mSelectedItems = new ArrayList();  // Where we track the selected items
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    // Set the dialog title
    builder.setTitle(R.string.pick_toppings)
    // Specify the list array, the items to be selected by default (null for none),
    // and the listener through which to receive callbacks when items are selected
           .setMultiChoiceItems(R.array.toppings, null,
                      new DialogInterface.OnMultiChoiceClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int which,
                       boolean isChecked) {
                   if (isChecked) {
                       // If the user checked the item, add it to the selected it@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    mSelectedItems = new ArrayList();  // Where we track the selected items
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    // Set the dialog title
    builder.setTitle(R.string.pick_toppings)
    // Specify the list array, the items to be selected by default (null for none),
    // and the listener through which to receive callbacks when items are selected
           .setMultiChoiceItems(R.array.toppings, null,
                      new DialogInterface.OnMultiChoiceClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int which,
                       boolean isChecked) {
                   if (isChecked) {
                       // If the user checked the item, add it to the selected ems
                       mSelectedItems.add(which);
                   } else if (mSelectedItems.contains(which)) {
                       // Else, if the item is already in the array, remove it 
                       mSelectedItems.remove(Integer.valueOf(which));
                   }
               }
           })
    // Set the action buttons
           .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int id) {
                   // User clicked OK, so save the mSelectedItems results somewhere
                   // or return them to the component that opened the dialog
                   ...
               }
           })
           .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int id) {
                   ...
               }
           });

    return builder.create();
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接