【问题标题】:Android Studio: Adding new group/child to ExpandableListView messes up with the previous oneAndroid Studio:将新组/子添加到 ExpandableListView 与前一个混淆
【发布时间】:2019-03-19 21:27:41
【问题描述】:

我在 ExpandableListView 中添加新的 group 和 child 没有问题,但是之前 group 的 child添加新的组/孩子时,总是会发生变化。

在下图中,我添加了 group = DO ZERO 和 child = 123。 p>

现在,当我添加一个新的 group = CORRECAO FINAL 和 child = 987 时,前一组(DO ZERO)也发生了变化。

我不知道为什么会这样。

这是我实现对象的方式。

public class ExpandableObjectc {

    private String groupName;
    private ArrayList<ProductObject> productList;

    public ExpandableObjectc(String groupName, ArrayList<ProductObject> productList) {
        this.groupName = groupName;
        this.productList = productList;
    }

    public String getGroupName() {
        return groupName;
    }

    public void setGroupName(String groupName) {
        this.groupName = groupName;
    }

    public ArrayList<ProductObject> getProductList() {
        return productList;
    }

    public void setProductList(ArrayList<ProductObject> productList) {
        this.productList = productList;
    }
}

这是我实现适配器的方式

public class ExpandableAdapterc extends BaseExpandableListAdapter {

    private Context context;
    private ArrayList<ExpandableObjectc> groupList;
    private ArrayList<ExpandableObjectc> groupListFull;

    public ArrayList<ExpandableObjectc> getMParent() {
        return groupList;
    }

    public ExpandableAdapterc(Context context, ArrayList<ExpandableObjectc> groupList) {
        this.context = context;
        this.groupList = groupList;
        this.groupListFull = new ArrayList<>(groupList);
    }

    @Override
    public int getGroupCount() {
        return groupList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        ArrayList<ProductObject> productList = groupList.get(groupPosition).getProductList();
        return productList.size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groupList.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        ArrayList<ProductObject> productList = groupList.get(groupPosition).getProductList();
        return productList.get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ExpandableObjectc group = (ExpandableObjectc) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.expandable_group, null);
        }

        TextView mGroupName = (TextView) convertView.findViewById(R.id.expandable_groupName);
        //ImageView mArrow = (ImageView) convertView.findViewById(R.id.expandable_arrow);

        mGroupName.setText(group.getGroupName());

        return convertView;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ProductObject product = (ProductObject) getChild(groupPosition, childPosition);
        if (convertView == null) {
            LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.item_product, null);
        }

        CircleImageView mImage = (CircleImageView) convertView.findViewById(R.id.item_productImage);
        TextView mCode = (TextView) convertView.findViewById(R.id.item_productCode);
        TextView mAmount = (TextView) convertView.findViewById(R.id.item_productAmount);
        TextView mPrice = (TextView) convertView.findViewById(R.id.item_productPrice);
        TextView mDescription = (TextView) convertView.findViewById(R.id.item_productDescription);
        ImageButton mProductDelete = (ImageButton) convertView.findViewById(R.id.item_productDelete);
        ImageButton mProductEdit = (ImageButton) convertView.findViewById(R.id.item_productEdit);
        ToggleButton mFavorite = (ToggleButton) convertView.findViewById(R.id.item_productFavorite);

        if (product.getImage() != null) {
            byte[] prodIMG = product.getImage();
            Bitmap bitmap = BitmapFactory.decodeByteArray(prodIMG,0,prodIMG.length);
            mImage.setImageBitmap(bitmap);
            //Log.i("debinf prodadapter", "Private Image != null for "+productList.get(position).getDescription());
        } else {
            mImage.setImageResource(R.drawable.shopping_cart_black_48dp);
        }

        mCode.setText(product.getCode());
        mAmount.setText(product.getAmount());
        mPrice.setText(product.getPrice());
        mDescription.setText(product.getDescription());

        mProductDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                removeChildFromGroup(groupPosition, childPosition);
            }
        });

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    public void removeChildFromGroup(int groupPosition, int childPosition) {
        ExpandableObjectc group = (ExpandableObjectc) getGroup(groupPosition);
        group.getProductList().remove(childPosition);

        notifyDataSetChanged();
    }

    public void filterData(String query) {
        query = query.toLowerCase();
        groupList.clear();

        if (query.isEmpty()) {
            groupList.addAll(groupListFull);
        } else {
            for (ExpandableObjectc group : groupListFull) {
                ArrayList<ProductObject> productList = group.getProductList();
                ArrayList<ProductObject> newList = new ArrayList<>();
                for (ProductObject product : productList) {
                    if (product.getCode().toLowerCase().contains(query) || product.getDescription().toLowerCase().contains(query)) {
                        newList.add(product);
                    }
                }
                if (newList.size() > 0) {
                    ExpandableObjectc nGroup = new ExpandableObjectc(group.getGroupName(), newList);
                    groupList.add(nGroup);
                }
            }
        }
        notifyDataSetChanged();
    }
}

这是我如何在MainActivity 中添加新的group/child,其中groupName 来自上面第一张图片中显示的spinner。

