BaseExpandableListAdapter convertview从getChildView()返回旧视图 [英] BaseExpandableListAdapter convertview returns old view from getChildView()

查看:56
本文介绍了BaseExpandableListAdapter convertview从getChildView()返回旧视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对BaseExpandableListAdapter概念感到困惑,下面解释了我的问题.

期望:

例如,我有两个小组,每个小组像下面的孩子一样拥有不同的布局

  • Group1 -ReceivedRequest:

    child1-来自Layout1

    child2-来自Layout1

  • Group2 -已接收已发送:

    child1-来自Layout2

    child2-来自Layout2

问题:

当我先单击 Group1 ,然后单击 Group2 时,它的工作非常完美(即,当第一次在各个组上进行首次调用时,convertview为空,因此可以正确放大),

但是当我先单击 Group2 然后单击 Group1 时,它仍返回旧的 Group2 视图...(即convertview应该为空,但其返回旧视图)

请告诉我问题出在哪里..我在Stackoverflow上的此处上找到了确切的问题,但还没有解决办法

下面是我的代码

  public View getChildView(int groupPosition,int childPosition,boolean isLastChild,View convertView,ViewGroup parent){SingleRow singleRow =新的SingleRow();RequestInfo Req_child =(RequestInfo)getChild(groupPosition,childPosition);字符串类型= Req_child.requestertype;if(convertView == null){开关(类型){案例RequestInfo.REQ_RECEIVED_ACCOUNT:LayoutInflater infalInflater =(LayoutInflater)上下文.getSystemService(Context.LAYOUT_INFLATER_SERVICE);convertView = infalInflater.inflate(R.layout.row_reqlist_acctrcvd,null);convertView.setTag(singleRow);休息;案例RequestInfo.REQ_SENTACCOUNT:LayoutInflater infalInflater1 =(LayoutInflater)上下文.getSystemService(Context.LAYOUT_INFLATER_SERVICE);convertView = infalInflater1.inflate(R.layout.row_reqlist_acctsent,null);convertView.setTag(singleRow);休息;}}singleRow =(SingleRow)convertView.getTag();开关(类型){案例RequestInfo.REQ_RECEIVED_ACCOUNT:singleRow.tvUsername_req_rcd =(TextView)convertView.findViewById(R.id.rw_reqaccts_rcvd_username);singleRow.tvNickName_req_rcd =(TextView)convertView.findViewById(R.id.rw_reqaccts_rcvd_nickname);singleRow.im_gender_req_rcd =(ImageView)convertView.findViewById(R.id.rw_reqaccts_rcvd_profpic);singleRow.btAcceptReq =(按钮)convertView.findViewById(R.id.rw_reqaccts_rcvd_accpt);singleRow.btRjctReq =(按钮)convertView.findViewById(R.id.rw_reqaccts_rcvd_rjct);singleRow.tvUsername_req_rcd.setText(Req_child.requesterUsername);singleRow.tvNickName_req_rcd.setText(Req_child.requesterNickName);singleRow.im_gender_req_rcd.setImageBitmap(Req_child.requestergender.equals("Male")?rmale_pic:rfem​​ale_pic);Log.i("Expand",您的子用户名是" + Req_child.requesterUsername);休息;案例RequestInfo.REQ_SENTACCOUNT:singleRow.tvUsername_req_sent =(TextView)convertView.findViewById(R.id.rw_reqaccts_sent_username);singleRow.tvNickName_req_sent =(TextView)convertView.findViewById(R.id.rw_reqaccts_sent_nickname);singleRow.im_gender_req_sent =(ImageView)convertView.findViewById(R.id.rw_reqaccts_sent_profpic);singleRow.btReqSent =(按钮)convertView.findViewById(R.id.rw_btn_sent_reqaccts);singleRow.tvUsername_req_sent.setText(Req_child.requesterUsername);singleRow.tvNickName_req_sent.setText(Req_child.requesterNickName);singleRow.im_gender_req_sent.setImageBitmap(Req_child.requestergender.equals("Male")?rmale_pic:rfem​​ale_pic);Log.i("Expand",您的子用户名是" + Req_child.requesterUsername);}返回convertView;} 

解决方案

