2

我正在尝试使用列表视图实现核对列表。我的列表视图包含一个字符串和旁边的复选框。现在,arraylist 中共有 15 个项目。当我单击一个列表项时,会onclicklistener启动一个camera activity结果。如果拍摄照片一切都成功,它会返回到列表,其中包含单击 和 的行的 id RESULT_OK。我现在一切正常,除了标记照片已成功拍摄的复选框。

我假设代码需要放在onActivityResult方法中。我在这里真的很茫然。

这是我到目前为止所拥有的。

这是我的自定义数组适配器,也是onListItemClick我的onActivityResult.

private class GrassAdapter extends ArrayAdapter<Grass> {
    public GrassAdapter(ArrayList<Grass> grasss) {
        super(getActivity(), 0, grasss);
    }

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // If we weren't given a view, inflate one
    if (convertView == null) {
        convertView = getActivity().getLayoutInflater()
            .inflate(R.layout.fragment_grasslist, null);
    }

    // Configure the view for this Crime
    Grass c = getItem(position);

    TextView titleTextView =
        (TextView)convertView.findViewById(R.id.photo_titleTextView);
    titleTextView.setText(c.getName());

    CheckBox solvedCheckBox =
        (CheckBox)convertView.findViewById(R.id.photo_solvedCheckBox);
    solvedCheckBox.setChecked(c.isChecked());

    return convertView;
}
}

public void onListItemClick(ListView l, View v, int position, long id) {
    Grass selectedValue = (Grass) getListAdapter().getItem(position);
    //Toast.makeText(this, selectedValue, Toast.LENGTH_SHORT).show();

    // Start CameraActivity
    Intent i = new Intent(getActivity(), CameraActivity.class);
    i.putExtra("ID", id);
    i.putExtra("FILE_NAME", selectedValue.getName());
    startActivityForResult(i, POSITION_CHECKED);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == POSITION_CHECKED) {
        if (resultCode == Activity.RESULT_OK) {
            int id = data.getData();
            .setChecked(true);
        }
    }

}

现在我坚持从意图数据中获取 id。

日食说Type mismatch cannot covert from Uri to int

然后,一旦我找回了 id,我该如何将复选框设置为setChecked().

4

3 回答 3

0

尝试使用此代码:

id = Integer.parseInt(data.getData().toString());
于 2013-10-19T03:09:12.887 回答
0

你在做什么是错的。这是如何做到这一点的。在CameraClass活动中,您必须添加id这样的

Intent intent = new Intent();
    Bundle bundle = new Bundle();
    bundle.putInt("PhotoTaken",id);
    intent.putExtras(bundle);
    setResult(RESULT_OK,intent);
    finish();

要标记CheckBox确定,您必须访问该特定行的视图。这可以通过在View课程开始时声明一个全局来实现。

View view_at_position=null;

然后在onListItemClick初始化Viewwith v。这将为您提供该特定位置的视图。

view_at_position=v;

然后onActivityResult你需要从Intent data

Bundle bundle = data.getExtras();
int id = bundle.getInt("PhotoTaken");
CheckBox cb = (CheckBox)view_at_position.findViewById(R.id.photo_solvedCheckBox);
cb.setChecked(true);
于 2013-10-19T03:20:03.580 回答
0

传递项目在 Intent 数据中的位置而不是 id。CameraActivity 他们需要保存这个 int 并将其返回到它的意图中:

Intent i = new Intent();
i.putExtra("RETURN_POSITION", mSavedInt;

然后在您的 onActivityResult() 中,您将获得执行以下操作的位置:

int position = data.getIntExtra("RETURN_POSITION");

然后您可以通过在您的 Grass 集合中访问它来设置复选框:

mGrass.get(position).setIsChecked(true);
mGrassAdapter.notifyDataSetChanged();

您的 getView 将被调用,查看该项目是否已检查并将 listview 项目设置为已检查。

于 2013-10-19T06:20:11.850 回答