gpt4 book ai didi

Android listview多选

转载 作者:太空宇宙 更新时间:2023-11-03 11:45:51 25 4
gpt4 key购买 nike

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

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));
}
}

最佳答案

嘿,我已经使用 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);
}
});
}
}

关于Android listview多选,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5458989/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com