I have a ListView
where each item contains multiple EditTexts
. I'm defining a scale, so one of the EditTexts is dependent on an EditText in the item above. In the example below, B is set to the same value as A when A has been edited. Thus, the user doesn't actually use the right column.
I'm able to listen to events in A and update B and call notifyDataSetChanged()
accordingly, but then I lose focus of my EditText as the ListView is redrawn.
Bottom line, what I want is:
Edit field A and when I then tap on field C, I'd like A's value to apply to B and I'd like focus to go to C.
It's the "focus goes to C" part that I'm having trouble with.
p.s. There's actually another EditText on the row, so focus won't always be going to the column that has A and C.
Edit: Here is my adapter's getItemId()
method, to help answer @aniv's comment.
@Override
public Object getItem(int arg0) {
return arg0;
}
EDIT_2: After overriding hasStableIds()
to return true, it works great when I go from A to a third column in another item, but not when I go from A to C. I think my onFocusChange
override is messing something up because it only behaves strangely when changing focus in that column (in which share the same onFocusChange method). Here's my custom onFocusChangeListener.
private class MyFocusChangeListener implements View.OnFocusChangeListener{
private EditText et;
private ScaleItem item;
private Integer pos;
private MyFocusChangeListener(ScaleItem item, Integer pos){
this.item = item;
this.pos = pos;
}
@Override
public void onFocusChange(View v, boolean hasFocus){
if(!hasFocus){
et = (EditText) v;
Activity_EditCourseGPA a = (Activity_EditCourseGPA) activity;
a.updateMaxOnTextChange(item, pos, et.getText().toString());
} else {
}
}
}
The function it refers to in my activity (to use the EditText field to update underlying data)....
public void updateMaxOnTextChange(ScaleItem item, Integer pos, String str){
if(pos < scaleItems.size()){
scaleItems.get(pos + 1).setMax(Double.valueOf(str));
scaleListAdapter.notifyDataSetChanged();
}
}
I use the EditText position and contents to update the underlying data at the same position. Here's a glimpse of what happens when I change focus within the same column. Very weird.