我已经尝试了一段时间,但似乎无法做到。我将适配器应用于对象,但是当我在视图中输入字符时,仍然没有任何反应。
public View getView(int position, View view, ViewGroup parent) {
ViewHolder holder;
if (view == null) {
holder = new ViewHolder();
view = mInflater.inflate(R.layout.list_item, parent, false);
holder.nameTxt = (AutoCompleteTextView) view.findViewById(R.id.name_txt);
String[] drinkArray = activity.getResources().getStringArray(R.array.drink_array);
holder.adapter = new ArrayAdapter<>(activity, android.R.layout.simple_list_item_1, drinkArray);
holder.nameTxt.setAdapter(holder.adapter);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
if (!itemExists(position)) return view;
ListItem item = getItem(position);
holder.nameTxt.setText(item.name);
holder.nameTxt.setAdapter(holder.adapter);
holder.nameTxt.setTag(position);
holder.nameTxt.setOnFocusChangeListener(this);
return view;
}
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) return;
int position;
ListItem item;
switch (v.getId()) {
case R.id.name_txt:
position = (int) v.getTag();
if (!itemExists(position)) return;
item = getItem(position);
AutoCompleteTextView txtView = (AutoCompleteTextView) v;
item.name = txtView.getText().toString();
break;
default:
Log.e(TAG, "Unknown onFocusChange View ID: " + v.getId());
break;
}
}
public class ViewHolder {
public AutoCompleteTextView nameTxt;
public ArrayAdapter<String> adapter;
}
public class ListItem {
public String name;
public ListItem() {
name = "";
}
}
好了,上面的代码是我到目前为止所拥有的,可以完美地保存和加载来自textview的数据。
最佳答案
我认为您的方法getView else中存在错误。您可以尝试以下
更改
holder=(viewHolder) View.getTag();
通过
holder = view;
然后别的这种类型
ViewHolder item = (ViewHolder) holder.getTag();
...