更改 ListActivity 的托管光标的正确方法是什么
我有一个 ListActivity,在其顶部我想要一个微调器,以允许用户选择 ListView 应实际显示的内容。
在 onCreate 方法中,我实例化第一个光标并调用 startManagingCursor()。我还实例化了一个负责渲染视图的自定义 CursorAdapter。
我想知道的是当用户在过滤器微调器中选择某个项目时更改光标的正确方法。
我正在做的是将 OnItemSelectedListener 添加到微调器,并在 onItemSelected() 方法内创建一个新的 Cursor,然后创建一个新的 CursorAdapter,然后调用
stopManagingCursor(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 ?
}
}
}
这就是想法。
感谢您的帮助!
I have a ListActivity at the top of which I want a spinner to allow th user select what the ListView should actually display.
In the onCreate method I instanciate my first cursor and call startManagingCursor(). I also instanciate a custom CursorAdapter in charge of rendering the view.
What I want to know is the correct way to change the cursor when the user selects an item in the filter spinner.
What I'm doing is add a OnItemSelectedListener to the spinner and inside the onItemSelected() method I create a new Cursor then new CursorAdapter and then call
stopManagingCursor(currentCursor);
currentCursor = newCursor;
startManagingCursor(currentCursor);
setListAdapter(newAdapter);
Is this an appropriate way to do this ?
How else should/could I do it ?
Am I forgetting something ?
Is this ugly ?
Here's some code :
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 ?
}
}
}
Thats the idea.
Thanks for helping !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
stopManagingCursor
不会关闭Cursor
,因此您需要在切换它们时执行此操作。正如您在代码中指出的那样,最好保留相同的适配器,并为其提供新的光标。作为奖励,调用 CursorAdapter.changeCursor(Cursor) 将为您关闭旧的 Cursor。stopManagingCursor
doesn't close theCursor
, so you'll want to do that when you are switching them. As you point out in your code, it be better to keep the same adapter, and give it the new cursor. And as a bonus, callingCursorAdapter.changeCursor(Cursor)
will close the oldCursor
for you.