我已经在网上关注了很多教程,但最后我无法得到我想要的东西。在我的所有研究之后,我们终于找到了更好的建议和答案。
我有一个带复选框的列表视图,我需要获取listview的所有选中项目。
这是我的布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/lineItem"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<CheckBox android:id="@+id/chklist"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:ems="10"
android:padding="10dp"
android:textColor="#800080"
android:textSize="16sp"/>
</LinearLayout>
我正在使用自定义适配器,并且我收到了所显示的已检查项目:
holder.chk.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String listItemText = holder.txtName.getText()
.toString();
if (holder.chk.isChecked())
Toast.makeText(getApplicationContext(),
"Checked" + " " + listItemText,
Toast.LENGTH_SHORT).show();
else
Toast.makeText(getApplicationContext(),
"Unchecked" + " " + listItemText,
Toast.LENGTH_SHORT).show();
}
});
如何从ListView OnItemClick Listener中获取所有选中的项目:
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent,
View view, int position, long id) {
//Here I need to get all the checked items
}
});
自定义适配器:
public class MyListAdapter extends BaseAdapter {
public LayoutInflater inflater;
public Activity context;
ArrayList<String> arr_calllog_name = new ArrayList<String>();
boolean[] itemChecked;
public MyListAdapter(Activity context,
ArrayList<String> arr_calllog_name) {
super();
this.context = context;
this.arr_calllog_name = arr_calllog_name;
this.inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
itemChecked = new boolean[arr_calllog_name.size()];
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return arr_calllog_name.size();
}
@Override
public Object getItem(int position) {
return arr_calllog_name.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public class ViewHolder {
CheckBox chk;
ImageView editimage, delimage;
TextView txtName;
LinearLayout row;
}
@Override
public View getView(final int position, View convertView,
ViewGroup parent) {
// TODO Auto-generated method stub
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.list, null);
holder.chk = (CheckBox) convertView
.findViewById(R.id.chklist);
holder.txtName = (TextView) convertView
.findViewById(R.id.textView);
holder.row = (LinearLayout) convertView
.findViewById(R.id.lineItem);
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();
holder.txtName.setText(arr_calllog_name.get(position));
holder.row.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/*
* Intent i = new Intent(AddItems.this, AddItems.class);
* Bundle bundle = new Bundle(); // Add your data from
* getFactualResults method // to bundle String listItemText
* = holder.txtName.getText().toString();
*
* bundle.putString("Listname", listItemText);
* i.putStringArrayListExtra("AddedList", MyList); // Add
* the bundle to the intent i.putExtras(bundle);
* startActivity(i);
*/
}
});
holder.chk.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String listItemText = holder.txtName.getText()
.toString();
if (holder.chk.isChecked())
Toast.makeText(getApplicationContext(),
"Checked" + " " + listItemText,
Toast.LENGTH_SHORT).show();
else
Toast.makeText(getApplicationContext(),
"Unchecked" + " " + listItemText,
Toast.LENGTH_SHORT).show();
}
});
return convertView;
}
}
答案 0 :(得分:1)
您的适配器包含列表中的所有项目。在您的情况下,此类项的一个实例应至少包含String(对于textView)和Bool(对于CheckBox)。每当您想要所有复选框的列表时,只需遍历列表项并创建具有真实布尔值的所有项的临时列表。
List<YourListItemObject> checkedItems = new ArrayList<YourListItemObject>();
for (YourListItemObject listItem : adapter.getItems()) {
if (listItem.getBool()) {
checkedItems.Add(listItem);
}
}
答案 1 :(得分:-1)
尝试使用此功能进行多项选择..
<ListView
android:id="@+id/myListView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:choiceMode="multipleChoice"
/>
请参阅此链接http://adanware.blogspot.in/2012/04/android-multiple-selection-listview.html