Question

I am trying to achieve a check off list with a listview. My listview contains a string and a checkbox next to it. Right now there are a total of 15 items in the arraylist. When I click on a list item the onclicklistener starts a camera activity for result. If everything is successful with taking the picture it returns to the list with the id of the row that was click and RESULT_OK. I have everything working right now except the checkbox getting marked that the photo was successfully taken.

I am assuming that the code needs to be placed in the onActivityResult method. I am really just at a loss here.

Here is what I have so far.

This is my custom arrayadapter, the onListItemClick and the start of my 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);
        }
    }

}

Right now I am stuck with getting the id from the intent data.

Eclipse says Type mismatch cannot covert from Uri to int.

Then once I get the id back how do I set the checkbox to setChecked().

Was it helpful?

Solution

Pass the position of the item in the Intent data instead of the id. The CameraActivity them needs to save this int and return it back in its intent with this:

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

then in your onActivityResult() you get the position doing this:

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

Then you can set the checkbox by accessing it in your Grass collection:

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

Your getView will get called, see that the item is checked and set the listview item to checked too.

OTHER TIPS

try using this code :

id = Integer.parseInt(data.getData().toString());

what you are doing is wrong. Here is how to do this. In CameraClass activity you have to add the id like this

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

To mark the CheckBox ok, you have to access the view of that particular row. This you can do by doing declaring a global View at the start of the class.

View view_at_position=null;

Then in onListItemClick intitialize the View with v. This will get you the view of that particular location.

view_at_position=v;

then in onActivityResult you need to get the bundle from the 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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top