对于 android 来说仍然是新手,对于自定义光标适配器来说甚至更多,所以我无法理解如何防止我的 ListView 回收 View 以防止滚动时来自一个编辑文本的输入显示在另一个编辑文本中。我在其他帖子上看到说要更改 convertview 的名称,但如何更改我正在绘制空白。我希望这里有人能够根据我目前编写的代码提供更多细节或示例。
public class editview extends ListActivity {
private dbadapter mydbhelper;
private PopupWindow pw;
public static int editCount;
public static ListView listView;
/** Called when the activity is first created. */
@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};
ItemAdapter 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;
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(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);
return convertView;
}
}
改成
class ItemAdapter extends SimpleCursorAdapter {
private LayoutInflater mInflater;
private Cursor cursor;
Map<Integer, String> inputValues = new HashMap<Integer, String>();
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);
String 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());
}
但在所有的edittext有数据后它正在回收。尝试使用 holder.edittext.setText(oldText) 但效果相同。
最佳答案
首先,您真的不想阻止 ListView 回收其 View 。 View 回收是一个巨大的优化。有关列表的很多非常好的信息,请参阅 google IO 讨论:http://www.youtube.com/watch?v=wDBM6wVEO70
也就是说,您已经正确地确定了您的问题:您的 EditTexts 比列表中的项目少得多。当您滚动浏览列表时,这些 EditText 将被回收,因此您会一遍又一遍地看到相同的输入。
基本上您需要做的是将 EditText 的输入保存在映射的某些数据结构中(如果它们只会编辑几个值,则为 HashMap,如果它们将更改大部分值,则可能为 List,两者都可以)输入的位置。您可以通过将 textChangedListener 添加到 getView 中的编辑文本来实现此目的:
@Override
public View getView(final int position, View convertView, ViewGroup parent){
...
cursor.moveToPosition(position);
int label_index = cursor.getColumnIndex("userword");
String label = cursor.getString(label_index);
holder.text.setText(label);
//clear whatever text was there from some other position
//and set it to whatever text the user edited for the current
//position if available
String oldText = yourMapOfPositionsToValues.get(position);
holder.setText(oldText == null ? "" : oldText);
//every time the user adds/removes a character from the edit text, save
//the current value of the edit text to retrieve later
holder.edittext.addTextChangedListener(new TextWatcher(){
@Override
public void afterTextChanged(Editable editable) {
yourMapOfPositionsToValues.put(position, editable.toString());
}
....
};
return convertView;
}
无论何时您的用户完成编辑,您都可以遍历您的数据结构并对这些值执行任何操作。
编辑:
我将 onTextChanged 更改为 afterTextChanged 因为我以前用过它并且我知道它有效。请记住,每次 LETTER 更改时都会调用 afterTextChanged,而不是在用户完成输入单词后调用。如果用户键入“dog”,afterTextChanged 将被调用三次,首先是“d”,然后是“do”,然后是“dog”。
HashMap 很简单:Map yourMapOfPositionsToValues = new HashMap();
添加或更新项目:yourMap.put(position, someText); 获取项目:yourMap.get(position);
如果 HashMap 没有意义,请花一些时间研究它们。它们是非常重要的数据结构。
您的 TextWatcher 实现不正确。您的数据结构不应属于单个 View ,而应属于 Activity 或您的适配器。在您看来,位置并不稳定,因为您的列表由每个 View 拥有。位置本身是稳定的,因为除非基础数据发生变化,否则游标每次都会为相同的位置返回相同的数据。但是,编辑文本用于多个不同的位置。
创建一个 hashmap 作为我在适配器的构造函数中演示的实例变量。然后直接加上我原来写的TextWatcher,不需要命名类,匿名更简单。您的代码应该可以工作。
关于android - ListView 中的 EditText 没有它回收输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9438676/