1

我有一个可扩展的列表视图。每个子行中有一个 TextView 和一个 CheckBox。当我单击一个复选框时,子项目会获得显示该项目已完成的删除效果。当一个组中的所有子项都被选中时,该组项也会获得删除效果。至少它应该。我可以通过长按某个项目并选择“设置完成”或使用手势打开上下文菜单来将子项目设置为完成。这两个过程都在持有者之外,所以我可以调用 refreshList() (见下面的代码)来重新创建列表。

假设我在一个小组中有两个孩子。我将两个孩子都设置为通过菜单或手势完成-> 组项目接收删除效果。好的。假设现在我通过单击复选框取消选中其中一个子项 -> 组项仍然具有删除效果,因为列表未刷新,因此 getGroupView 未激活。

简化代码:

public View getGroupView(int groupPosition, boolean isExpanded,
     View convertView, ViewGroup parent) {
    View view = convertView;
    if (view == null) {
     LayoutInflater inflater = (LayoutInflater) 
      context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     view = inflater.inflate(R.layout.groupitem, null);
    }
    TextView title = (TextView) view.findViewById(R.id.groupText);
    ImageView tick = (ImageView) view.findViewById(R.id.groupTick);

  //if sum of records in the finished column equals the number of records (e.g. 1+1+1 == 3)
   if ((grpfinsum == todoItemsgroupsFinished.size()) && (grpfinsum > 0))
      {
          title.setPaintFlags(title.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
          tick.setImageDrawable(tick.getResources().getDrawable(R.drawable.btn_finished));
          tick.setVisibility(View.VISIBLE);
      }
      else
      {
        title.setPaintFlags(title.getPaintFlags() & (~ Paint.STRIKE_THRU_TEXT_FLAG));
        tick.setVisibility(View.INVISIBLE);
      }
}



 public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
       final ViewHolder holder;
    if (convertView == null) 
    {
      LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      convertView = inflater.inflate(R.layout.childitem2, null);
      holder = new ViewHolder();
      holder.cTitle = (TextView) convertView.findViewById(R.id.child_title);
      holder.linlay = (LinearLayout) convertView.findViewById(R.id.childlayout);
      holder.cText = (TextView) convertView.findViewById(R.id.child_text);
      holder.cText2 = (TextView) convertView.findViewById(R.id.child_text2);
      holder.checkBox = (CheckBox) convertView.findViewById(R.id.multiple_checkbox);
      convertView.setTag(holder);
    } 
    else 
    {
     holder = (ViewHolder) convertView.getTag();
    }

holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

          if (isChecked)
          {
              holder.cTitle.setPaintFlags(holder.cTitle.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
              HotOrNot entry24 = new HotOrNot(GroceryListsInner2.this);
              entry24.open();
              entry24.modifyEntryInner(modifiedtablename.replaceAll(" ","_"), todoItemsgroupsID.get(childPosition), "1");
              entry24.close();
          }
          else
          {
              holder.cTitle.setPaintFlags(holder.cTitle.getPaintFlags() & (~ Paint.STRIKE_THRU_TEXT_FLAG));
              HotOrNot entry25 = new HotOrNot(GroceryListsInner2.this);
              entry25.open();
              entry25.modifyEntryInner(modifiedtablename.replaceAll(" ","_"), todoItemsgroupsID.get(childPosition), "0");
              entry25.close();
          }

            }
        }
    });

public void refreshList() {
             groupData.clear();
             childData.clear();
             groupCheckBox.clear();
             childCheckBox.clear();

             if (todoItemsgroups.size() > 0) todoItemsgroups.clear();
             if (todoItemsgroupsNote.size() > 0) todoItemsgroupsNote.clear();
             if (todoItemsgroupsFinished.size() > 0) todoItemsgroupsFinished.clear();
             HotOrNot entry = new HotOrNot(this);
                entry.open();
                Cursor c = entry.getAllTitlesFromInnerListGroupBy(originaltablename);
                if (c.moveToFirst())
                {
                do{
                    todoItemsgroups.add(c.getString(7));
                    todoItemsgroupsNote.add(c.getString(4));
                 }while (c.moveToNext());
                 }

                entry.close();

                max = 0;
                for (int i = 0; i < todoItemsgroups.size(); i++) 
                {
                     Map<String, String> curGroupMap = new HashMap<String, String>();
                     groupData.add(curGroupMap);
                     curGroupMap.put(G_TEXT, todoItemsgroups.get(i).replaceAll("_", " "));

                     List<Map<String, String>> children = new ArrayList<Map<String, String>>();
                     HotOrNot entry2 = new HotOrNot(this);
                     entry2.open();
                     Cursor c2 = entry2.getAllTitlesFromInnerListGroup(originaltablename, todoItemsgroups.get(i));
                     if (c2.moveToFirst())
                     {
                      do{
                           Map<String, String> curChildMap = new HashMap<String, String>();
                           children.add(curChildMap);
                           curChildMap.put(C_TITLE, c2.getString(1).replaceAll("_", " "));
                           curChildMap.put(C_TEXT, c2.getString(4));
                           curChildMap.put(C_TEXT2, c2.getString(6));
                        }while (c2.moveToNext());
                     }
                     entry2.close();

                     if (children.size() > max)
                     {
                         max = children.size();  
                     }

                     childData.add(children);
                 }

                 for ( int i = 0; i < todoItemsgroups.size(); i++) {

                        List<Map<String, Boolean>> childCB = new ArrayList<Map<String,Boolean>>();
                        for (int j = 0; j < max; j++) {
                            Map<String, Boolean> curCB = new HashMap<String, Boolean>();
                            childCB.add(curCB);
                            curCB.put(C_CB, false);
                        }
                        childCheckBox.add(childCB);
                  }
                   adapter2.notifyDataSetChanged();

                   adapter2 = new ExpandBaseAdapter2(GroceryListsInner2.this, groupData, childData, groupCheckBox, childCheckBox);
                   exList  = (ExpandableListView) findViewById(R.id.layoutExListView2);
                   exList.setAdapter(adapter2);
}

gestureDetector = new GestureDetector(new MyGestureDetector()); 
         gestureListener = new View.OnTouchListener() { 
             public boolean onTouch(View v, MotionEvent event) { 
                 if (gestureDetector.onTouchEvent(event)) { 

                    if (todoItemsgroupsID.size() > 0) todoItemsgroupsID.clear();

                    HotOrNot entry = new HotOrNot(GroceryListsInner2.this);
                    entry.open();
                    Cursor c = entry.getAllTitlesFromInnerListGroup(originaltablename, todoItemsgroups.get(groupPosition));
                    if (c.moveToFirst())
                    {
                    do{
                        todoItemsgroupsID.add(c.getString(0));
                     }while (c.moveToNext());
                    }
                    entry.close();


                    if (gestureDirection.equals("ltr"))
                    {
                        HotOrNot entryG = new HotOrNot(GroceryListsInner2.this);
                        entryG.open();
                        entryG.modifyEntryInner(modifiedtablename.replaceAll(" ","_"), todoItemsgroupsID.get(childPosition), "1");
                        entryG.close();
                        refreshList();
                    }
                    else 
                    {
                        HotOrNot entryG = new HotOrNot(GroceryListsInner2.this);
                        entryG.open();
                        entryG.modifyEntryInner(modifiedtablename.replaceAll(" ","_"), todoItemsgroupsID.get(childPosition), "0");
                        entryG.close();
                        refreshList();
                    }

                    return true; 
                 } 
                 return false; 
             } 
         };
4

0 回答 0