ListActivityがあり、そのリストの先頭にリストビューが実際に表示する内容を選択できるようにしたいと思っています。ListActivityのマネージドカーソルを変更する正しい方法は何ですか
onCreateメソッドで私は最初のカーソルをインスタンス化し、startManagingCursor()を呼び出します。私はまた、ビューのレンダリングを担当するカスタムCursorAdapterをインスタンス化します。
私が知りたいのは、ユーザーがフィルタースピナーの項目を選択したときにカーソルを変更する正しい方法です。私がやっている何
はスピナーに、私は新しいカーソル、新しいCursorAdapterを作成し、
stopManagingCursor(currentCursor)を呼び出すonItemSelected()メソッド内OnItemSelectedListenerを追加することです。 currentCursor = newCursor; startManagingCursor(currentCursor); setListAdapter(newAdapter);
これは適切な方法ですか? どうすればいいですか? 何か忘れていますか? これは醜いですか?
public class MyListActivity extends ListActivity {
private Spinner typeFilterSpinner;
private MyListAdapter cursorAdapter;
private Cursor currentCursor;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
typeFilterSpinner = (Spinner) findViewById(R.id.TypeFilterSpinner);
typeFilterSpinner.setOnItemSelectedListener(new SpinnerItemSelectedListener());
currentCursor = MyDAO.getInstance().getMyCursor(null);
startManagingCursor(currentCursor);
cursorAdapter = new SelectionListAdapter(this, currentCursor);
setListAdapter(cursorAdapter);
}
class SelectionListAdapter extends CursorAdapter {
public FavouriteLocationSelectionListAdapter(Context context, Cursor cursor){
super(context, cursor, true);
[....] other initialization stuff here
}
[....] overriden rendering methods here
}
public class SpinnerItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent,View view, int pos, long id) {
Long mi = spinnerItems.get(pos);
Cursor newCursor = MyDAO.getInstance().getMyCursor(mi);
//TODO maybe call setCursor on the adapter here instead of instanciating a new cursor
SelectionListAdapter newAdapter =
new SelectionListAdapter(MyListActivity.this, newCursor);
stopManagingCursor(currentCursor);
currentCursor = newCursor;
startManagingCursor(currentCursor);
setListAdapter(newAdapter);
}
public void onNothingSelected(AdapterView parent) {
// woooork ?
}
}
}
アイデアザッツ:
はここにいくつかのコードです。
ありがとうございます!
ありがとうございました。スレッドに関しても、別のスレッドに新しいカーソルを作成する必要がありますか? – azpublic
実際に使用しているカーソルのタイプとその速さによって異なります。実際には、多くのデータを扱っているか、パフォーマンスの問題が顕著でない限り、ほとんどの開発者がこれを行うとは思わない。 Honeycombには新しい['CursorLoader'](http://developer.android.com/reference/android/content/CursorLoader.html)APIもあります。 –