2012-06-26 4 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

같아요. 그냥 linerlayout을 포함하는 treeitem.xml과 linearlayout을 가지고 treeview.xml을 포함하는 2 개의 레이아웃 파일이 있지만 이미지 뷰도 포함됩니다. 내가 잘못했으면 용서해주세요 - 그냥 초보자. –

답변

0

당신이 아이 뷰의 두 가지 유형이있는 경우, 당신은 당신의 어댑터에서 이러한 방법을 구현해야합니다

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 
:

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

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

는 그 다음 getChildView 방법을 다음과 같이 변경을

이것이 작동하는 방식을 이해하려면 this 비디오와 also을 보면서 시간을 보내십시오. ListView에 대해 설명 된 개념은 ExpandableListView에도 똑같이 적용됩니다.

+0

내가 잘못 생각한 것 같습니다. 실제로 달성하고 싶은 것은 3 개의 헤더가있는 확장 가능한 목록이고 여러 연락처는이 헤더 (온라인, 오프라인, 오프라인) 아래에 그룹화되지만 자녀는 일관되게 표시됩니다. 온라인 상태에 따라 내 목록의 다양한 그룹에 맞춤 개체 벡터를 삽입하는 빠른 방법은 무엇입니까? –

+0

혼란은 arraylist (아이들)가 어떻게 그룹에 연결되게합니까? –

+0

나는 당신이 그룹으로 키와 각 키에 대한 아이들의 목록으로지도를 사용해야 할 것 같아요. 'Map >와 유사합니다. – Jeshurun

관련 문제