我正在创建一个自定义listview
,其中我给出了一个复选框。我创建了一个扩展适配器的自定义适配器类。我将这些复选框的值存储在适配器类的数组中。arraylist 的名称是 itemname(您可以在 customadapter 类的底部看到它)。我想将此数组列表发送到一个活动中(为了将这些值存储到数据库中)。由于我正在扩展数组适配器类,因此我不能使用sharedpreferences
. 能解决这个问题吗?我的适配器类如下。
public class CustomArrayAdapter extends ArrayAdapter<Model> {
protected static final String MODE_PRIVATE = null;
private final List<Model> list;
private final Activity context;
public CustomArrayAdapter(Activity context, List<Model> list) {
super(context, R.layout.customlistlayout, list);
this.context = context;
this.list = list;
}
static class ViewHolder {
protected TextView mText;
protected CheckBox mCheckbox;
public string getItemAtPosition(int i) {
// TODO Auto-generated method stub
return null;
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView == null) {
final ArrayList<String> itemname = new ArrayList<String>();
LayoutInflater inflator = context.getLayoutInflater();
view = inflator.inflate(R.layout.customlistlayout, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.mText = (TextView) view.findViewById(R.id.settings_label);
viewHolder.mCheckbox = (CheckBox) view.findViewById(R.id.check);
Activity activity = new SettingsActivity();
viewHolder.mCheckbox.setOnCheckedChangeListener(activity);
viewHolder.mCheckbox
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
Model element = (Model) viewHolder.mCheckbox
.getTag();
element.setSelected(buttonView.isChecked());
**itemname.add(element.getName());**
}
});
for (String myValue : itemname) {
System.out.println(myValue);
}
view.setTag(viewHolder);
viewHolder.mCheckbox.setTag(list.get(position));
} else {
view = convertView;
((ViewHolder) view.getTag()).mCheckbox.setTag(list.get(position));
}
ViewHolder holder = (ViewHolder) view.getTag();
holder.mText.setText(list.get(position).getName());
holder.mCheckbox.setChecked(list.get(position).isSelected());
return view;
}
}
我从一个活动中调用这个适配器类如下
ArrayAdapter<Model> settingsadapter = new CustomArrayAdapter(this, getModel()); setListAdapter(settingsadapter);