Android - 无法刷新 ExpendableListView,它冻结

发布于 2024-12-09 14:26:52 字数 6795 浏览 0 评论 0原文

我有一个 ExpandableListView,如果我在活动的 onCreate() 期间添加一些组,那就没问题,但如果我尝试稍后添加组,然后执行 notificationDataSetChanged(),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);
        }

    }
}

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

    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

欢烬 2024-12-16 14:26:52

有几件事:

  1. 您不想让 TeamListAdapter 成为静态子类。
  2. 尝试将添加团队的逻辑放入 AsyncTask 中,因为这可能会冻结 UI 线程。

另外,你的日志说了什么?

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?

彼岸花ソ最美的依靠 2024-12-16 14:26:52

我通过在 addTeam() 中使用 runOnUiThread 解决了这个问题,并且工作完美。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文