0

我想用自定义适配器创建一个 ExpandableListView,但是它只显示我的标题组,当我单击标题(查看子项)时它什么也没做(我只想有一个组项,因此项组计数是 1)。

请帮我找出我错在哪里,非常感谢。

这是我的适配器:

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 个 List 不是空的。

4

1 回答 1

0

阅读您的代码,我发现 3 点对我来说不是很清楚:

  1. getGroup 你返回null?你为什么不返回真正的对象
  2. getChildId 你应该返回一个唯一标识你的孩子
  3. hasStableIds。你返回假。您不需要重写此方法。

如果您想了解有关 ExpandableListView 的更多信息,请在此处查看我的博客

您使用 ExpandableListView 就像一个简单的 ListView 为什么不考虑给您一个 ListView?

于 2013-04-10T09:03:06.067 回答