【问题标题】:how can i access certain content (for example checkbox) of certain child or of all children in an expandable listview?如何在可扩展列表视图中访问某些子项或所有子项的某些内容(例如复选框)?
【发布时间】:2014-08-20 22:27:20
【问题描述】:

我这两天就遇到这个问题,我在这里搜索解决方案,但没有找到任何东西。

问题如下: 我正在尝试设置复选框,它们是可扩展列表视图的每个子项的一部分。 我正在尝试设置它们,当我设置另一个复选框时,它不是可扩展列表视图的一部分。

那么我如何访问复选框?

我想从外部复选框的侦听器中访问它们。

chckbxAlleTage.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView,
                        boolean isChecked) {
                    int childrenCount = listAdapter.getChildrenCount(0);
                    if (chckbxAlleTage.isChecked()) {
                        for (int index = 0; index<childrenCount; index++ )
                        {
                            CheckBox tempchckBx = (CheckBox) (listAdapter.getChildView(
                                    0, index, true,findViewById(R.id.LinearLayout1) , expListView))
                                    .findViewById(R.id.chckBx);
                            tempchckBx.setChecked(true);
                        }
                        AnzahlTage = 7;

                    }else
                        {
                            AnzahlTage = 0;
                        }   

                    setEtAnzahlTage();
                }
            });

此代码在侦听器中的结果是仅选中了一个复选框。 我希望有人能弄清楚这是什么问题。

thnx

【问题讨论】:

    标签: android listview checkbox expandablelistview


    【解决方案1】:

    您需要通过更改支持适配器的数据而不是手动更改视图来处理此问题。无论如何滚动,适配器都会重新绘制视图。

    您真正应该做的是更改适配器的支持 DATA 层,以便当适配器调用 getView 时,它可以检查是否应该选中该框。将支持数据更改为适配器后,您可以在适配器上调用 notifyDatasetChanged(),以便它可以根据新数据重绘其视图。

    如果您发布您的适配器代码,我可以更具体。

    【讨论】:

      【解决方案2】:

      感谢您的回答。 这是适配器的代码

       private class ExpandableListAdapter extends BaseExpandableListAdapter {
          private Context _context;
          private List<String> _listDataHeader;       
          private HashMap<String, List<String>> _listDataChild;
      
          public ExpandableListAdapter(Context context,
                  List<String> listDataHeader,
                  HashMap<String, List<String>> listChildData) {
              this._context = context;
              this._listDataHeader = listDataHeader;
              this._listDataChild = listChildData;
          }
      
          @Override
          public Object getChild(int groupPosition, int childPosititon) {
              return this._listDataChild.get(
                      this._listDataHeader.get(groupPosition))
                      .get(childPosititon);
          }
      
          @Override
          public long getChildId(int groupPosition, int childPosition) {
              return childPosition;
          }
      
          @SuppressLint("InflateParams")
          @Override
          public View getChildView(int groupPosition, final int childPosition,
                  boolean isLastChild, View convertView, ViewGroup parent) {
              String childText = (String) getChild(groupPosition, childPosition);
      
              if (convertView == null) {
                  LayoutInflater infalInflater = (LayoutInflater) this._context
                          .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                  convertView = infalInflater.inflate(R.layout.list_item, null);
              }
      
              CheckBox chckBx = (CheckBox) convertView.findViewById(R.id.chckBx);
              chckBx.setEnabled(false);
              if (allTageFlag) {
                  chckBx.setChecked(true);
              }
      
              TextView tv = (TextView) convertView.findViewById(R.id.tv);
              tv.setText(childText);      
      
              return convertView;
          }
      
          @Override
          public int getChildrenCount(int groupPosition) {
              return this._listDataChild.get(
                      this._listDataHeader.get(groupPosition)).size();
          }
      
          @Override
          public Object getGroup(int groupPosition) {
              return this._listDataHeader.get(groupPosition);
          }
      
          @Override
          public int getGroupCount() {
              return this._listDataHeader.size();
          }
      
          @Override
          public long getGroupId(int groupPosition) {
              return groupPosition;
          }
      
          @SuppressLint("InflateParams")
          @Override
          public View getGroupView(int groupPosition, boolean isExpanded,
                  View convertView, ViewGroup parent) {
              String headerTitle = (String) getGroup(groupPosition);
              if (convertView == null) {
                  LayoutInflater infalInflater = (LayoutInflater) this._context
                          .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                  convertView = infalInflater.inflate(R.layout.list_group, null);
              }
      
              TextView lblListHeader = (TextView) convertView
                      .findViewById(R.id.lblListHeader);
              lblListHeader.setTypeface(null, Typeface.BOLD);
              lblListHeader.setText(headerTitle);
      
              return convertView;
          }
      
          @Override
          public boolean hasStableIds() {
              return false;
          }
      
          @Override
          public boolean isChildSelectable(int groupPosition, int childPosition) {
              return true;
          }
      }
      

      这里是案例just paste.it的截图! . (我不能把它贴在这里:)) 当我选中复选框“Alla Tage”时,我想在可扩展列表视图中设置复选框

      【讨论】:

        【解决方案3】:

        我是这样解决的:

        if (chckbxAlleTage.isChecked()) {
        
                                for (int index = 0; index < childrenCount; index++ )
                                {
                                    if (listAdapter.isChildSelectable(0,index))
                                    {   
                                        CheckBox tempchckBx =  (CheckBox) (listAdapter.getChildView(
                                                0, index, false, expListView.getChildAt(index+1), expListView))
                                                .findViewById(R.id.chckBx);
                                        tempchckBx.setChecked(true);
                                    }
                                }       
                            }
        

        使用“getChildAt”允许我访问列表视图的每个孩子,然后选择每个孩子的复选框,然后将它们设置为 cheked。

        【讨论】:

          猜你喜欢
          • 2012-05-04
          • 2015-03-03
          • 2014-09-28
          • 2011-02-23
          • 2011-02-28
          • 2014-09-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多