0

複数選択する必要のあるリストビューがあります(つまり、各リスト項目にはチェックボックスをオン/オフにすることができます)。リストビューはタブホストにあり、最初のタブのコンテンツです。

私の設定は次のようになります。

私のタブは次のように設定されています:

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);
            }

私の理解では、onListItemClickメソッドに渡されたView vはリスト要素を表すため、vをCheckedTextViewとしてキャストし、チェックされた値をtrueに設定しようとしていますが、これによりアプリがクラッシュします。ここで簡単なものが欠けていますか、それともこれを行うためのより簡単な方法がありますか?

ありがとう

ケビン

4

1 に答える 1

0

'v'がnullかどうかを確認するためにデバッグしましたか?その場合は、layoutInflatorを使用してビューを取得する必要があります。

if(v == null)
{
    LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    v = vi.inflate(R.layout.service_list, null);
}
于 2010-09-05T14:09:27.430 に答える