Farhat Shakoor
Farhat Shakoor

Reputation: 25

Multiple ArrayLists in ListAdapter [Android]

UPDATED CODE after Niv's answer:

Here's my Adapter method:

public class PostsListAdapter extends ArrayAdapter<String> {

/**
 * ViewHolder class for layout.<br />
 * <br />
 */
private static class ViewHolder {
    public final RelativeLayout rootView;
    public final TextView postUname;
    public final TextView postText;
    public final ImageView postPlatform;
    public final ImageView postImage;
    public final VideoView postVideo;

    private ViewHolder(RelativeLayout rootView, TextView postUname, TextView postText, ImageView postPlatform, ImageView postImage, VideoView postVideo) {
        this.rootView = rootView;
        this.postUname = postUname;
        this.postText = postText;
        this.postPlatform = postPlatform;
        this.postImage = postImage;
        this.postVideo = postVideo;
    }

    public static ViewHolder create(RelativeLayout rootView) {
        TextView postUname = (TextView)rootView.findViewById( R.id.post_uname );
        TextView postText = (TextView)rootView.findViewById( R.id.post_text );
        ImageView postPlatform = (ImageView)rootView.findViewById( R.id.post_platform );
        ImageView postImage = (ImageView)rootView.findViewById( R.id.post_image );
        VideoView postVideo = (VideoView)rootView.findViewById( R.id.post_video );
        return new ViewHolder( rootView, postUname, postText, postPlatform, postImage, postVideo );
    }
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    final ViewHolder vh;
    if ( convertView == null ) {
        View view = mInflater.inflate( R.layout.post_list, parent, false );
        vh = ViewHolder.create( (RelativeLayout)view );
        view.setTag( vh );
    } else {
        vh = (ViewHolder)convertView.getTag();
    }

    Object item = getItem( position );

    Log.d("Text", item.toString());
    vh.postText.setText(item.toString());

    return vh.rootView;
}

private LayoutInflater mInflater;

// Constructors
public PostsListAdapter(Context context, ArrayList<String> objects) {
    super(context, 0, objects);
    this.mInflater = LayoutInflater.from( context );
}

}

Now, the question is, how do I pass multiple arraylists, like I have eight arrays from which I have to populate the list. Like, for every item, I have to get eight items to show for that item, I have those eight arraylists sorted to keep a one-to-one correspondence.

Any idea how to handle multiple arraylists?

Upvotes: 0

Views: 82

Answers (2)

Hein Htet Aung
Hein Htet Aung

Reputation: 854

Don't understand fully your question, but you can pass List parameters like this

List<List<String>> datalist = new Arraylist<>();

or

List of object list that contains String list

Hope, this will help you

Upvotes: 0

Nivedh
Nivedh

Reputation: 961

Try This Code. A sample Adapter

public class ListAdapter extends ArrayAdapter {

private static class ViewHolder {
    public final FrameLayout rootView;
    public final ImageView imHotelProfile;


    private ViewHolder(FrameLayout rootView, ImageView imHotelProfile) {
        this.rootView = rootView;
        this.imHotelProfile = imHotelProfile;

    }

    public static ViewHolder create(FrameLayout rootView) {
        ImageView imHotelProfile = (ImageView)rootView.findViewById( R.id.im_hotel_profile );
        ImageView imLike = (ImageView)rootView.findViewById( R.id.im_like );

        return new ViewHolder( rootView, imHotelProfile);
    }
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    final ViewHolder vh;
    if ( convertView == null ) {
        View view = mInflater.inflate( R.layout.list_adapter, parent, false );
        vh = ViewHolder.create((FrameLayout) view);
        view.setTag( vh );
    } else {
        vh = (ViewHolder)convertView.getTag();
    }

    GsonObj item = getItem( position );

    // TODOBind your data to the views here
    // fill data

    return vh.rootView;
}

private LayoutInflater mInflater;

// Constructors
public ListAdapter(Context context, List<GsonObj> objects) {
    super(context, 0, objects);
    this.mInflater = LayoutInflater.from(context);
}
public ListAdapter(Context context, GsonObj[] objects) {
    super(context, 0, objects);
    this.mInflater = LayoutInflater.from(context);
}

}

Upvotes: 1

Related Questions