Android CheckedTextView - 单击时更改选中状态

发布于 2024-09-15 06:25:06 字数 3561 浏览 4 评论 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,但这只会导致我的应用程序崩溃。我在这里错过了一些简单的事情,还是有更简单的方法来做到这一点?

谢谢

凯文

I have a listview that needs to be multiple choice (i.e each list item has a checkbox which can be checked / unchecked)
The list view is in a tabhost and is the content for teh first tab.

My set up is like so:

My tab is set up with:

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

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

When the tab is clicked new activity ServiceList is started

ServiceList is defined as such:

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

              }
         }
    }

So, everything works ok until I click on my list item to change the checkbox state.

The part of teh code set to handle click events is causing me problems:

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

My understanding is that the View v passed to the onListItemClick method reperesents my list element, so I am trying to cast v as a CheckedTextView and set teh checked value to true, however this just causes my app to crash. Am I missing something simple here, or is there an easier way to do this?

Thanks

Kevin

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

终难愈 2024-09-22 06:25:06

您是否调试过它以检查“v”是否为空?如果是这样,您需要使用layoutInflator 来检索视图。

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

Have you debugged it to check if 'v' is null? If so, you need to use a layoutInflator to retrieve the view.

if(v == null)
{
    LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    v = vi.inflate(R.layout.service_list, null);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文