private void writeProductOnDisk(String groupName) {
        boolean isGroup = false;

        ProductObject productObject = new ProductObject(imageViewToByte(mProductImage), mCode.getText().toString(), mAmount.getText().toString(), mPrice.getText().toString(), mDescription.getText().toString(),false);

        for (int i = 0; i < dataForExpandable.size(); i++) {
            if (groupName.equals(dataForExpandable.get(i).getGroupName())) {
                dataForExpandable.get(i).getProductList().add(productObject);
                isGroup = true;
                mProductListAdapter.notifyDataSetChanged();
                Log.i("debinf prodact", "groupName already exists "+ groupName);
                break;
            }
        }

        if (!isGroup) {
            productList.clear();
            productList.add(productObject);
            dataForExpandable.add(new ExpandableObjectc(groupName,productList));
            mProductListAdapter.notifyDataSetChanged();
            Log.i("debinf prodact", "groupName DO NOT exists, creating new one "+ groupName);
        }
    }

我不知道问题是发生在适配器中还是当我将新的element 添加到dataForExpandable 变量时。

感谢您的帮助!

编辑

我在添加新的 group/child 后添加了一个循环来打印 dataForExpandable 变量中的内容,我可以看到问题可能发生在添加方法dataForExpandable.add(new ExpandableObjectc(groupName,productList));,因为它似乎用新的group/child覆盖了之前的group。

它覆盖了我在groupName = Grupo Diamante 中添加的所有产品,并在groupName = boa gatolino 中添加了新的子项。

debinf prodact: groupName DO NOT exists, creating new one boa gatolino
debinf prodact: groupName is DO ZERO product is 123
debinf prodact: groupName is CORRECAO FINAL product is 987
debinf prodact: groupName is CORRECAO FINAL product is 111
debinf prodact: groupName is CORRECAO FINAL product is 223
debinf prodact: groupName is CORRECAO FINAL product is 741
debinf prodact: groupName is PARECE QUE AGORA FOI product is 555
debinf prodact: groupName is PARECE QUE AGORA FOI product is 369
debinf prodact: groupName is Grupo Diamante product is 357
debinf prodact: groupName is boa gatolino product is 357

添加新组/孩子的最佳方式是什么?

【问题讨论】:

  • 在 writeProductOnDisk() 中,if (!isGroup) { 之后,我认为您必须为 productList 而不是 productList.clear(); 创建一个新列表,因为它是为一个新组。实际上我看不到 productList 来自哪里,但我猜它指向最后一组。希望有帮助!
  • productList 变量首先用于从 SQLite 数据库加载数据。稍后,此变量用于在writeProductOnDisk() 中添加新的group**/**product。
  • 看来你是完全正确的。我已将 productList.clear() 替换为 productList = new ArrayList&lt;&gt;();。
  • @I_A_Mok 感谢您的解决方案,它确实有效。除此之外,我注意到当我添加一个新的 child 时,我的 adapter 底部的过滤器 filterData(String query) 没有过滤。当我搜索我已经知道它存在的产品时,ExpandableListView 变为空。你知道为什么会这样吗?
  • 适配器内部有 2 个列表:groupList 和 groupListFull。 writeProductOnDisk() 只更新 groupList 而 groupListFull 仍然是原始数据。此外,filterData(String query) 始终使用来自 groupListFull 的数据并将结果输出到 groupList。因此,调用filterData(String query) 后新添加的项目将被删除。

标签: android expandablelistview


【解决方案1】:

与其将项目添加到 dataForExpandable(Activity)/groupList(Adapter),不如将项目添加到 groupListFull(Adapter ) 并致电filterData() 更新groupList。所以尝试以下方法:

在适配器中添加这些方法:

public int getBackingListGroupCount() {
    return groupListFull.size();
}

public ExpandableObjectc getBackingListGroup(int groupPosition) {
    return groupListFull.get(groupPosition);
}

public void addBackingListGroup(ExpandableObjectc group) {
    groupListFull.add(group);
}

活动变化:

private void writeProductOnDisk(String groupName) {
    boolean isGroup = false;

    SearchView searchView = findViewById(R.id.SearchView);

    ProductObject productObject = new ProductObject(imageViewToByte(mProductImage), mCode.getText().toString(), mAmount.getText().toString(), mPrice.getText().toString(), mDescription.getText().toString(),false);

    for (int i = 0; i < mProductListAdapter.getBackingListGroupCount(); i++) {
        if (groupName.equals(mProductListAdapter.getBackingListGroup(i).getGroupName())) {
            mProductListAdapter.getBackingListGroup(i).getProductList().add(productObject);
            isGroup = true;
            mProductListAdapter.filterData(searchView.getQuery().toString());
            Log.i("debinf prodact", "groupName already exists "+ groupName);
            break;
        }
    }

    if (!isGroup) {
        productList = new ArrayList<>();
        productList.add(productObject);
        mProductListAdapter.addBackingListGroup(new ExpandableObjectc(groupName,productList));
        mProductListAdapter.filterData(searchView.getQuery().toString());
        Log.i("debinf prodact", "groupName DO NOT exists, creating new one "+ groupName);
    }
}

希望对你有帮助!

【讨论】:

  • 成功了!但我必须将项目添加到 dataForExpandable (Activity) / groupList (Adapter),因为添加后它不再更新我的 ExpandableListView。很好的解决方案!我们只需将项目添加到两个变量(groupList 和 groupListFull)
猜你喜欢
  • 2016-04-22
  • 1970-01-01
  • 1970-01-01
  • 2015-11-10
  • 2013-08-13
  • 1970-01-01
  • 2013-06-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多