如何在微调器选择上刷新CursorAdapter?

时间:2022-06-01 21:58:45

I have a SpinnerListner class that is nested in ListActivity inherited class. My aim is to update the CursorAdapter that is implemented by the ListActivity class.

我有一个嵌套在ListActivity继承类中的SpinnerListner类。我的目标是更新由ListActivity类实现的CursorAdapter。

Here is my code:

这是我的代码:

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.
        }
    }
}

Both requery() and notifyDataSetChanged() are not working. What am I doing wrong here?

requery()和notifyDataSetChanged()都不起作用。我在这做错了什么?

1 个解决方案

#1


0  

I solved this problem by recreating adapter object and calling setListAdapter. So refresh function look something like this:

我通过重新创建适配器对象并调用setListAdapter解决了这个问题。所以刷新功能看起来像这样:

public void refresh()
    {
        cursor = getCursor();
        adapter = new testAdapter(context, cursor);

        setListAdapter(adapter);
}

#1


0  

I solved this problem by recreating adapter object and calling setListAdapter. So refresh function look something like this:

我通过重新创建适配器对象并调用setListAdapter解决了这个问题。所以刷新功能看起来像这样:

public void refresh()
    {
        cursor = getCursor();
        adapter = new testAdapter(context, cursor);

        setListAdapter(adapter);
}