2010-12-06 34 views

回答

3

回答第二個問題(行添加到一個孩子&數據結構)

對於數據結構的:

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; 
} 

所以,我認爲這應該讓你去。

另外,如果他們滿足你,請接受我的答案。

乾杯。

1

添加功能,以您的適配器刪除一個項目:

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; 
} 

所以,希望幫助。乾杯

+0

我試過了,但是在那段代碼中我不能訪問它們(組,子組),我只能在MyExpandableListAdapter中訪問它們。我希望能夠通過onContextItemSelected訪問它們,所以當選擇我的用戶可以通過選擇的項目刪除它時 – rayman 2010-12-06 12:17:14

+0

向適配器添加一個removeItem(long id){}方法。在onContextItemSelected中,執行ExpandableListContextMenuInfo info =(ExpandableListContextMenuInfo)item.getMenuInfo(); mAdapter.remove(info.getId()); – metter 2010-12-06 12:19:44