Android ExpandableListView如何只加载一次数据

时间:2014-01-05 17:21:56

标签: android expandablelistview

我将一些数据从互联网加载到ExpandableListView,但如果我转向另一个活动并返回,则数据会再次从互联网加载。
如何加载数据一次?
我需要将数据保存在某处并从那里加载,我是对的吗? 如果是这样,怎么样?

我的适配器:

public class ExpandableAdapter extends BaseExpandableListAdapter {

    private List<Category> catList;
    private int itemLayoutId;
    private int groupLayoutId;
    private Context ctx;

    static class ViewHolder {
        public TextView team1;
        public TextView team2;
        public TextView score;
        public TextView time;
        public ImageView imgTeam1;
        public ImageView imgTeam2;
        public TextView groupName;
      }

    public ExpandableAdapter(List<Category> catList, Context ctx) {

        this.itemLayoutId = R.layout.child_view;
        this.groupLayoutId = R.layout.group_view;
        this.catList = catList;
        this.ctx = ctx;
    }

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

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return catList.get(groupPosition).getItemList().get(childPosition).hashCode();
    }
    ViewHolder viewHolder;
    @Override
    public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {      
        View v = convertView;       
        if (v == null) {
            LayoutInflater inflater = (LayoutInflater)ctx.getSystemService
                      (Context.LAYOUT_INFLATER_SERVICE);
            v = inflater.inflate(R.layout.child_view, parent, false);
        viewHolder = new ViewHolder();
        viewHolder.team1 = (TextView) v.findViewById(R.id.team1);
        viewHolder.team2 = (TextView) v.findViewById(R.id.team2);
        viewHolder.score = (TextView) v.findViewById(R.id.score);
        viewHolder.time = (TextView) v.findViewById(R.id.time);
        viewHolder.imgTeam1 = (ImageView) v.findViewById(R.id.team1Img);
        viewHolder.imgTeam2 = (ImageView) v.findViewById(R.id.team2Img);        
          v.setTag(viewHolder);}
        else {
            viewHolder = (ViewHolder) v.getTag();
            }
        ItemDetail det = catList.get(groupPosition).getItemList().get(childPosition);
        viewHolder.team1.setText(det.getTeam1());
        viewHolder.team2.setText(det.getTeam2());
        viewHolder.score.setText(det.getScore());
        viewHolder.time.setText(det.getTime());
        viewHolder.imgTeam1.setImageResource(det.getImgTeam1());
        viewHolder.imgTeam2.setImageResource(det.getImgTeam2());                
        return v;       
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        int size = catList.get(groupPosition).getItemList().size();
        System.out.println("Child for group ["+groupPosition+"] is ["+size+"]");
        return size;
    }

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

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

    @Override
    public long getGroupId(int groupPosition) {
        return catList.get(groupPosition).hashCode();
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null) {
            LayoutInflater inflater = (LayoutInflater)ctx.getSystemService
                      (Context.LAYOUT_INFLATER_SERVICE);
            v = inflater.inflate(R.layout.group_view, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.groupName = (TextView) v.findViewById(R.id.groupName);   
            v.setTag(viewHolder);
        }
        else {
            viewHolder = (ViewHolder) v.getTag();
        }
        Category cat = catList.get(groupPosition);
        viewHolder.groupName.setText(cat.getName());
        return v;

    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}

1 个答案:

答案 0 :(得分:0)

将值对象放在活动之外,例如在一些持有永久对象的单例类中