I have a ListView
with each row consisting of a TextView
and a CheckBox
.
The user is allowed to click on each TextView
, and once he does a Dialog is presented to him where he is expected to either choose Yes or No.
When he chooses Yes, another activity is presented to him where he needs to enter data.
I am implementing all this inside a base adapter class, so inside the base adapter I created the AlertDialog
and handled it's OnClickListeners
.
Here is the problem: I need to use startActivityForResult
in order to get back the data that the user will enter in the new activity, and like I said above, I have done so in the BaseAdapter
. Now, how can I get the data from the new activity back inside the BaseAdapter
? I researched various sources and found out that one cannot start an Intent directly from a BaseAdapter
class, but instead needs to reference the Intent to the calling activity like below:
((Activity) mContext).startActivityForResult(intent, 1);
This would then result in having the the onActivityResult()
method in the adapter Activity and not inside the BaseAdapter
.
I need to leave the code inside the BaseAdapter
for various reasons.
The value I need to retrieve is a simple boolean
that if it results to true, will tick the CheckBox
next to the selected TextView
.
How could I implement this? What alternatives do you guys suggest? I tried creating a method inside the BaseAdapter
so that I can call it from the "main" Activity at the OnActivityResult()
but the CheckBox
that I need to tick is returning null at that point; the reason being quite obvious.
I would appreciate any help on this matter.
Inside BaseAdapter class
final AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setTitle("Materials");
builder.setMessage("Did you require any materials to fix this error?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
String clickedError;
clickedError = holder.text.getText().toString();
Intent intent = new Intent(mContext, Material.class);
intent.putStringArrayListExtra("materialList", materialList);
intent.putExtra("clickedError", clickedError);
intent.putExtra("repairID", repairID);
((Activity) mContext).startActivityForResult(intent, 1);
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
if(checkbox.getTag() == v.getTag())
{
checkbox.setChecked(true);
}
}
});
builder.show();
// Method to tick the checkbox.
public void TickBox(CheckBox cb)
{
cb.setChecked(true);
}
The main activity containing OnActivityResult()
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if(resultCode == RESULT_OK)
{
boolean moreThanOne = data.getBooleanExtra("moreThanOne", false);
if(moreThanOne)
{
CheckBox cb = adapter.checkbox;
adapter.TickBox(cb);
}
else
{
// ....
}
}
}