Android checkedTextView-单击时更改检查状态
-
30-09-2019 - |
题
我有一个列表视图,它需要是多项选择(即每个列表项目都有一个可以检查 /未检查的复选框)列表视图在TABHOST中,是TEH第一个选项卡的内容。
我的设置就像:
我的标签设置为:
TabSpec tab = tabHost.newTabSpec("Services");
tabHost.addTab(tabHost.newTabSpec("tab_test1").setIndicator("Services").setContent(new Intent(this, ServiceList.class)));
单击选项卡时,新的活动服务师开始
服务主义者被定义为:
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();
//Generate and display the List of visits for this day by calling the AsyncTask
GenerateServiceList services = new GenerateServiceList();
int id = ServiceList.this.application.getVisitId();
services.execute(id);
listView = getListView();
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}
//The AsyncTask to generate a list
private class GenerateServiceList extends AsyncTask<Integer, String, Cursor> {
// can use UI thread here
protected void onPreExecute() {
}
// automatically done on worker thread (separate from UI thread)
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;
}
// can use UI thread here
protected void onPostExecute(Cursor cur){
startManagingCursor(cur);
// the desired columns to be bound
String[] columns = new String[] {ServicesAdapter.KEY_SERVICE};
// the XML defined views which the data will be bound to
int[] to = new int[] {R.id.display_service};
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(ServiceList.this, R.layout.service_list_element, cur, columns, to);
// set this adapter as your ListActivity's adapter
ServiceList.this.setListAdapter(mAdapter);
ServiceList.this.application.getServicesHelper().close();
}
}
}
因此,一切正常,直到我单击列表项以更改复选框状态。
用于处理点击事件的TEH代码设置的一部分使我有问题:
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);
}
我的理解是,视图v传递给了onListItemClick方法记录我的列表元素,因此我试图将v施加为checkEdTextView,并将teh ocked值设置为true,但是这只是导致我的应用程序崩溃。我在这里缺少简单的东西,还是有一种更简单的方法来做到这一点?
谢谢
凯文
解决方案
您是否对其进行调试以检查“ V”是否为空?如果是这样,您需要使用LayoutInflator来检索视图。
if(v == null)
{
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.service_list, null);
}
不隶属于 StackOverflow