我有一个带有自定义游标适配器的Listview。我试图阻止edittext字段中的输入被回收。我一直在使用Hashmap。它首先向下滚动(在新显示的视图中不使用0等数据)。但是,当您向上滚动时,它会再次回收输入。我希望有人在这里可以发现我的问题bc我已经查看了有关使用edittext的回收输入问题的其他帖子,我看不出我做错了什么,而不是其他的。
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;
}
}
我在下面的代码中尝试了这个,但最终我的回收视图称为位置0。
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 {....
答案 0 :(得分:0)
您可能希望在添加新的TextWatcher实例之前删除旧的TextWatcher实例。看起来您有多个TextWatchers可以在列表中的多个位置观看相同的EditText,这可能最终看起来像是视图回收出错了。