Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put another adapter inside my custom adapter (info from one object)

How do I fill my listview that is inside a custom adapter row already. Is this the right way?

I have an organization object. And this organization can have more contacts.

So I want to create for example, a layout with on the top the name of the organization and then the name of the contacts under it.

Getting the organization object and putting it inside my custom adapter and showing the name is no problem. But how can I fill the listview in that row-layout? I dont really get the context parameter of new ArrayAdapter Constructor inside the custom adapter. What does it need?

public class ContactAdapter extends ArrayAdapter<Organization> {
Context context;
int layoutResourceId;
ArrayList<Organization> data = null;

public ContactAdapter(Context context, int layoutResourceId, ArrayList<Organization> data) {
    super(context, layoutResourceId, data);
    this.context = context;
    this.layoutResourceId = layoutResourceId;
    this.data = data;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    OrganizationHolder holder = null;

    if (row == null) {
        LayoutInflater inflater = ((Activity)context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent,false);

        holder = new OrganizationHolder();
        holder.tvOrgName = (TextView)row.findViewById(R.id.tvOrgName);
        holder.LvContacts = (ListView)row.findViewById(R.id.LvContacts);

        row.setTag(holder);

    } else {
        holder = (OrganizationHolder) row.getTag();
    }

    Organization org = data.get(position);
    ArrayList<String> contacts = new ArrayList<String>();
    for (Contact c : org.getContacts()) {
        contacts.add(c.getName());
    }
    Log.i("List",""+contacts);
    ArrayAdapter<String> contactsadapter = new ArrayAdapter<String>(??,android.R.layout.simple_list_item_1,contacts);
    holder.LvContacts.setAdapter(contactsadapter);
    holder.tvOrgName.setText(org.getName());

    return row; 
}

static class OrganizationHolder {
    TextView tvOrgName;
    ListView LvContacts;
    Button btnDetails;
}
like image 599
Lokkio Avatar asked Jul 16 '13 13:07

Lokkio


Video Answer


1 Answers

You should not have ListView inside the row of another ListView.

You might consider using ExpandableListView and subclass BaseExpandableListAdapter instead of the classes you are using now. Use the organization as the "group" views and the contacts as "child" views.

If you dont want an expandable list and absolutely need all the information in one Adapter, you can form a data structure that is a map (or a list) of organizations, each with a list of contacts. Then you would write a custom adapter implementation to work with it.

public class MyAdapter extends BaseAdapter {
    private List<Organization> mList;
    private LayoutInflater mInflater;
    private int mCount;

    public MyAdapter(Context context, List<Organization> list) {
        mInflater = LayoutInflater.from(context);
        mLIst = list;
        mCount = countItems();
    }

    private int countItems() {
        int count = 0;
        if (list != null) {
            for (Organization org : organizations) {
                count++; // org
                List<Contact> contacts = org.getContacts();
                if (contacts != null) count += contacts.size()); // contacts
            }
        }
        return count;
    }

    public int getCount() {
        return mCount;
    }

    public Object getItem(int position) {
        for (Organization org : orgnizations) {
            if (position == 0) {
                return org;
            }
            position--;
            List<Contact> contacts = org.getContacts();
            if (contacts != null) {
                int size = contacts.size();
                if (position >= size) {
                    position -= size;
                } else {
                    return contacts.get(position);
                }
            }
        }
        // not found
        throw new IndexOutOfBoundsException(); // or some other exception
    }

    // other methods omitted for brevity
}

I left our the getView() method, but hopefully you can sort that out on your own. If your rows need to look different based on what data is shown (Organization or Contact), you should also implement getItemViewType() and getItemViewTypeCount().

I recommend you watch a video called The World of ListView

like image 121
Karakuri Avatar answered Oct 30 '22 19:10

Karakuri