Search code examples
androidandroid-arrayadapterandroid-adapterlistadaptercommonsware-cwac

Is this ArrayAdapter suitable for use with CommonsWare MergeAdapter? If so, why is it not working?


I'm attempting to use CommonsWare's MergeAdapter class and having limited success. In particular, I am not sure if 1) my ArrayAdapter is suitable for use, 2) if I am adding it correctly, and 3) if I am doing all that is necessary to wire everything up.

Here is my subclass of ArrayAdapter:

class PDLAdapter extends ArrayAdapter<PartnerDisease> {

    public PDLAdapter(final Context context) {
        super(context, 0);
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        ViewHolder viewHolder;

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.partnerdisease_list_item, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.populateViews(getItem(position));

        return convertView;
    }

}

Here is my object StructuredSubDisease (the name makes no sense if you actually consider it's a top-level object containing sub diseases, but whatever):

class StructuredSubDisease {
    public String headingText;
    public ArrayList<PartnerDisease> subDiseases;

    public View headingView() {
        View returnView = mInflater.inflate(R.layout.partnerdisease_list_item, null);
        TextView t = (TextView) returnView.findViewById(R.id.tv_displayname);
        t.setText(headingText);
        return returnView;
    }
}

...and here is where the "magic" is supposed to be happening.

            for (StructuredSubDisease s : subDiseaseList) {
                mMergeAdapter.addView(s.headingView()); // @Alex, <--- thing 1

                PartnerDiseaseListAdapter adapter = new PartnerDiseaseListAdapter(this);

                for (PartnerDisease p : s.subDiseases) {
                    adapter.add(p);
                }

                mMergeAdapter.addAdapter(adapter); // <--- and thing 2
            }

I have Logged the count:

   Log.i("mergecount", "" + mMergeAdapter.getCount());

This returns 1, where I would expect 2.

EDIT: I forgot to mention, the result of this is that the headingView() is displayed with the proper heading text, but there is no list beneath it.

Where am I going wrong?


Solution

  • my ArrayAdapter is suitable for use

    It seems OK.

    if I am adding it correctly

    It seems OK.

    if I am doing all that is necessary to wire everything up

    You don't have any diseases, apparently.

    because I added two things - the headingView() (which is rendered) and the adapter (which silently fails)

    getCount() returns the number of total rows that should be in your ListView, not the number of things added to the MergeAdapter. In your case, it would appear that you have no diseases.

    Start by putting your PartnerDiseaseListAdapter directly into your ListView, ignoring the MergeAdapter. Get that working. Then, switch back to the MergeAdapter.