user3310467
user3310467

Reputation: 45

how to make my checkbox unchecked

I've got big problem with checkbox. I've got chceckbox in my MainAdapter(not Activity), I'm checking it and go to the next actvity by clicking on button. Then I return from DefaultActivity to MainActivity and I want checkbox to be unchecked. I also add that checkbox's logic is in Adapter in ViewHolder like this :

class ViewHolder {
    TextView tv1;
    TextView tvC;
    ImageView ivT;
    CheckBox chb;
}

and all logic is in getView method.If you don't understand something and you want me to help. Just ask what you want to get.

Upvotes: 0

Views: 110

Answers (3)

Pablo Pantaleon
Pablo Pantaleon

Reputation: 1536

if is in adapter you can retrieve it in when getView() is called...

e.g.

holder.chb.setChecked(false);

with this the checkbox will be unchecked every time that de adapter is notified or created again

Upvotes: 0

nKn
nKn

Reputation: 13761

When you start your new Activity, do it calling startActivityForResult(). This will call a callback method once you close your second activity, so this way you make sure you'll enter that method once you finish() your newly opened Activity.

Once in there, simply find your view by id, and uncheck it. This is a sample code:

final Intent intent = new Intent(YourActivityThatContainsListViewDefinition.class, YourNewActivity.class);
startActivityForResult(intent, 1);

Afterwards, just override the onActivityResult() method.

protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
  switch (requestCode) {
    case 1:
      CheckBox cb = (CheckBox) findViewById(R.id.your_checkbox_id);
      cb.setChecked(false);

      break;
  }
}

---- EDIT ----

All this code would be outside your Adapter implementation - so, if you look at the code I provided, in the Intent, the first parameter is the context of the Activity that opens the second one. That means that those two overrides, you have to implement them in the Activity that calls the other (in your case, the MainActivity).

In your second Activity (DefaultActivity), you need to do nothing, just notify the first Activity (MainActivity) that it has to unckeck the CheckBox. To do so, you simply do something like this when you want to close DefaultActivity:

Intent returnIntent = new Intent();
setResult(RESULT_OK, returnIntent);
finish();

This way, you're notifying MainActivity that it should fire onActivityResult() and there's where you uncheck that CheckBox.

Upvotes: 1

Stephan
Stephan

Reputation: 16719

To uncheck your checkbox do:

chb.setChecked(false);

Upvotes: 0

Related Questions