2012-06-26 76 views
0

我是新來的Android和我努力做到以下使用ExpandableListView如何使動態ExpandableListView

我想有3組 - 「在線」,「離線」和「離開」。這些組將包含將從互聯網加載的兒童自定義對象(聯繫人)。這些對象一次只能在一個組中(儘管稍後可能會更改)。

我自己嘗試過這個,沒有尋求幫助,我會粘貼代碼 - 它讓我用正確的組顯示列表視圖,但孩子們在所有組中。

在代碼中,聯繫人項目目前只是在另一個類的向量。

我真的很感謝一些幫助,因爲我真的被卡住了。

public class MyTreeAdaptor extends BaseExpandableListAdapter{ 
@Override 
public boolean areAllItemsEnabled() 
{ 
    return true; 
} 


private Context context; 
private ArrayList<String> groups; 
private ArrayList<ArrayList<Contact>> children; 
public MyTreeAdaptor(Context context, ArrayList<String> groups, 
     ArrayList<ArrayList<Contact>> children) { 
    this.context = context; 
    this.groups = groups; 
    this.children = children; 
} 


/** 
* A general add method, that allows you to add a Contact to this list 
* 
* Depending on if the category opf the Contact is present or not, 
* the corresponding item will either be added to an existing group if it 
* exists, else the group will be created and then the item will be added 
* @param Contact 
*/ 
public void addItem(Contact aContact) { 
    try { 
     Log.e("additem", ""+aContact.show); 
     if (!groups.contains(aContact.show)) { 
      groups.add(aContact.show); 
     } 
     int index = groups.indexOf(aContact.show); 
     if (children.size() < index + 1) { 
      children.add(new ArrayList<Contact>()); 
     } 
     children.get(index).add(aContact); 
    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     Log.e("additem error", " "+e); 
    } 
} 


@Override 
public Object getChild(int groupPosition, int childPosition) { 
    return children.get(groupPosition).get(childPosition); 
} 


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

// Return a child view. You can load your custom layout here. 
@Override 
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, 
     View convertView, ViewGroup parent) { 
    try { 
     Contact contact = (Contact) getChild(groupPosition, childPosition); 
     if (convertView == null) { 
      LayoutInflater infalInflater = (LayoutInflater) context 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = infalInflater.inflate(R.layout.treeview, null); 
     } 
     TextView tv = (TextView) convertView.findViewById(R.id.text1); 
     try { 
      tv.setText(" " + contact.name); 
      tv.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0); 
     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 


     // Depending upon the child type, set the imageTextView01 

     /*if (Contact instanceof Car) { 
      tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.car, 0, 0, 0); 
     } else if (Contact instanceof Bus) { 
      tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.bus, 0, 0, 0); 
     } else if (Contact instanceof Bike) { 
      tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.bike, 0, 0, 0); 
     }*/ 
     return convertView; 
    } catch (Exception e) { 
     Log.e("error thr", " "+e); 
     e.printStackTrace(); 
     return null; 
    } 
} 


@Override 
public int getChildrenCount(int groupPosition) { 
    try 
    { 
    return children.get(groupPosition).size(); 
    } 
    catch(Exception hh) 
    { 
     return 0; 

    } 
} 


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


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


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


// Return a group view. You can load your custom layout here. 
@Override 
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, 
     ViewGroup parent) { 
    try { 
     Log.e(groupPosition+"groups size is", " "+groups.size()); 
     String group; 
     if(groups.size() > 0) 
     { 
      group = (String) getGroup(groupPosition); 
     } 
     else 
     { 
      group = "null";//(String) getGroup(groupPosition); 
     } 

     if (convertView == null) { 
      LayoutInflater infalInflater = (LayoutInflater) context 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = infalInflater.inflate(R.layout.treeview, null); 
     } 
     TextView tv = (TextView) convertView.findViewById(R.id.text1); 
     try { 
      tv.setText(group); 
     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     return convertView; 
    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     return null; 
    } 
} 


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


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

孩子的意見結構相同或不同? – Jeshurun

+0

同樣我想 - 我只有2個佈局文件treeitem.xml包含一個linerlayout和treeview.xml,它也有一個linearlayout,但也包含一個imageview。原諒我,如果我實施了這個錯誤 - 只是一個新手。 –

回答

0

如果您有兩種不同類型的子視圖,你將不得不實現你的適配器這些方法:

@Override 
public int getChildTypeCount() { 
    return 2; 
} 

@Override 
public int getChildType(int groupPosition, int childPosition) { 
    return groupPosition == 4 ? 1 : 0; 
} 

然後進行以下更改getChildView方法:

LayoutInflater layoutInflater = (LayoutInflater) mContext 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
View v = convertView; 
if(v == null) { 
    if (groupPosition == 4) { 
     v = layoutInflater.inflate(R.layout.treeitem, null); 
    } else { 
     v = layoutInflater.inflate(R.layout.treeview, null); 
} 


TextView tt = null; 
if (groupPosition != 4) { 
     tt = (TextView) v.findViewById(R.id.text1); 
// rest of the method 

要理解它是如何工作的,花一些時間看this視頻和also這個答案的SO。爲ListView解釋的概念同樣適用於ExpandableListView。

+0

我想我可能會被誤解 - 我實際上想要實現的是一個可擴展的列表,其中有3個標題,各種聯繫人將被分組在這些標題下(在線,離開,離線),但我的孩子將一直顯示。任何快速的方式來插入一個自定義對象的矢量到我的列表的各個組,取決於說他們的在線狀態? –

+0

我的困惑是ArrayList(子女)如何鏈接到組? –

+0

我猜你將不得不使用一個地圖組作爲鍵和每個鍵的兒童列表。像'Map >'。 – Jeshurun