Android - 无法刷新ExpendableListView,它冻结了

时间:2011-10-12 14:58:24

标签: android android-activity expandablelistview

我有一个ExpandableListView,如果我在活动的onCreate()期间添加一些组,那很好,但如果我稍后尝试添加组然后执行notifyDataSetChanged(),则ExpandableListView会冻结 - 活动中的其他所有内容工作,只是Exp.ListView挂起。

在这里,您可以看到我将两个组(带子项)添加到Exp.ListView,它们运行正常。如果我尝试调用addTeam()虽然它会冻结/挂起。

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);
        }

    }
}

2 个答案:

答案 0 :(得分:0)

一些事情:

  1. 您不希望将TeamListAdapter设为静态子类。
  2. 尝试将用于添加团队的逻辑放入AsyncTask中,因为这可能会冻结UI线程。
  3. 另外,你的日志说什么?

答案 1 :(得分:0)

我在我的runOnUiThread中使用addTeam()来解决这个问题,而且它完美无缺。