【问题标题】:Android listview multiple choiceAndroid列表视图多选
【发布时间】:2014-03-11 22:14:40
【问题描述】:

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

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() 一个人在外面做什么。
  • 那是吐司消息..你能帮我如何将列表视图的选定项目存储在字符串数组中。
  • 不是将(String) lv.getAdapter().getItem(selectedItems.keyAt(i)); 分配给整个数组,而是将其分配给单个元素,例如getstring[i] = (String) lv.getAdapter().getItem(selectedItems.keyAt(i));
  • 还要注意,getstring 不是一个好的数组名称,它听起来像是一个方法名称。使用一些描述内容的名称,如roseArray、usedItems、newItems 等。
  • 我试过但得到空指针异常..

标签: android listview


【解决方案1】:

嘿,我已经使用 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 的代码 sn-p 中,我在这一行得到一个 Index Out Of Bound Exception

    如果 (a.valueAt(i))

    还有一些交错索引。

    我找到了this example,它完全符合您的要求。

    【讨论】:

      【解决方案3】:
      @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();
      }
      

      【讨论】:

        猜你喜欢
        • 2016-10-17
        • 1970-01-01
        • 2012-01-13
        • 1970-01-01
        • 1970-01-01
        • 2016-12-31
        • 1970-01-01
        相关资源
        最近更新 更多