【问题标题】:Delete group in Expandable List删除可展开列表中的组
【发布时间】:2011-05-20 22:48:01
【问题描述】:

我正在尝试在我的可扩展列表中选择时删除组,并在删除发生后刷新列表。

我从 google 获取源代码:

http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/ExpandableList1.html

我尝试了几种方法来删除它,但都没有成功,有人已经做到了吗?

谢谢, 射线。

【问题讨论】:

    标签: android android-expandable-list-view


    【解决方案1】:

    第二个问题的答案(向子数据结构添加行)

    对于数据结构:

    public class GroupItem {
     private String mItemText;
     private List<ChildItem> mChildItems=new ArrayList<ChildItem>();    
    
     public GroupItem(String itemText) {
      mItemText=itemText;
     }
    
     public String getItemText() {
      return mItemText;
     }
    
     public ChildItem getChild(int childPosition) {
      return mChildItems.get(childPosition);
     }
    
     public void addChild(ChildItem childItem) {
      return mChildItems.add(childItem)
     }
    
     public void removeChild(int childPosition) {
      return mChildItems.remove(childPosition);
     }
    
    }
    
    public class ChildItem {
     private String mItemText;
     public ChildItem(itemText) {
      mItemText=itemText;
     }
    
     public String getItemText() {
      return mItemText;
     }
    }
    

    现在要设置它,您可以执行以下操作:

    List<GroupItem> items = new ArrayList<GroupItem>();
    
    GroupItem item1 = new GroupItem("This is group 1");
    item1.addChild(new ChildItem("This is a child item of group 1"));
    item1.addChild(new ChildItem("This is another child item of group 1"));
    

    等等……

    然后,您需要在适配器中返回适当的数据。

    关于行的问题: 在您的 Google 示例中,它们返回一个 TextView。但是,您可以使用您喜欢的任何内容制作自己的布局。例如:

    <ImageView android:id="@+id/RowIcon" 
      android:layout_width="56px" 
      android:layout_height="56px" 
      android:layout_marginLeft="12px" 
      android:layout_marginTop="2px">
    </ImageView>
    <TextView android:id="@+id/RowTextView"
         android:paddingLeft="10px"
         android:paddingTop="10px"       
         android:layout_width="wrap_content"
         android:layout_height="wrap_content" android:textColor="#666666" android:textSize="14px"/>
    
    
    </LinearLayout>
    

    然后在您的适配器中使用此布局。与其返回一个 TextView,不如将它作为一个 View 返回:

    public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
                View convertView, ViewGroup parent) {
    
    LayoutInflater mInflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = mInflater.inflate(YOUR XML FILE FROM ABOVE, null);
        ImageView rowIcon = (ImageView)rowView.findViewById(R.id.RowIcon);
        iconType.setBackgroundResource(AN IMAGE HERE);
        TextView rowText =(TextView)rowView.findViewById(R.id.RowTextView);
        textAddress.setText(items.get(groupPosition).getChild(childPosition));
    
        return rowView;
     }
    

    所以,我认为这应该让你继续前进。

    另外,如果我的回答让你满意,请接受。

    干杯。

    【讨论】:

    • 谢谢兄弟,你的回答很棒。告诉我一些事情,我正在尝试通过执行以下操作将视图切换到另一个视图:ExpandableList.this.setContentView(R.layout.errorscreen);,我得到了这个异常:ERROR/AndroidRuntime(23948): java.lang.RuntimeException:您的内容必须有一个 ExpandableListView,其 id 属性为 'android.R.id.list' 知道为什么吗?再次感谢。
    • 关于你刚才写的布局,你在哪里声明了变量“textAddress”?
    • 嘿。我从我从事的一些项目中得到了这个。 textAdress 只是一个我忘记更改的变量。重命名为 rowText :)。至于 RuntimeException: 完全按照它所说的去做 - 将 ExpandableListView 添加到您的 xml 布局并将其 id 设置为列表。像这样:
    • 但是我无法访问 ExpandableListView 布局,我从未制作过 xml,我猜它默认从某个地方获取它,您将如何调整它?
    • 您在扩展 ExpandableListActivity 的 Activity 中执行 ExpandableList.this.setContentView(R.layout.errorscreen) (我猜)。这样的活动需要一个包含 ExpandableListView 的布局。现在要么创建一个包含此元素的新布局(请参阅我上面的评论),要么将其添加到您现有的 errorscreen.xml
    【解决方案2】:

    将功能添加到您的适配器以删除项目:

        public void removeGroup(int group) {
            //TODO: Remove the according group. Dont forget to remove the children aswell!
            Log.v("Adapter", "Removing group"+group);
            notifyDataSetChanged();
        } 
    
        public void removeChild(int group, int child) {
            //TODO: Remove the according child
            Log.v("Adapter", "Removing child "+child+" in group "+group);
            notifyDataSetChanged();
        }
    

    通过更改确保您可以访问新方法:

    ExpandableListAdapter mAdapter;

    MyExpandableListAdapter mAdapter;

    在需要时调用方法:

     @Override
     public boolean onContextItemSelected(MenuItem item) {
        ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) item.getMenuInfo();
        String title = ((TextView) info.targetView).getText().toString();
    
        int type = ExpandableListView.getPackedPositionType(info.packedPosition);
        if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
            int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition); 
            int childPos = ExpandableListView.getPackedPositionChild(info.packedPosition); 
            mAdapter.removeChild(groupPos, childPos);
            return true;
        } else if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
            int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition); 
            mAdapter.removeGroup(groupPos);
            return true;
        }
    
        return false;
    }
    

    所以,希望对您有所帮助。干杯

    【讨论】:

    • 我试过了,但在那个代码中我无法访问它们(组、儿童),我只能在 MyExpandableListAdapter 中访问它们。我希望能够通过 onContextItemSelected 访问它们,因此当用户选择我时,我可以通过选择的项目将其删除
    • 向适配器添加方法 removeItem(long id) {}。在 onContextItemSelected 你做 ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) item.getMenuInfo(); mAdapter.remove(info.getId());
    • 这正是我试图做的,但我无法在 mAdapter 范围内找到 removeItem .. 因为它正在实现一个接口,并且没有在那里声明这个方法。你和我在同一个地方。 - 在我将该方法添加到适配器之后,mAdapter.removeItm 无法解析或不是字段。
    • 我认为你正在创建适配器的匿名类。创建一个扩展可扩展适配器的类。然后按照米特所说的添加方法,然后继续..
    • 啊,我明白了问题所在。您可以随时作弊并更改 ExpandableListAdapter mAdapter;到 MyExpandableListAdapter mAdapter;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-21
    相关资源
    最近更新 更多