Android 即使使用Hashmap,EditText输入也可以循环使用
我有一个带有自定义光标适配器的Listview。我试图阻止edittext字段中的输入被回收。我一直在用Hashmap。当您第一次向下滚动时(在新显示的视图中不使用来自0等的数据),它会起作用。但当你向上滚动时,它会再次回收输入。我希望这里有人能发现我的问题,因为我看过其他关于edittext回收输入的文章,我看不出和其他人相比我做错了什么Android 即使使用Hashmap,EditText输入也可以循环使用,android,listview,hashmap,android-edittext,Android,Listview,Hashmap,Android Edittext,我有一个带有自定义光标适配器的Listview。我试图阻止edittext字段中的输入被回收。我一直在用Hashmap。当您第一次向下滚动时(在新显示的视图中不使用来自0等的数据),它会起作用。但当你向上滚动时,它会再次回收输入。我希望这里有人能发现我的问题,因为我看过其他关于edittext回收输入的文章,我看不出和其他人相比我做错了什么 public class editview extends ListActivity { private dbadapter mydbhelper;
public class editview extends ListActivity {
private dbadapter mydbhelper;
public static int editCount;
public static ListView listView;
public ItemAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mydbhelper = new dbadapter(this);
mydbhelper.open();
View footer = getLayoutInflater().inflate(R.layout.footer_layout, null);
ListView listView = getListView();
listView.addFooterView(footer);
showResults();
}
//Populate view
private void showResults (){
Cursor cursor = mydbhelper.getUserWord();
startManagingCursor(cursor);
String[] from = new String[] {dbadapter.KEY_USERWORD};
int[] to = new int[] {R.id.textType};
adapter = new ItemAdapter(this, R.layout.edit_row, cursor,
from, to);
adapter.notifyDataSetChanged();
this.setListAdapter(adapter);
editCount = adapter.getCount();
}
//footer button
public void onClick(View footer){
final MediaPlayer editClickSound = MediaPlayer.create(this, R.raw.button50);
editClickSound.start();
startActivity(new Intent("wanted.pro.madlibs.OUTPUT"));
}
...
//custom cursor adapter
class ItemAdapter extends SimpleCursorAdapter {
private LayoutInflater mInflater;
private Cursor cursor;
static Map<Integer, String> inputValues = new LinkedHashMap<Integer, String>();
static String oldText;
public ItemAdapter(Context context, int layout, Cursor cursor, String[] from,
int[] to) {
super(context, layout, cursor, from, to);
this.cursor = cursor;
mInflater = LayoutInflater.from(context);
}
static class ViewHolder {
protected TextView text;
protected EditText edittext;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.edit_row, null);
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.textType);
holder.edittext = (EditText) convertView.findViewById(R.id.editText);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
cursor.moveToPosition(position);
int label_index = cursor.getColumnIndex("userword");
String label = cursor.getString(label_index);
holder.text.setText(label);
oldText = inputValues.get(position);
holder.edittext.setText(oldText == null ? "" : oldText);
holder.edittext.addTextChangedListener(new TextWatcher(){
public void afterTextChanged(Editable editable) {
inputValues.put(position, editable.toString());
//Log.e(String.valueOf(position), "position in Hashmap");
//Log.e(inputValues.get(position), "data in Hashmap");
}
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
// TODO Auto-generated method stub
}
public void onTextChanged(CharSequence s, int start,
int before, int count) {
// TODO Auto-generated method stub
}
});
return convertView;
}
}
您可能希望在添加新的TextWatcher实例之前删除旧的TextWatcher实例。看起来您可以让多个textwatcher在列表中的多个位置查看同一EditText,这可能会导致查看循环出错。如果已经附加了一个textwatcher子类,是否有办法删除或阻止添加一个新的textwatcher?我担心,如果我将其子类化,我将在获取inputValues.put(position,editable.toString())时花费大量时间;要按需工作(需要确保它将数据放置在与视图中的edittext相同的位置),您不需要这样做。只需在ViewHolder类中实现TextWatcher,并仅在膨胀视图时添加TextChangedListener()。这将为每个EditText提供一个TextWatcher。然后还需要将“位置”添加到ViewHolder中,以便在afterTextChanged()中找到该行对应的数据。
static class ViewHolder implements TextWatcher {
protected TextView text;
protected EditText edittext;
protected int position;
public void afterTextChanged(Editable editable) {
Log.e(String.valueOf(position), "Position in array");
Log.e(editable.toString(), "data in array");
inputValues.put(position, editable.toString());
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
}
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.edit_row, null);
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.textType);
holder.edittext = (EditText) convertView.findViewById(R.id.editText);
holder.position = position;
holder.edittext.addTextChangedListener(holder);
convertView.setTag(holder);
} else {....