我有一个嵌套在ListActivity继承类中的SpinnerListner类。我的目标是更新由ListActivity类实现的CursorAdapter。
这是我的代码:
public class test extends ListActivity {
private testAdapter adapter;
protected SQLiteDatabase db;
protected Cursor cursor;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Display wrapper
setContentView(R.layout.wrapper);
// Query Database
db = (new DatabaseHelper(this)).getWritableDatabase();
cursor = getCursor();
adapter = new testAdapter(this, cursor);
setListAdapter(adapter);
}
public class SpinnerListener implements OnItemSelectedListener {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
Toast.makeText(parent.getContext(), "The planet is " + parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
String[] values = getResources().getStringArray(R.array.values);
SharedPreferences.Editor editor = pref.preferences.edit();
editor.putString("planet", values[pos]);
editor.commit();
// notifyDataSetChanged is not working
adapter.notifyDataSetChanged();
// requery is not working either
cursor.requery();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// Do nothing.
}
}
}
requery()和notifyDataSetChanged()都不起作用。我在这里做错了什么?
答案 0 :(得分:0)
我通过重新创建适配器对象并调用setListAdapter解决了这个问题。所以刷新功能看起来像这样:
public void refresh()
{
cursor = getCursor();
adapter = new testAdapter(context, cursor);
setListAdapter(adapter);
}