Frage

I want to have an expandable list showing two different normal lists, one with static data from /assets/, and one with data fetched from the database. Illustrated:

enter image description here

I am not really sure how to go about this. Will I be okay if I just extend BaseExpandableListAdapter and then provide the two adapters as groups, forwarding calls to notifyDataSetChanged to the ExpandableListAdapter? I also assume that if I want to show a footer in the sub-lists, I will need to add them as a row. Will this work? Are there better alternatives?

War es hilfreich?

Lösung

Sure! Use this class as a starting point:

Watch out that child views are recycled, no matter what group they're from.

public static class MergeAdapter extends BaseExpandableListAdapter {
    private final List<Adapter> adapters = new ArrayList<Adapter>(2);
    private final Context ctx;

    public MergeAdapter(Context ctx) {
        List<String> firstList = new ArrayList<String>(3);
        firstList.add("one");
        firstList.add("two");
        firstList.add("three");
        List<String> secondList = new ArrayList<String>(3);
        secondList.add("fo'");
        secondList.add("five");
        secondList.add("six");

        adapters.add(new ArrayAdapter<String>(ctx, android.R.layout.simple_list_item_1, firstList));
        adapters.add(new ArrayAdapter<String>(ctx, android.R.layout.simple_list_item_1, secondList));
        this.ctx = ctx;
    }

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

    @Override
    public int getChildrenCount(int i) {
        return adapters.get(i).getCount();
    }

    @Override
    public Adapter getGroup(int i) {
        return adapters.get(i);
    }

    @Override
    public Object getChild(int i, int i2) {
        return adapters.get(i).getItem(i2);
    }

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

    @Override
    public long getChildId(int i, int i2) {
        return adapters.get(i).getItemId(i2);
    }

    @Override
    public boolean hasStableIds() {
        //In our case true, but for dynamic data likely to be false
        return false;
    }

    @Override
    public View getGroupView(int i, boolean isExpanded, View view, ViewGroup viewGroup) {
        if (view == null) {
            LayoutInflater inflater = LayoutInflater.from(ctx);
            //Better to use layout custom-made for expandable lists...
            view = inflater.inflate(android.R.layout.simple_list_item_1, viewGroup, false);
        }
        TextView tv = (TextView) view.findViewById(android.R.id.text1);
        tv.setText("List " + i);
        return view;
    }

    @Override
    public View getChildView(int i, int i2, boolean isLastView, View view, ViewGroup viewGroup) {
        //isLastView will be handy if you want to make a footer 
        return adapters.get(i).getView(i2, view, viewGroup);
    }

    @Override
    public boolean isChildSelectable(int i, int i2) {
        //Customize
        return true;
    }
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top