2011-09-16 71 views
0

我有一個SpinnerListner類嵌套在ListActivity繼承類中。我的目標是更新由ListActivity類實現的CursorAdapter。如何刷新微調器選擇上的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. 
     } 
    } 
} 

雙方再次查詢()和notifyDataSetChanged()不工作。我在這裏做錯了什麼?

回答

0

我通過重新創建適配器對象並調用setListAdapter來解決此問題。所以刷新功能看起來像這樣:

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

     setListAdapter(adapter); 
}