我试图从我的列表视图中获取视图的位置,但SparseBooleanArray的大小始终为0.
这是我活动的代码。
IngredientIndivAdapter adapter;
ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_screen);
Intent intent = getIntent();
String prev_but = intent.getStringExtra("hi");
ArrayList<String> list1 = new ArrayList<String>();
list1.add("a");
list1.add("b");
list1.add("c");
list1.add("d");
list1.add("e");
adapter = new IngredientIndivAdapter(this,list1);
lv = (ListView)findViewById(R.id.listview1);
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
lv.setAdapter(adapter);
}
public void remove_ingredient_button(View v){
SparseBooleanArray remove_list = lv.getCheckedItemPositions();
Log.d("poo",""+remove_list.size());
if(remove_list!=null)
for(int i = 0; i < remove_list.size(); i++){
if(remove_list.valueAt(i)){
View remove_view = (View)findViewById(remove_list.keyAt(i));
((LinearLayout)remove_view.getParent()).removeView(remove_view);
}
}
adapter.notifyDataSetChanged();
}
这是该活动的XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.JTA.recipeshoppinglist.ListScreen">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<ListView
android:id="@+id/listview1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
></ListView>
</LinearLayout>
<Button
android:id="@+id/add_b"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="#FFFF9900"
android:text="+"
android:onClick="add_button" />
<Button
android:id="@+id/remove_item"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:text="-"
android:onClick="remove_ingredient_button" />
这是我的自定义行XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<CheckBox
android:id="@+id/ingredient_check"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:checkable="true"
android:background="#FF0000" />
</LinearLayout>
这是我的自定义数组适配器。
public class IngredientIndivAdapter extends ArrayAdapter<String> {
static class ViewHolder{
CheckBox text;
}
ArrayList<String> list = new ArrayList<String>();
Context context;
ViewHolder viewHolder;
public IngredientIndivAdapter(Context context, ArrayList<String> iList){
super(context,R.layout.ingredient_row,iList);
this.list=iList;
this.context=context;
}
public View getView(int position, View convertView, ViewGroup parent){
if(convertView == null){
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
convertView = inflater.inflate(R.layout.ingredient_row, parent, false);
viewHolder = new ViewHolder();
viewHolder.text = (CheckBox) convertView.findViewById(R.id.ingredient_check);
convertView.setTag(viewHolder);
}
else{
viewHolder = (ViewHolder)convertView.getTag();
}
viewHolder.text.setText(list.get(position));
return convertView;
}
public boolean hasStableIds(){
return true;
}
}
答案 0 :(得分:0)
SparseBooleanArray checked = listView.getCheckedItemPositions();
for (int i = 0; i < listview.getCount(); i++){
if(checked.get(i)==true) {
// do something
}
}
我希望它能奏效。