【问题标题】:Android, Expandable List View Delete child on Button clickAndroid,可扩展列表视图在按钮单击时删除子项
【发布时间】:2016-02-20 05:53:19
【问题描述】:

我已经实现了一个可扩展列表视图,在 Android 中,您可以在屏幕截图中看到,它工作正常,并且在可扩展列表中,我有多个带有删除选项的子组,正如您在屏幕截图中看到的那样,我想要要删除用户选择删除的特定孩子,我无法删除子表单可扩展列表,我将发布我的代码,请任何人指导我

    public class PendingFragment extends Fragment {

    private AnimatedExpandableListView listView;
    private PendingAdapter adapter;
    CoordinatorLayout coordinatorLayout;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_pending, container, false);

        coordinatorLayout = (CoordinatorLayout)view.findViewById(R.id.pending_frag_coordinatorLayout);

        List<GroupItem> items = new ArrayList<GroupItem>();

        // Populate our list with groups and it's children
        for(int i = 1; i < 10; i++) {
            GroupItem item = new GroupItem();

            item.title = "GroupItem " + i;

            for(int j = 0; j < i; j++) {
                ChildItem child = new ChildItem();
                child.title = "ChildItem " + j;

                item.items.add(child);
            }

            items.add(item);
        }

        adapter = new PendingAdapter(getActivity());
        adapter.setData(items);

        listView = (AnimatedExpandableListView)view.findViewById(R.id.pending_explistView);
        listView.setAdapter(adapter);


        return view;
    }

}

我的列表视图适配器

    public class PendingAdapter extends AnimatedExpandableListView.AnimatedExpandableListAdapter {

    private LayoutInflater inflater;

    private List<GroupItem> items;

    ListPopupWindow listPopupWindow;
    String[] products = {"Delete"};
    Context contexts;

    public PendingAdapter(Context context) {
        inflater = LayoutInflater.from(context);
        this.contexts = context;
    }

    public void setData(List<GroupItem> items) {
        this.items = items;
    }

    private class ChildHolder {
        TextView title;
        ImageView option_menu;
    }

    private class GroupHolder {
        TextView title;
    }
    @Override
    public ChildItem getChild(int groupPosition, int childPosition) {
        return items.get(groupPosition).items.get(childPosition);
    }

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

    @Override
    public View getRealChildView(final int groupPosition,final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildHolder holder;
        ChildItem item = getChild(groupPosition, childPosition);
        Log.e("groupPosition=" + groupPosition, "childPosition=" + childPosition);
        if (convertView == null) {
            holder = new ChildHolder();
            convertView = inflater.inflate(R.layout.expchildlistview, parent, false);
            holder.title = (TextView) convertView.findViewById(R.id.txtListChild);
            holder.option_menu = (ImageView) convertView.findViewById(R.id.checkin_option_menu);
            convertView.setTag(holder);

        } else {
            holder = (ChildHolder) convertView.getTag();
        }

        holder.title.setText(item.title);
        holder.option_menu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ShowListMenu(view,groupPosition, childPosition);
            }
        });

        return convertView;
    }

    public void ShowListMenu(View v, final int childPosition, final int groupPosition){
        listPopupWindow = new ListPopupWindow(contexts);
        listPopupWindow.setAdapter(new ArrayAdapter(contexts, android.R.layout.simple_list_item_1, products));
        listPopupWindow.setAnchorView(v);
        if (Utils.device_width(contexts) >= 320 && Utils.device_width(contexts) < 480) {
            //Log.v(">= 320 < 480=", ">= 320 < 480");
            listPopupWindow.setWidth(150);
        }else if (Utils.device_width(contexts) >= 480 && Utils.device_width(contexts) < 500) {
            //Log.v(">= 480=", "< 500");
            listPopupWindow.setWidth(150);
        }else{
            listPopupWindow.setWidth(200);
        }
        listPopupWindow.setModal(true);
        listPopupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                Log.v("item.remove=", "" + items.get(groupPosition).items.get(childPosition));
items.get(groupPosition).items.remove(childPosition);
notifyDataSetChanged();
                listPopupWindow.dismiss();

            }
        });
        listPopupWindow.show();
    }

    @Override
    public int getRealChildrenCount(int groupPosition) {
        return items.get(groupPosition).items.size();
    }

    @Override
    public GroupItem getGroup(int groupPosition) {
        return items.get(groupPosition);
    }

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

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

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupHolder holder;
        GroupItem item = getGroup(groupPosition);
        if (convertView == null) {
            holder = new GroupHolder();
            convertView = inflater.inflate(R.layout.explist_group, parent, false);
            holder.title = (TextView) convertView.findViewById(R.id.lblListHeader);
            convertView.setTag(holder);
        } else {
            holder = (GroupHolder) convertView.getTag();
        }

        holder.title.setText(item.title);

        return convertView;
    }

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

    @Override
    public boolean isChildSelectable(int arg0, int arg1) {
        return true;
    }

}
public class GroupItem {

    public String title;
    public List<ChildItem> items = new ArrayList<ChildItem>();

}
public class ChildItem {
    public String title;
}

【问题讨论】:

  • remove child 后有没有尝试过 notifydatasetchange() 方法?
  • 我已经尝试过,请查看我编辑的问题,当我尝试从第 0 组中删除第一个孩子时,它工作正常,但是当我尝试从第 2 组中删除孩子时,我得到了 ArrayindexOut 异常

标签: java android listview expandablelistview


【解决方案1】:

点击删除时看不到 items.remove(child)。我认为您应该尝试从列表中删除孩子,而不是使用更新的值设置适配器。 @阿钦

【讨论】:

  • 这不是你的答案,如果你知道答案,那就不要回答,然后写代码..
  • 我已经尝试过,请查看我编辑的问题,当我尝试从第 0 组中删除第一个孩子时,它工作正常,但是当我尝试从第 2 组中删除孩子时,我得到了 ArrayindexOut 异常
  • Sir Ravi VGHL 我知道这不是我的答案,仅供参考,我的声誉不像 497,因为 Stackoverflow 的默认规则和规定你不能对声誉低于 50 的问题发表评论。我希望你明白了!
  • 请检查我编辑的问题,使用 items.get(groupPosition).items.remove(childPosition); notifyDataSetChanged();
  • 在此之后,当我尝试从第二组中删除孩子时,我得到了 ArrayindexOut 异常
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-12
  • 2012-04-10
  • 2023-03-12
  • 2021-08-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多