我创建了一个带复选框的CustomListView。如何创建代码,以便我可以在代码中使用ListView的选定项。此代码已被建议: 主要活动代码:
ArrayList<PeopleDetails> people_selected = new ArrayList<PeopleDetails>();
peoplelist_customadapter adapter = (peoplelist_customadapter) people_list.getAdapter();
StringBuilder test_builder = new StringBuilder();
for(int i=0; i < adapter.checkstates.size() ; i++){
if(adapter.checkstates.get(i) == true){
people_selected.add(people_details.get(i));///lines throws error java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
///so people_details is empty - 0 entries. why???
test_builder.append(people_details.get(i).name);
}
适配器:
package com.example.partyorganiser;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
public class peoplelist_customadapter extends ArrayAdapter<PeopleDetails> implements CompoundButton.OnCheckedChangeListener{
Context context;
int layoutResourceId;
ArrayList<PeopleDetails> data = null;
SparseBooleanArray checkstates;
public peoplelist_customadapter(Context context, int layoutResourceId, ArrayList<PeopleDetails> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
checkstates = new SparseBooleanArray(data.toArray().length);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
personHolder holder = null;
PeopleDetails[] people_array = data.toArray(new PeopleDetails[data.size()]);
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new personHolder();
holder.txtName = (TextView)row.findViewById(R.id.name_txt);
holder.txtNumber = (TextView)row.findViewById(R.id.number_txt);
holder.checkbox = (CheckBox) row.findViewById(R.id.checkBox);
///holder.chkSelect.setTag(position);
///holder.chkSelect.setChecked(mCheckStates.get(position, false));
///holder.chkSelect.setOnCheckedChangeListener(this);
holder.checkbox.setTag(position);
holder.checkbox.setOnCheckedChangeListener((new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
}
}));
row.setTag(holder);
}
else
{
holder = (personHolder)row.getTag();
}
PeopleDetails person = people_array[position];
holder.txtName.setText(person.name);
holder.txtNumber.setText(person.number);
holder.checkbox.setChecked(checkstates.get(position, false));
holder.checkbox.setOnCheckedChangeListener(this);
return row;
}
public boolean isChecked(int position) {
return checkstates.get(position, false);
}
public void setChecked(int position, boolean isChecked) {
checkstates.put(position, isChecked);
}
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
checkstates.put((Integer) buttonView.getTag(), isChecked);
}
static class personHolder
{
TextView txtName;
TextView txtNumber;
CheckBox checkbox;
public boolean isChecked(){
boolean isChecked_boolean = false; ///this holds the return value, of whether the checkbox is checked or not.
if(checkbox.isChecked()){
isChecked_boolean = true;
}
else{
isChecked_boolean = false;
}
return isChecked_boolean;
}
}
}
}
但是没有用,因为文件应该被写入时为空。这段代码有错误吗?有没有更好的方法来实现这个目标?
感谢您的帮助!!