2010-08-23 71 views
0

我有一个列表视图需要多项选择(即每个列表项都有一个复选框,可以选中/不选中) 列表视图位于tabhost中,是内容第一个标签。Android CheckedTextView - 点击更改检查状态

我的设立是像这样:

我的选项卡设置了:

TabSpec tab = tabHost.newTabSpec("Services"); 

tabHost.addTab(tabHost.newTabSpec("tab_test1").setIndicator("Services").setContent(new Intent(this, ServiceList.class))); 

当标签被点击新活动ServiceList开始

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(); 
      //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();  

       } 
     } 
    } 

所以,一切正常,直到我点击我的列表项目更改复选框状态。

设置为处理单击事件格兰代码的部分引起了我的问题:

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方法reperesents我的列表元素,所以我想投v作为CheckedTextView并将检查值设置为true,但是这只会导致我的应用程序崩溃。我在这里错过简单的东西,还是有更简单的方法来做到这一点?

感谢

凯文

+0

可以显示崩溃的确切堆栈跟踪或logcat输出吗? – 2010-08-23 17:24:41

回答

0

你有没有调试它来检查 'V' 为空?如果是这样,您需要使用layoutInflator来检索视图。

if(v == null) 
{ 
    LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    v = vi.inflate(R.layout.service_list, null); 
}