开发者

Android - cannot refresh ExpendableListView, it freezes

开发者 https://www.devze.com 2023-04-12 18:19 出处:网络
I have an ExpandableListView in which if I add some Groups during onCreate() of the activity, it\'s fine, but if I try to add Groups later on and then do notifyDataSetChanged() the ExpandableListView

I have an ExpandableListView in which if I add some Groups during onCreate() of the activity, it's fine, but if I try to add Groups later on and then do notifyDataSetChanged() the ExpandableListView just freezes - everything else in the activity works, just the Exp.ListView hangs.

Here as you can see I add two Groups (with children) to the Exp.ListView and they function just fine. If I try to call addTeam() though it freezes/hangs.

package org.mytest;

import java.util.ArrayList;

import eigc.doubango.ScreenTeam.ScreenTeamItemGroup;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.ImageButton;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.ImageView;
import android.widget.TextView;

public class ScreenTeam extends Activity {
private TeamListAdapter mTeamListAdapter;
private ExpandableListView mTeamList;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.screen_team);

        mTeamListAdapter = new TeamListAdapter(getLayoutInflater());
        mTeamList =  (ExpandableListView) findViewById(R.id.screen_team_teamslist);
        mTeamList.setAdapter(mTeamListAdapter);
        mTeamList.setGroupIndicator(null);




        /* TEST */

        ScreenTeamItemGroup grp1 = new ScreenTeamItemGroup("team1",1);
        ScreenTeamItemChild ch1 = new ScreenTeamItemChild("child1");
        ScreenTeamItemChild ch2 = new ScreenTeamItemChild("child2");
        grp1.addTeamMember(ch1);
        grp1.addTeamMember(ch2);

        ScreenTeamItemGroup grp2 = new ScreenTeamItemGroup("team2",2);
        ScreenTeamItemChild ch3 = new ScreenTeamItemChild("child1");
        ScreenTeamItemChild ch4 = new ScreenTeamItemChild("child2");
        ScreenTeamItemChild ch5 = new ScreenTeamItemChild("child3");
        grp2.addTeamMember(ch3);
        grp2.addTeamMember(ch4);
        grp2.addTeamMember(ch5);

        mTeamListAdapter.addTeam(grp1);
        mTeamListAdapter.addTeam(grp2);
    }



    /*
     * Callbacks
     */

    /* adds a team */
    public void addTeam() {
        ScreenTeamItemGroup grp1 = new ScreenTeamItemGroup("team1",1);
        ScreenTeamItemChild ch1 = new ScreenTeamItemChild("child1");
        ScreenTeamItemChild ch2 = new ScreenTeamItemChild("child2");
        grp1.addTeamMember(ch1);
        grp1.addTeamMember(ch2);
        mTeamListAdapter.addTeam(grp1);
        mTeamListAdapter.notifyDataSetChanged();
    }





    /*
     * Aux classes
     */
    /* team group item */
    static class ScreenTeamItemGroup {
        final String mItemText;
        final int mIconResId;
        final int mTeamID;
        public ArrayList<ScreenTeamItemChild> mTeamMembers; 

        public ScreenTeamItemGroup(String itemText, int id) {
            mItemText = itemText;
            mTeamID = id;
            mIconResId = R.drawable.teams;
            mTeamMembers = new ArrayList<ScreenTeamItemChild>();
        }

        public void addTeamMember(ScreenTeamItemChild member) {
            if (member != null)
                mTeamMembers.add(member);
        }开发者_如何学运维
    }

    /* team child item */
    static class ScreenTeamItemChild {
        final int mIconResId;
        final String mItemText;

        public ScreenTeamItemChild(String itemText) {
            mIconResId = R.drawable.p2p;
            mItemText = itemText;
        }
    }

    /* handles the list of teams */
    static class TeamListAdapter extends BaseExpandableListAdapter {
        public ArrayList<ScreenTeamItemGroup> mTeams;
        private final LayoutInflater mInflater;

        public TeamListAdapter(LayoutInflater inflater) {
            mInflater = inflater;
            mTeams = new ArrayList<ScreenTeamItemGroup>();
        }

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

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

        @Override
        public View getChildView(int groupPosition, int childPosition,
                boolean isLastChild, View convertView, ViewGroup parent) {
            View view = convertView;
            final ScreenTeamItemChild item = (ScreenTeamItemChild)getChild(groupPosition,childPosition);

            if(item == null){
                return null;
            }
            if (view == null) {
                view = mInflater.inflate(R.layout.screen_team_item, null);
            }
            ((TextView) view.findViewById(R.id.screen_team_item_text)).setText(item.mItemText);
            ((ImageView) view.findViewById(R.id.screen_team_item_icon)).setImageResource(item.mIconResId);

            return view;
        }

        @Override
        public int getChildrenCount(int groupPosition) {
            return mTeams.get(groupPosition).mTeamMembers.size();
        }

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

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

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

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

            if(item == null){
                return null;
            }
            if (view == null) {
                view = mInflater.inflate(R.layout.screen_team_item, null);
            }
            ((TextView) view.findViewById(R.id.screen_team_item_text)).setText(item.mItemText);
            ((ImageView) view.findViewById(R.id.screen_team_item_icon)).setImageResource(item.mIconResId);
            return view;
        }

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

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

        public void addTeam(ScreenTeamItemGroup grp) {
            if (grp != null)
                mTeams.add(grp);
        }

    }
}


A few things:

  1. You don't want to make your TeamListAdapter a static subclass.
  2. Try putting the logic for adding a team into an AsyncTask as this could freeze up the UI thread.

Also, what are your logs saying?


I solved this by using runOnUiThread in my addTeam() instead and it worked flawlessly.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号