2013-04-10 47 views
0

我想用自定義適配器創建一個ExpandableListView,但它只顯示我的標題組,並且當我點擊標題(查看子項)時它不執行任何操作 (我想只有一個組項目,因此項目組計數爲1)。ExpandableListView與適配器

請幫我找到我錯在哪裏,非常感謝。

這是我的適配器:

class chatListAdapter extends BaseExpandableListAdapter { 
private String objectsIdInsideAdapter; 
private List<String> commentsInsideAdapter = new ArrayList<String>(); 
private List<String> FacebookIdInsideAdapter=new ArrayList<String>(); 
private List<String> NamesInsideAdapter =new ArrayList<String>(); 
private Context mContext; 

chatListAdapter(String objectId,Context ctx,List<String> comments,List<String> face,List<String> names){ 
    objectsIdInsideAdapter=objectId; 
    mContext=ctx; 
    commentsInsideAdapter=comments; 
    FacebookIdInsideAdapter=face; 
    NamesInsideAdapter=names; 
} 
@Override 
public Object getChild(int arg0, int arg1) { 
    // TODO Auto-generated method stub 
    return NamesInsideAdapter.get(arg1); 
} 

@Override 
public long getChildId(int arg0, int arg1) { 
    // TODO Auto-generated method stub 
    return 0; 
} 

@Override 
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View v, 
     ViewGroup parent) { 
    // TODO Auto-generated method stub 

    LayoutInflater inflater = getLayoutInflater(); 


    if(v==null){ 
     v = inflater.inflate(R.layout.chat_item, parent, false); 
    } 



    String comment =commentsInsideAdapter.get(position); 
    Button sendCommentBtn =(Button) v.findViewById(R.id.chatitemSendComment); 
     TextView commentView= (TextView) v.findViewById(R.id.chateitemViewComment); 
     commentView.setText(comment); 
     TextView commentName =(TextView) v.findViewById(R.id.chatitemfreindName); 
     commentName.setText(NamesInsideAdapter.get(position)); 
     EditText commentText= (EditText) v.findViewById(R.id.chatitemEnterComment); 
     commentText.setVisibility(View.GONE); 
     sendCommentBtn.setVisibility(View.GONE); 




    return v; 
} 

@Override 
public int getChildrenCount(int arg0) { 
    // TODO Auto-generated method stub 
    return NamesInsideAdapter.size(); 
} 

@Override 
public Object getGroup(int arg0) { 
    // TODO Auto-generated method stub 
    return null; 
} 

@Override 
public int getGroupCount() { 
    // TODO Auto-generated method stub 
    return 1; 
} 

@Override 
public long getGroupId(int arg0) { 
    // TODO Auto-generated method stub 
    return 0; 
} 

@Override 
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, 
     ViewGroup parent) { 
    // TODO Auto-generated method stub 
    View v = convertView; 

    if (v == null) { 
     LayoutInflater inflater = getLayoutInflater(); 
     v = inflater.inflate(R.layout.chat_item_group_layout,parent, false); 
    } 

    TextView groupName = (TextView) v.findViewById(R.id.groupName); 
    TextView groupDescr = (TextView) v.findViewById(R.id.groupDescr); 

    groupName.setText("view comments"); 
    groupDescr.setText("groupDSCR"); 


    return v; 

} 

@Override 
public boolean hasStableIds() { 
    // TODO Auto-generated method stub 
    return false; 
} 

@Override 
public boolean isChildSelectable(int arg0, int arg1) { 
    // TODO Auto-generated method stub 
    return false; 
} 

,這是主要的活動代碼:

ExpandableListView chat =(ExpandableListView) v.findViewById(R.id.ExpList); 

    String faceids =facebookIdChatInside.get(position); 
    String comments = commentInside.get(position); 
    String namesChat =nameChatInside.get(position); 
    List<String> chatItems = new ArrayList<String>(Arrays.asList(comments.split(","))); 
    List<String> facebookids = new ArrayList<String>(Arrays.asList(faceids.split(","))); 
    List<String> namesList = new ArrayList<String>(Arrays.asList(namesChat.split(","))); 
    chatItems.add(" "); 
    facebookids.add(id); 
    namesList.add(name); 


    chatListAdapter chatA =new chatListAdapter(objectsId.get(position),profileContext,chatItems,facebookids,namesList); 
    chat.setAdapter(chatA); 

我檢查,而那些3名列表是不是空的。

回答

0

閱讀你的代碼,我發現3個點是不那麼清楚,我:

  1. getGroup你返回null?爲什麼不返回真實對象
  2. getChildId你應該返回一個唯一的ID標識你的孩子
  3. hasStableIds。你回覆錯誤。你不需要重寫這個方法。

如果你想有ExpandableListView更多信息給看看我的博客here

您使用的是像一個簡單的ListView中ExpandableListView爲什麼不ü考慮到你一個ListView?

+0

嗨,我不想使用listView,我也看看這個指南和getChildId和getGroup方法返回保存值作爲我的代碼:http://androidtrainningcenter.blogspot.in/2012/07/android-expandable- listview-simple.html – 2013-04-10 14:57:50

+0

嘗試替換getInflater()寬度LayoutInflater inflater =(LayoutInflater)ctx.getSystemService (Context.LAYOUT_INFLATER_SERVICE); – FrancescoAzzola 2013-04-10 15:14:36

+0

嗨,仍然只顯示我的組標題「查看評論,groupDSCR」,當我點擊它,它不打開列表...請別人幫我:) – 2013-04-10 15:25:28