我有一个需要多选的listview(即每个列表项都有一个复选框,可以选中/取消选中)
列表视图位于tabhost中,是第一个选项卡的内容。
我的设置是这样的:
TabSpec tab = tabHost.newTabSpec("Services");
tabHost.addTab(tabHost.newTabSpec("tab_test1").setIndicator("Services").setContent(new Intent(this, ServiceList.class)));
单击选项卡时,将启动新的活动服务列表
ServiceList的定义如下:
public class ServiceList extends ListActivity{
private EscarApplication application;
ListView listView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.service_list);
ServiceList.this.application = (EscarApplication) this.getApplication();
final ListView listView = getListView();
}
protected void onListItemClick(ListView l, View v, int position, long id) {
String.valueOf(id);
Long.toString(id);
((CheckedTextView) v).setChecked(true);
super.onListItemClick(l, v, position, id);
}
@Override
public void onStart() {
super.onStart();
GenerateServiceList services = new GenerateServiceList();
int id = ServiceList.this.application.getVisitId();
services.execute(id);
listView = getListView();
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}
private class GenerateServiceList extends AsyncTask<Integer, String, Cursor> {
protected void onPreExecute() {
}
protected Cursor doInBackground(Integer...params) {
int client_id = params[0];
ServiceList.this.application.getServicesHelper().open();
Cursor cur = ServiceList.this.application.getServicesHelper().getPotentialVisitServices(client_id);
return cur;
}
protected void onPostExecute(Cursor cur){
startManagingCursor(cur);
String[] columns = new String[] {ServicesAdapter.KEY_SERVICE};
int[] to = new int[] {R.id.display_service};
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(ServiceList.this, R.layout.service_list_element, cur, columns, to);
ServiceList.this.setListAdapter(mAdapter);
ServiceList.this.application.getServicesHelper().close();
}
}
}
所以,一切正常,直到我点击我的列表项改变复选框的状态。
用于处理单击事件的代码集部分导致了以下问题:
protected void onListItemClick(ListView l, View v, int position, long id) {
String.valueOf(id);
Long.toString(id);
((CheckedTextView) v).setChecked(true);
super.onListItemClick(l, v, position, id);
}
我的理解是,传递给onListItemClick方法的视图v会重新呈现我的列表元素,因此我尝试将v转换为CheckedTextView,并将checked值设置为true,但是这只会导致我的应用程序崩溃。我是错过了一些简单的东西,还是有更简单的方法?
谢谢