如何动态更新Android可扩展列表数据?

时间:2014-01-27 08:53:03

标签: java android expandablelistview expandablelistadapter robospice

我在片段中有一个ExpandableListView。 我正在使用RoboSpice onCreateView向服务器发布请求。 我希望能够两次更新列表,一次是从缓存中获取数据,另一次是在响应返回后。 由于某种原因,列表未填充。 当我实例化一个新的适配器并将适配器设置为列表时,我能够填充它,但由于某种原因它在某些Android设备上不起作用。

这是我正在使用的代码:

片段

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    _expandableListAdapter = new GroupsExpandableListAdapter(getActivity(), getSpiceManager());
    _groupListView.setAdapter(_expandableListAdapter);
    DBServerHelper.Groups.getByBusinessId(getActivity(), getSpiceManager(), getDBHelper(), _clientBusiness.getId(),
        new CachedRequestListener<ClientGroup.List>(getDBHelper(), getActivity()) {
            @Override
            // This method runs twice, after the data comes from the cache and once the request succeed
            public void onRequestFromCache(ClientGroup.List clientGroups) {
                if (clientGroups == null || clientGroups.isEmpty()) {
                    Ln.d("Received clientGroups " + clientGroups == null ? "null" : "empty" + " form request GetByBusinessId");
                    return;
                }

                _expandableListAdapter.clear();
                _expandableListAdapter.setGroups(clientGroups);
                _expandableListAdapter.notifyDataSetChanged();
                _groupListView.invalidateViews();
            }

            @Override
            public void onRequestFailure(SpiceException spiceException) {
                super.onRequestFailure(spiceException);
                Ln.e(spiceException, "Failed to get groups ");
                Toaster.showLong(getActivity(), "Could not get businesses groups: " + spiceException.getMessage());
            }
        });
}

GroupsExpandableListAdapter

public class GroupsExpandableListAdapter extends BaseExpandableListAdapter {

    private ClientGroup.List _data;

    private Activity _activity;
    /**
     * It is only possible to create a group using the factory method create below
     * @param activity
     */
    public GroupsExpandableListAdapter(Activity activity, SpiceManager manager) {
        super();        
        _data = new ClientGroup.List();
        _activity = activity;
        _manager = manager;
    }
    @Override
    public int getGroupCount() {
        return _data.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return _data.size();
    }

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

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

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

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

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

    public void clear() {
        _data.clear();
    }

    public void setGroups(ClientGroup.List groups) {
        _groups = groups;
    }

    @Override
    public View getGroupView(final int groupPosition, final boolean isExpanded, final View convertView,
                             final ViewGroup parent) {
        View v;
        if (convertView == null) {
            LayoutInflater li = (LayoutInflater) _activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = li.inflate(R.layout.group_list_group, parent, false);

            TextView textView = (TextView) v.findViewById(R.id.groupName);
            textView.setText((CharSequence) _data.get(groupPosition).getName());

            ((ImageView) v.findViewById(R.id.groupIndicator))
                    .setImageResource(isExpanded ? R.drawable.arrow_up_turquoise : R.drawable.arrow_down_white);

        } else {
            v = convertView;
        }

        return v;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        View v;
        if (convertView == null) {
            LayoutInflater li = (LayoutInflater) _activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = li.inflate(R.layout.group_list_child, parent, false);

        } else {
            v = convertView;
        }

        TextView joinGroupButton = (TextView) v.findViewById(R.id.joinGroupButton);
        final ClientGroup group = _data.get(groupPosition);
        String leaveGroupStr = _activity.getString(R.string.leave_group);
        String joinGroupStr = _activity.getString(R.string.join_group);

        return v;
    }

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

有什么建议吗? 谢谢!

0 个答案:

没有答案