0

I have a ExpandableListView in a Fragment. I'm posting a request to the server using RoboSpice onCreateView. I want to be able to update the list twice, once when I get the data from the cache and second after the response returns. For some reason the list is not populated. I was able to populate it when I instantiate a new adapter and set the adapter for list, but for some reason it doesn't work on some android devices.

This is the code I'm using:

Fragment

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

Any suggestions? Thanks!

bentzy
  • 1,244
  • 2
  • 15
  • 31
  • I think that if in debugging your code run the rows of clear, set and notify everything must works well. Just make attenction that the line code _groupListView.invalidateViews(); i unusless and you must prevent the use of the getActivity() in the method onCreateView(). This callback can be fired before the activity is finish to create and the getActivity can be null. – phemt.latd Jan 27 '14 at 09:04
  • These rows do run, but the list is not populated :( It's OK if the Activity is null, I check it later – bentzy Jan 27 '14 at 09:42
  • My fear is, if the Activity is null the CachedRequestListener working well? Is strange that this code not work, in debug if you check a breakpoint you arrive to the code row: _expandableListAdapter.clear(); ? – phemt.latd Jan 27 '14 at 10:07
  • We need more debug information to help you. It's also strange to me that groups and child have the same count. – Snicolas Feb 11 '14 at 05:52
  • Hi @Snicolas, thanks for helping. You are right about the count - this seems like a mistake - it should be 1 child for each group. I ended up changing the Adapter to SimpleExpandableListAdapter and creating a new adapter each time I wanted to update the list. I still don't like this solution, but it's working now. I'll try to get back to this when I have more time. THANKS! – bentzy Feb 11 '14 at 09:47

0 Answers0