我试图通过意图的帮助将我的listview中存在的复选框的位置传递给另一个活动。但它在另一项活动中显示我为空。或者有人可以告诉我如何使用下面的代码将选定的复选框数据传递给另一个活动?
我想从相应复选框的列表视图中将值存储在数组中。然后转移到另一个活动
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);
}
}
}
答案 0 :(得分:1)
将getPosition清除为静态变量,您可以访问 值MyAdapter.getPosition到另一个活动
public static getPosition;
以及您要访问此变量的活动
MyAdapter.getPosition use this.
但是你传递价值的方式也是怀特但是在传递之前 它是意图检查天气你的值不为空,在这种情况下
getPosition = (Integer) buttonView.getTag();
无法获取值。
答案 1 :(得分:0)
尝试以下方式。无需在构造函数中传递getPositionx
,因为您已在类中全局定义它。
class MyCustomItemClickListener implements OnClickListener {
MyCustomItemClickListener() {
/*
* Whatever initialization you need can be done here. You can pass
* values in constructor when calling it from getview and use with
* intent extras
*/
}
@Override
public void onClick(View arg0) {
// Launch Activity
in = new Intent(context, YourActivity.class);
in.putExtra("aa", getPositionx);
context.startActivity(in);
}
}
并将初始化更改为
convertView.setOnClickListener(new MyCustomItemClickListener());