How to dynamically add ImageView to layout using Adapter

user3228515

I would like to dynamically add ImageViews to my RelativeLayout through my Adapter. The code I have isn't producing any errors, however, the ImageViews are not being created (confirmed using Hierarchy View).

The code that I'm using to add the ImageView is in the OnBindViewHolder() method.

// Create the basic adapter extending from RecyclerView.Adapter
// Note that we specify the custom ViewHolder which gives us access to our views

public class RallyAdapter extends RecyclerView.Adapter<RallyAdapter.ViewHolder> {

// Provide a direct reference to each of the views within a data item
// Used to cache the views within the item layout for fast access

public static class ViewHolder extends RecyclerView.ViewHolder{
    // Your holder should contain a member variable
    // for any view that will be set as you render a row
    public TextView nameTextView;
    public TextView dateTextView;
    public TextView creatorTextView;
    public ImageButton thumbnail;
    public ImageView image;
    public RelativeLayout relativeLayout;

    // We also create a constructor that accepts the entire item row
    // and does the view lookups to find each subview
    public ViewHolder(View itemView) {
        // Stores the itemView in a public final member variable that can be used
        // to access the context from any ViewHolder instance.
        super(itemView);

        relativeLayout = (RelativeLayout) itemView.findById(R.id.view); 
        nameTextView = (TextView) itemView.findViewById(R.id.name);
        dateTextView = (TextView) itemView.findViewById(R.id.date);
        thumbnail = (ImageButton) itemView.findViewById(R.id.imageButton);
        creatorTextView = (TextView) itemView.findViewById(R.id.creator_name);
    }
}

// Store a member variable for the contacts
private ArrayList<Rally> mRallys;
private Context mContext;

// Pass in the contact array into the constructor
public RallyAdapter(Context context, ArrayList<Rally> rallys) {
    this.mRallys = rallys;
    this.mContext = context;
}

// Usually involves inflating a layout from XML and returning the holder
@Override
public RallyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    Context context = parent.getContext();
    LayoutInflater inflater = LayoutInflater.from(context);

    // Inflate the custom layout
    View rallyView = inflater.inflate(R.layout.rally, parent, false);

    // Return a new holder instance
    ViewHolder viewHolder = new ViewHolder(rallyView);

    return viewHolder;
}

// Involves populating data into the item through holder
@Override
public void onBindViewHolder(RallyAdapter.ViewHolder viewHolder, final int position) {

    // Get the data model based on position
    final Rally rally = mRallys.get(position);

    TextView dateTV = viewHolder.dateTextView;

    ImageButton imageButton = viewHolder.thumbnail;

    Picasso.with(mContext).load(rally.getThumbnail()).fit().centerCrop().into(imageButton);

    List<String> image_urls = rally.getTransportationImgs();
    List<String> methods = rally.getTransportationStrs();

    Log.d("IMG URLS", image_urls.toString());

    for (int i = 0; i < methods.size(); i++) {

        String method = methods.get(i);

        for (int j = 0; j < image_urls.size(); j++) {

            String img_url = image_urls.get(j);

            if (img_url.toLowerCase().contains(method.toLowerCase()) == true) {

                viewHolder.image = new ImageView(mContext);
                dateTV = viewHolder.dateTextView;
                imageButton = viewHolder.thumbnail;

                RelativeLayout rallyLayout = new RelativeLayout(mContext);
                RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT);

                params.addRule(RelativeLayout.RIGHT_OF, imageButton.getId());
                params.addRule(RelativeLayout.BELOW, dateTV.getId());
                params.addRule(RelativeLayout.END_OF, imageButton.getId());
                params.height = 65;
                params.width = 65;

                viewHolder.image.setLayoutParams(params);

                rallyLayout.addView(viewHolder.image);

                viewHolder.relativeLayout.addview(rallyLayout);

                Picasso.with(mContext).load(img_url).fit().centerCrop().into(viewHolder.image);
            }
        }
    }
}

@Override
public int getItemCount() {
    // TODO Auto-generated method stub
    return mRallys.size();
}

I've tried to accomplish this through the getView() method but it seems as if the getView() function wasn't being called, for some reason.

xiaomi

You are adding your image to a Relative layout but this relative layout isn't added to your view.

