我试图借助意图将列表视图中存在的复选框的位置传递给另一个活动。但它显示我在另一项活动中为空。或者有人可以告诉我如何使用下面的代码将选定的复选框数据传递给另一个活动?
我想从相应复选框的列表视图中将值存储在一个数组中。然后转移到另一个活动
public class MyAdapter extends ArrayAdapter<Model> {
int getPosition;
Intent in;
private final List<Model> list;
public final Activity context;
boolean checkAll_flag = false;
boolean checkItem_flag = false;
public MyAdapter(Activity context, List<Model> list) {
super(context, R.layout.row, list);
this.context = context;
this.list = list;
}
static class ViewHolder {
protected TextView text;
protected CheckBox checkbox;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
convertView = inflator.inflate(R.layout.row, null);
viewHolder = new ViewHolder();
viewHolder.text = (TextView) convertView.findViewById(R.id.label);
viewHolder.checkbox = (CheckBox) convertView
.findViewById(R.id.check);
viewHolder.checkbox
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
getPosition = (Integer) buttonView.getTag(); // setTag.
}
});
convertView.setTag(viewHolder);
convertView.setOnClickListener(new MyCustomItemClickListener(getPosition));
convertView.setTag(R.id.label, viewHolder.text);
convertView.setTag(R.id.check, viewHolder.checkbox);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.checkbox.setTag(position); // This line is important.
viewHolder.text.setText(list.get(position).getName());
viewHolder.checkbox.setChecked(list.get(position).isSelected());
return convertView;
}
class MyCustomItemClickListener implements OnClickListener {
MyCustomItemClickListener(int getPositionx) {
/*
* Whatever initialization you need can be done here. You can pass
* values in constructor when calling it from getview and use with
* intent extras
*/
in = new Intent(context, YourActivity.class);
in.putExtra("aa", getPositionx);
}
@Override
public void onClick(View arg0) {
// Launch Activity
context.startActivity(in);
}
}
}