0

我试图借助意图将列表视图中存在的复选框的位置传递给另一个活动。但它显示我在另一项活动中为空。或者有人可以告诉我如何使用下面的代码将选定的复选框数据传递给另一个活动?

我想从相应复选框的列表视图中将值存储在一个数组中。然后转移到另一个活动

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);
    }

  }
}
4

2 回答 2

1

将 getPosition 清除为静态变量,您可以将值 MyAdapter.getPosition 访问到另一个活动

public static getPosition;

以及您要访问此变量的活动

MyAdapter.getPosition use this.

但是您传递值的方式也是正确的,但在将其传递给意图之前检查天气您的值不为空,在这种情况下

getPosition = (Integer) buttonView.getTag();

无法获取值。

于 2013-02-23T06:35:49.243 回答
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());
于 2013-02-23T06:31:39.167 回答