我只是回答自己的问题,以便对其他人有帮助,我想手动覆盖该方法 getChildTypeCount() getChildType(int groupPosition,int childPosition),如果您使用不同的子布局,请在下面找到我的更新代码.

  @Overridepublic int getChildTypeCount(){返回2;}@Overridepublic int getChildType(int groupPosition,int childPosition){返回groupPosition;}@Override公共视图getChildView(int groupPosition,int childPosition,boolean isLastChild,View convertView,ViewGroup parent){SingleRow singleRow = null;RequestInfo Req_child = childtems.get(groupPosition).get(childPosition);字符串类型= Req_child.requestertype;if(convertView == null){开关(类型){案例RequestInfo.REQ_RECEIVED_ACCOUNT:singleRow =新的SingleRow();convertView = inflater.inflate(R.layout.row_reqlist_acctrcvd,null);singleRow.tvUsername_req_rcd =(TextView)convertView.findViewById(R.id.rw_reqaccts_rcvd_username);singleRow.tvNickName_req_rcd =(TextView)convertView.findViewById(R.id.rw_reqaccts_rcvd_nickname);singleRow.im_gender_req_rcd =(ImageView)convertView.findViewById(R.id.rw_reqaccts_rcvd_profpic);singleRow.btAcceptReq =(按钮)convertView.findViewById(R.id.rw_reqaccts_rcvd_accpt);singleRow.btRjctReq =(按钮)convertView.findViewById(R.id.rw_reqaccts_rcvd_rjct);convertView.setTag(singleRow);休息;案例RequestInfo.REQ_SENTACCOUNT:singleRow =新的SingleRow();convertView = inflater.inflate(R.layout.row_reqlist_acctsent,null);singleRow.tvUsername_req_sent =(TextView)convertView.findViewById(R.id.rw_reqaccts_sent_username);singleRow.tvNickName_req_sent =(TextView)convertView.findViewById(R.id.rw_reqaccts_sent_nickname);singleRow.im_gender_req_sent =(ImageView)convertView.findViewById(R.id.rw_reqaccts_sent_profpic);singleRow.btReqSent =(按钮)convertView.findViewById(R.id.rw_btn_sent_reqaccts);convertView.setTag(singleRow);休息;}}别的{singleRow =(SingleRow)convertView.getTag();}开关(类型){案例RequestInfo.REQ_RECEIVED_ACCOUNT:singleRow.tvUsername_req_rcd.setText(Req_child.requesterUsername);singleRow.tvNickName_req_rcd.setText(Req_child.requesterNickName);singleRow.im_gender_req_rcd.setImageBitmap(Req_child.requestergender.equals("Male")?rmale_pic:rfem​​ale_pic);休息;案例RequestInfo.REQ_SENTACCOUNT:singleRow.tvUsername_req_sent.setText(Req_child.requesterUsername);singleRow.tvNickName_req_sent.setText(Req_child.requesterNickName);singleRow.im_gender_req_sent.setImageBitmap(Req_child.requestergender.equals("Male")?rmale_pic:rfem​​ale_pic);}返回convertView;} 

I have confusion BaseExpandableListAdapter concept, explained my problem below.

Expectation:

For example I have two groups , each group holds different layout as child like below

  • Group1-ReceivedRequest:

    child1 - from Layout1

    child2 - from Layout1

  • Group2-ReceivedSent:

    child1 - from Layout2

    child2 - from Layout2

Problem:

When I click Group1 first and then Group2 its work perfect (i.e. convertview is null when calling for first time on respective group so its inflating right way),

But when I click Group2 first and then Group1 its still returning old Group2 view ...(i.e convertview should be null but its returning old view )

Kindly route me where is the issue.. I found exact ask here on Stackoverflow but there is no solution yet

