仍然是新的android和更多的自定義遊標適配器,所以我無法理解如何防止從列表視圖回收視圖,以防止輸入從一個編輯文本滾動顯示在另一個。我在其他帖子上看到說要更改convertview的名稱,但是如何做到這一點我正在畫空白。我希望這裏的某個人能夠根據我迄今爲止編寫的代碼提供更多細節或示例。EditText在ListView沒有它回收輸入
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),但效果相同。
我正在玩aftertextchange,但我發現它沒有存儲我想要的值。 IE它只將部分字符串存儲到數組的不同部分。我可以在最後一個編輯文本中輸入類似狗的東西,以及何時調用ArrayList editTextList中的位置。它看起來像pos1狗pos2做pos3做的。我不明白爲什麼它只抓取edittext字符串的一部分。 –
maebe
2012-02-25 05:38:31
職位似乎不可靠,但也許我把他們稱爲錯誤的。當我使用simplecursor適配器時,我通過for循環更改了edittext的id,並使用該循環在下一個活動中存儲和調用它們。但仍遇到回收問題。過去兩週我一直在關注hashmaps,但似乎無法找到解釋如何使用它們的好消息。只有一點點的代碼在這裏和那裏。 – maebe 2012-02-25 05:38:54
// TextWatcher,將數據保存到數組以備後用。 class watcher實現TextWatcher {0}私有int位置; public watcher(int position){this.position = position; } \t公共無效afterTextChanged(可編輯或多個){ \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t Log.e(將String.valueOf(位置) 「是位置INT」) ; \t String text = s.toString(); \t editview.editTextList.add(position,text); 是我想到了我的textwatcher – maebe 2012-02-25 05:39:19