Question

I am trying to load a list of images from an array. I got the images in list. Now I need to add a text for every image in the list. Can someone help me with this? I am new to Android.

public class EfficientAdapter extends BaseAdapter {
    public EfficientAdapter(Context c) {
        mContext = c;
    }

    public int getCount() {
        return mImageIds.length;
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView i = new ImageView(mContext);
        i.setImageResource(mImageIds[position]);
        i.setScaleType(ImageView.ScaleType.FIT_END);
        i.setLayoutParams(new ListView.LayoutParams(60, 60));
        return i;
    }

    private Context mContext;
    private Integer[] mImageIds = { R.drawable.video3, R.drawable.video5, R.drawable.music2, };
}

This is my code where I have loaded list of images from an array. Please help me with this code.

Was it helpful?

Solution

You could do something like

public View getView(int position, View convertView, ViewGroup parent) 
{
    // Create a linear layout to hold other views
    LinearLayout oItemViewLayout = new LinearLayout(mContext);

    // ImageView
    ImageView i = new ImageView(mContext);
    i.setImageResource(mImageIds[position]);
    i.setScaleType(ImageView.ScaleType.FIT_END);
    i.setLayoutParams(new ListView.LayoutParams(60,60));

    // Add ImageView to item view layout
    oItemViewLayout.addView(i);

    // TextView
    TextView lblTextView = new TextView(mContext);
    lblTextView.setText(mImageNames[position]);

    // Add ImageView to item view layout
    oItemViewLayout.addView(lblTextView);

    return oItemViewLayout;
}

Where you have also defined an array of Strings to hold the names of the images, perhaps like

private String[] mImageNames = {"title of video3", "video5", "music2",};

It would be even easier if you create a layout for the ListItem and load that to create the view instead

Create a layout called say "mylistview.xml"

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:orientation="vertical" >
            <ImageView 
                android:id="@+id/ITEMVIEW_imgImage" 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <TextView 
                android:id="@+id/ITEMVIEW_lblText" 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
</LinearLayout>

You could then make the getView() method like this

public View getView(int position, View convertView, ViewGroup parent) 
{
    // Create a new view or recycle one if available
    View oItemViewLayout;
    if (convertView == null)
    {
        // New view needs to be created
        oItemViewLayout = (View)LayoutInflater.from(mContext).inflate(R.layout.mylistview, parent, false);
    }
    else
    {
        // Recycle an existing view
        oItemViewLayout = (View)convertView;
    }

    // ImageView
    ImageView i = (ImageView)oItemViewLayout.findViewById(R.id.ITEMVIEW_imgImage);
    i.setImageResource(mImageIds[position]);

    // TextView
    TextView lblTextView = (TextView)oItemViewLayout.findViewById(R.id.IITEMVIEW_lblText);
    lblTextView.setText(mImageNames[position]);

    return oItemViewLayout;
}

That will not only make life easier by allowing you design the view in XML but it will also be more efficient because you will re recycling views that have gone off screen but are still in memory because you are grabbing the convertView instance when there is one.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top