Below is my code

    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

        SingleRow singleRow = new SingleRow();
        RequestInfo Req_child = (RequestInfo) getChild(groupPosition,childPosition);
        String type = Req_child.requestertype;
        if(convertView==null) {

            switch (type)
            {
                case RequestInfo.REQ_RECEIVED_ACCOUNT:

                    LayoutInflater infalInflater = (LayoutInflater) context
                            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    convertView = infalInflater.inflate(R.layout.row_reqlist_acctrcvd, null);
                    convertView.setTag( singleRow);
                    break;

                case RequestInfo.REQ_SENTACCOUNT:

                    LayoutInflater infalInflater1 = (LayoutInflater) context
                            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    convertView = infalInflater1.inflate(R.layout.row_reqlist_acctsent, null);
                    convertView.setTag(singleRow);
                    break;
                    }
        }

        singleRow = (SingleRow) convertView.getTag();
        switch (type)
        {
            case RequestInfo.REQ_RECEIVED_ACCOUNT:
                singleRow.tvUsername_req_rcd = (TextView) convertView.findViewById(R.id.rw_reqaccts_rcvd_username);
                singleRow.tvNickName_req_rcd = (TextView) convertView.findViewById(R.id.rw_reqaccts_rcvd_nickname);
                singleRow.im_gender_req_rcd = (ImageView) convertView.findViewById(R.id.rw_reqaccts_rcvd_profpic);
                singleRow.btAcceptReq = (Button) convertView.findViewById(R.id.rw_reqaccts_rcvd_accpt);
                singleRow.btRjctReq = (Button) convertView.findViewById(R.id.rw_reqaccts_rcvd_rjct);

                singleRow.tvUsername_req_rcd.setText(Req_child.requesterUsername);
                singleRow.tvNickName_req_rcd.setText(Req_child.requesterNickName);
                singleRow.im_gender_req_rcd.setImageBitmap(Req_child.requestergender.equals("Male") ? rmale_pic : rfemale_pic);
                Log.i("Expand", "your child username is " + Req_child.requesterUsername);

            break;

            case RequestInfo.REQ_SENTACCOUNT:
                singleRow.tvUsername_req_sent = (TextView) convertView.findViewById(R.id.rw_reqaccts_sent_username);
                singleRow.tvNickName_req_sent = (TextView) convertView.findViewById(R.id.rw_reqaccts_sent_nickname);
                singleRow.im_gender_req_sent = (ImageView) convertView.findViewById(R.id.rw_reqaccts_sent_profpic);
                singleRow.btReqSent = (Button) convertView.findViewById(R.id.rw_btn_sent_reqaccts);

                singleRow.tvUsername_req_sent.setText(Req_child.requesterUsername);
                singleRow.tvNickName_req_sent.setText(Req_child.requesterNickName);
                singleRow.im_gender_req_sent.setImageBitmap(Req_child.requestergender.equals("Male") ? rmale_pic : rfemale_pic);
                Log.i("Expand", "your child username is " + Req_child.requesterUsername);

        }
        return convertView;
    }

解决方案

I just answering my own question so that it will help others, I suppose to override the method manually getChildTypeCount() and getChildType(int groupPosition, int childPosition) incase you have different child layout, Find my below updated code.

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

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


@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
    SingleRow singleRow = null;
    RequestInfo Req_child = childtems.get(groupPosition).get(childPosition);
    String type = Req_child.requestertype;
    if(convertView==null ) {
        switch (type)
        {
            case RequestInfo.REQ_RECEIVED_ACCOUNT:
                 singleRow = new SingleRow();
                    convertView = inflater.inflate(R.layout.row_reqlist_acctrcvd, null);
                    singleRow.tvUsername_req_rcd = (TextView) convertView.findViewById(R.id.rw_reqaccts_rcvd_username);
                    singleRow.tvNickName_req_rcd = (TextView) convertView.findViewById(R.id.rw_reqaccts_rcvd_nickname);
                    singleRow.im_gender_req_rcd = (ImageView) convertView.findViewById(R.id.rw_reqaccts_rcvd_profpic);
                    singleRow.btAcceptReq = (Button) convertView.findViewById(R.id.rw_reqaccts_rcvd_accpt);
                    singleRow.btRjctReq = (Button) convertView.findViewById(R.id.rw_reqaccts_rcvd_rjct);
                    convertView.setTag(singleRow);
                break;

            case RequestInfo.REQ_SENTACCOUNT:
                 singleRow = new SingleRow();
                    convertView = inflater.inflate(R.layout.row_reqlist_acctsent, null);
                    singleRow.tvUsername_req_sent = (TextView) convertView.findViewById(R.id.rw_reqaccts_sent_username);
                    singleRow.tvNickName_req_sent = (TextView) convertView.findViewById(R.id.rw_reqaccts_sent_nickname);
                    singleRow.im_gender_req_sent = (ImageView) convertView.findViewById(R.id.rw_reqaccts_sent_profpic);
                    singleRow.btReqSent = (Button) convertView.findViewById(R.id.rw_btn_sent_reqaccts);
                    convertView.setTag(singleRow);
                break;
                }
    }
    else
    {
        singleRow = (SingleRow) convertView.getTag();
    }

    switch (type)
    {
        case RequestInfo.REQ_RECEIVED_ACCOUNT:
            singleRow.tvUsername_req_rcd.setText(Req_child.requesterUsername);
            singleRow.tvNickName_req_rcd.setText(Req_child.requesterNickName);
            singleRow.im_gender_req_rcd.setImageBitmap(Req_child.requestergender.equals("Male") ? rmale_pic : rfemale_pic);
        break;

        case RequestInfo.REQ_SENTACCOUNT:
            singleRow.tvUsername_req_sent.setText(Req_child.requesterUsername);
            singleRow.tvNickName_req_sent.setText(Req_child.requesterNickName);
            singleRow.im_gender_req_sent.setImageBitmap(Req_child.requestergender.equals("Male") ? rmale_pic : rfemale_pic);
    }
    return convertView;
}

这篇关于BaseExpandableListAdapter convertview从getChildView()返回旧视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