我有一个ListView
- getListView()
of a ListActivity
。ListView
正确填充,这一切都很好。
单击任何ListView
项目后,它会打开一个由 4 个标题 TextViews 和 4 个用户输入 Number EditTexts 组成的对话框。我想将一个 Tag 对象附加到其中的每一个EditText
或ListView
项目上,并尽可能跟踪对EditText
使用TextWatcher
.
即,Textwatcher
如果对文本进行了任何更改,则会在相应的标签中进行更新。
这将允许我将EditText
文本设置为已设置的任何预先存在的值。
问题是一旦对话框出现,我输入EditText
值并按肯定按钮关闭。当我再次打开并检索标签以EditText
在显示值之前设置值时,它们都是 0(除了位置 - 这是相当不重要的)
我的 onListItemClick 代码如下所示。
// 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 代码..
//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){}
}
任何建议或指导方向都会很棒!