0

I am trying to construct a ListActivity that uses a listview to display a list of friends and their associated status (ie single, in a relationship).

So far, I have an ArrayAdapter set like so:

Friend[] friendList = new Friend[] {
    new Friend("john doe", "single"), 
    new Friend("jane doe", "married")
};

setListAdapter(new ArrayAdapter<Friend>(this, R.layout.portal_listview, friendList));

I would like each item in the listview to display as such:

1name: john doe
status: single

2name: jane doe
status: married

3 Answers 3

2

Create you own CustomArrayAdapter which extends ArrayAdapter and overwrite the View getView(int position, View convertView, ViewGroup parent) method

I could be something like this

public class FriendAdapter extends ArrayAdapter<Friend>{
    private Context context;
    private int resource;
    private ArrayList<Friend> friends;

    public FriendAdapter(Context context, int resource, ArrayList<Friend> friends) {
        super(context, resource, objects);
        this.context = context;
        this.resource = resource;
        this.friends = friends;
    }

@Override
    public View getView(int position, View convertView, ViewGroup parent){
        View view = convertView;
        if (view == null){
            LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = vi.inflate(your_list_item_resource, null);
        }
        Friend friend = friends.get(position);
        view.setId(position);
        if (friend != null){
            TextView name = (TextView) view.findViewById(R.id.friendName);
            name.setText(friend.getName());
            TextView status = (TextView) view.findViewById(R.id.friendStatus);
            status.setText(friend.getStatus());
        }
        return view;
    }

Hope this can help you.

Sign up to request clarification or add additional context in comments.

Comments

0

In the xml row file, you can add a composite object, like a LinearLayout and inside that you could include more than one textview, or a combination etc.

I guess the linearlayout would be unnecessary if the individual items within the list are displayed vertically, but you can try.

Comments

0

You need to override getView in the ArrayAdapter class. In that method, inflate your R.layout.portal_listview instance and fill it with whatever you want.

That xml layout should be some layout form that includes a TextView for the two things you want to display.

Easiest would be a linear layout, set to vertical orientation, with 2 TextViews in it, stacked.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.