编辑:
我发现原因是当我尝试编辑某些内容时调用了 getView(),因此加载了来自 DataAdapter 的数据并且我编辑的更改消失了。
编辑:
我观察到一件事,如果列表视图中的行数很少,则可以,但是如果列表视图中有很多行无法在可见屏幕中显示(滚动条似乎滚动到其他记录),那么问题就出现了!
我正在处理我们使用 ListView实现内联编辑的项目,即可以在列表视图中编辑数据。
我为该 ListView 的每个项目/行定义了一个 xml。我正在使用自定义 DataAdapter 将数据与 ListView 绑定。
当我第一次加载 ListView 加载该活动时,我可以编辑数据并且它工作正常。编辑某些内容时,更改会保存到 SQLite 数据库中,为此我有一个按钮。
现在的问题是,在第一次保存数据并再次加载列表视图后,我无法再编辑数据了。当我尝试编辑数据时,键盘出现然后自动消失,输入的数据也消失了。请查看屏幕截图。
有人可以帮我解决这个问题吗?
我的自定义适配器类:
public class QuestionAdapter extends ArrayAdapter<QuestionEntity> {
private ArrayList<QuestionEntity> items;
private Context CurrentContext;
private QuestionEntity CurrentItem;
private Cursor OptionsCursor;
public QuestionAdapter(Context context, ArrayList<QuestionEntity> items, Cursor curOptions)
{
super(context, R.layout.grid_item, items);
this.CurrentContext = context;
this.items = items;
this.OptionsCursor = curOptions;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
//verify that the items list is still valid since
//the list may have been cleared during an update
if ((items == null) || ((position + 1) > items.size()))
return convertView; //Can't extract item
CurrentItem = items.get(position);
if(convertView == null)
{
LayoutInflater inflater = LayoutInflater.from(CurrentContext);
convertView = inflater.inflate(R.layout.grid_item, null);
}
if (convertView != null)
{
TextView txtQuestion = (TextView) convertView.findViewById(R.id.txtQuestion);
txtQuestion.setText(CurrentItem.getTitle());
Spinner cmbOptions = (Spinner)convertView.findViewById(R.id.cmbOptions);
/*
* Load the options from OptionsCursor
*/
LoadOptions(cmbOptions);
/*
* Attach onItemClick event with cmbOptions
* When the user change the option we will populate the comments based on the option
*/
cmbOptions.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id)
{
try
{
//If MyCondition is true show msg to user.
}
catch(Exception ex)
{
ex.toString();
}
}
});
}
return convertView;
}
private void LoadOptions(Spinner iacOptions)
{
//Load data in the spinner using the OptionsCursor
}
}