2010-08-23 72 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); 
}