I have a ListView – getListView() of a ListActivity. The ListView is populated correctly and thats all fine.
Once any ListView item is clicked, it opens up a dialog consisting of 4 title TextViews and 4 user input Number EditTexts. I want to attach a Tag object to each of those EditText or to the ListView item however possible and keep track any changes made to the EditText using a TextWatcher.
i.e. In the Textwatcher if any changes are made to the text it is updated in the corresponding tag.
This would allow me to set the EditText text to any pre-existing values that have already been set.
The PROBLEM is that once the Dialog comes up, I enter EditText values and press positive button to dismiss. When I go to open again and retrieve tag to set EditText values before displaying values, they are all 0 (Except position – which is rather unimportant)
My onListItemClick code can be seen below.
// On Routine Selected
@Override
protected void onListItemClick(ListView l, View view, int position, long id)
{
// Get the tag from 'v'
if (view.getTag() == null)
{
// Create a new Set object and set that as the tag
Set newTag = new Set(position);
view.setTag(newTag);
}
// Getting the Set tag from the ListView item
Set vTag = (Set) view.getTag();
/* ** Now an AlertDialog is presented to the user with a custom inflated view including EditTexts ** */
// Getting an inflater (getLayoutInflater()) also works
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// Inflating the XML defined layout as a view
final View setView = inflater.inflate(R.layout.set_dialog, null);
// Get EditText XML references
EditText etReps = (EditText) setView.findViewById (R.id.etReps);
EditText etIntensity = (EditText) setView.findViewById (R.id.etIntensity);
EditText etPosition = (EditText) setView.findViewById (R.id.etPosition);
EditText etChange = (EditText) setView.findViewById (R.id.etChange);
Log.d(TAG, "vTag.getReps(): " + (vTag.getReps() + 1));
try
{
// Set the EditText values to values from the Set Tag
etReps.setText(String.valueOf(vTag.getReps()));
etIntensity.setText(String.valueOf(vTag.getIntensity()));
etPosition.setText(String.valueOf(vTag.getPositionInExercise()));
etChange.setText(String.valueOf(vTag.getIntensityChange()));
// Add Custom TextWatcher listener to all EditTexts
etReps.addTextChangedListener(new MyTextWatcher(setView));
etIntensity.addTextChangedListener(new MyTextWatcher(setView));
etPosition.addTextChangedListener(new MyTextWatcher(setView));
etChange.addTextChangedListener(new MyTextWatcher(setView));
}
catch (Exception e)
{
Toast.makeText(this, e.toString(), Toast.LENGTH_SHORT).show();
}
// Create a new Builder to create the dialog
AlertDialog.Builder builder = new AlertDialog.Builder(ExerciseList.this);
builder.setTitle(vTag.getName());
builder.setView(setView);
builder.setPositiveButton("Save", new OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
if (which == dialog.BUTTON_POSITIVE)
{
// setView.setTag(tag);
dialog.dismiss();
}
}
});
builder.show();
}
MyTextWatcher code..
//Custom Watcher
class MyTextWatcher implements TextWatcher
{
View view;
public MyTextWatcher(View setView)
{
view = setView;
}
@Override
public void afterTextChanged(Editable editable)
{
String newText = editable.toString();
Set tag = (Set) view.getTag();
switch (view.getId())
{
case R.id.etReps:
tag.setReps(Integer.parseInt(newText));
break;
case R.id.etIntensity:
tag.setIntensity(Integer.parseInt(newText));
break;
case R.id.etPosition:
tag.setPositionInExercise(Integer.parseInt(newText));
break;
case R.id.etChange:
tag.setIntensityChange(Integer.parseInt(newText));
break;
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after){}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count){}
}
Any advice or steers in the direction would be great!
It turns out for this case, a
TextWatcheris not need at all. The main problem was to do with the scope of my vTag object.TextWatcherupdates with every character as well, that doesn’t really benefit me and is just unneccesary computation.Declaring my
EditTextXML referencesfinal(The ones which I assigned to the Tag object of each ListItem view) means I can update the tag in the positive Button listener.Benefits and learning outcomes:
EditText.TextWatcher, all sorts of updates would have taken place with the user just pressing Cancel and negating them all.TextWatcherdefinitely has a place, however this wasn’t it.Just thought I’d contribute my finding and reasoning!
Cheers