In onCreate, you should keep a reference to the view that inflate R.layout.rally by setting the root element must have an android:id="@+id/view". (without your xml file, I'm not sure, but seems you used a relative layout as root element)

Then into your viewholder add :

public RelativeLayout relativeLayout;

relativeLayout = (RelativeLayout) itemView.findById(R.id.view); 

Then in onBindViewHolder, put after rallyLayout.addView(viewHolder.image);

viewHolder.relativeLayout.addview(rallyLayout)

You may also have to adjust the layout parameters but seems you know how to do.

Just a little comment, try to not mix layout parameters even if they produce the same result RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to change adapter layout dynamically?

From Dev

How to add the layout dynamically

From Dev

How to add ImageView to layout programmatically

From Dev

How to add Image to ImageView dynamically?

From Dev

how to dynamically add image to imageView

From Dev

Add Images in ImageView of Listview dynamically via Custom Adapter

From Dev

How to dynamically add Image to dynamically created ImageView

From Dev

How to dynamically set imageView source for a List View from Custom Adapter?

From Dev

How to add an item in Listview adapter Linear Layout

From Dev

How to add an item in Listview adapter Linear Layout

From Dev

How do I add an ImageView to the root layout?

From Dev

How to add another ImageView over an existing ImageView dynamically?

From Dev

Android : How to populate fragment dynamically in a recycler layout adapter?

From Dev

Add layout to ImageView Activity

From Dev

How to add this type of layout dynamically (android)

From Dev

How to add 2 fragments dynamically in a single layout?

From Dev

Dynamically add items to list view using custom adapter for Android app

From Dev

How do I dynamically add buttons using Java in a given XML layout file in android studio?

From Dev

Dynamically add layout to another layout

From Dev

How to add a ListFragment Using to layout

From Dev

Add listview adapter to layout of Fragment

From Dev

Add listview adapter to layout of Fragment

From Dev

How to add a button over an ImageView in ScrollView dynamically? Android. Xamarin

From Dev

Dynamically add element to layout

From Dev

Dynamically add QWebEngineView to layout

From Dev

Drawables on ImageView: how to layout?

From Dev

How to use multiple layout using one RecyclerView Adapter?

From Dev

Row cannot add dynamically using code to Table Layout in Android

From Dev

Pinch to zoom IN or OUT imageview added dynamically to Layout

Related Related

  1. 1

    How to change adapter layout dynamically?

  2. 2

    How to add the layout dynamically

  3. 3

    How to add ImageView to layout programmatically

  4. 4

    How to add Image to ImageView dynamically?

  5. 5

    how to dynamically add image to imageView

  6. 6

    Add Images in ImageView of Listview dynamically via Custom Adapter

  7. 7

    How to dynamically add Image to dynamically created ImageView

  8. 8

    How to dynamically set imageView source for a List View from Custom Adapter?

  9. 9

    How to add an item in Listview adapter Linear Layout

  10. 10

    How to add an item in Listview adapter Linear Layout

  11. 11

    How do I add an ImageView to the root layout?

  12. 12

    How to add another ImageView over an existing ImageView dynamically?

  13. 13

    Android : How to populate fragment dynamically in a recycler layout adapter?

  14. 14

    Add layout to ImageView Activity

  15. 15

    How to add this type of layout dynamically (android)

  16. 16

    How to add 2 fragments dynamically in a single layout?

  17. 17

    Dynamically add items to list view using custom adapter for Android app

  18. 18

    How do I dynamically add buttons using Java in a given XML layout file in android studio?

  19. 19

    Dynamically add layout to another layout

  20. 20

    How to add a ListFragment Using to layout

  21. 21

    Add listview adapter to layout of Fragment

  22. 22

    Add listview adapter to layout of Fragment

  23. 23

    How to add a button over an ImageView in ScrollView dynamically? Android. Xamarin

  24. 24

    Dynamically add element to layout

  25. 25

    Dynamically add QWebEngineView to layout

  26. 26

    Drawables on ImageView: how to layout?

  27. 27

    How to use multiple layout using one RecyclerView Adapter?

  28. 28

    Row cannot add dynamically using code to Table Layout in Android

  29. 29

    Pinch to zoom IN or OUT imageview added dynamically to Layout

HotTag

Archive