0

How can I add image to a gridview with the image itself and also a text. I tried to make custom image adapter but it doesn't work for somereason get a crash on imgView.setLayoutParams(new GridView.LayoutParams(85, 85));

New question how I can add and remove from the image adapter... I need the behavior to be like one pic with a and I add same pic with text ab so there should be 2 pics and then i can remove the one with a..

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class AlbumAdapter extends BaseAdapter {

    private Context mContext;
    private ArrayList albNames;

    public AlbumAdapter(Context c, ArrayList albNames) {

        mContext = c;
        this.albNames = albNames;
    }

    public int getCount() {

        return mThumbIds.length;
    }

    public Object getItem(int position) {

        return null;
    }

    public long getItemId(int position) {

        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
/*  public View getView(int position, View convertView, ViewGroup parent) {

        ImageView imageView;

        if (convertView == null) { // if it's not recycled, initialize some attributes

            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(10, 10, 10, 3);
        } 

        else {

            imageView = (ImageView) convertView;
        }

        imageView.setImageResource(mThumbIds[position]);
        return imageView;
    }

    */

    public View getView(int position, View convertView, ViewGroup parent) {

        View view;

        if (convertView == null) {

            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.album_image, null);
        } 

        else {

            view = convertView;
        }

        ImageView imgView = (ImageView) view.findViewById(R.id.icon_image);
        imgView.setLayoutParams(new LinearLayout.LayoutParams(50, 50));
        imgView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imgView.setPadding(10, 10, 10, 3);
        TextView albNameView = (TextView) view.findViewById(R.id.icon_text);

        if (albNames.size() > 0) {

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

                imgView.setBackgroundResource(R.drawable.folder);           
                albNameView.setText((String)albNames.get(i));

            }

        }




        return view;
    }


    // references to our images
    public Integer[] mThumbIds = { R.drawable.folder};

}
jrdnsingh89
  • 215
  • 2
  • 7
  • 18

2 Answers2

0

Try

imgView.setLayoutParams(new LinearLayout.LayoutParams(85, 85));

EDIT

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class AlbumAdapter extends BaseAdapter {

    private Context mContext;

    public AlbumAdapter(Context c) {

        mContext = c;
    }

    public int getCount() {

        return mThumbIds.length;
    }

    public Object getItem(int position) {

        return null;
    }

    public long getItemId(int position) {

        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
/*  public View getView(int position, View convertView, ViewGroup parent) {

        ImageView imageView;

        if (convertView == null) { // if it's not recycled, initialize some attributes

            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(10, 10, 10, 3);
        } 

        else {

            imageView = (ImageView) convertView;
        }

        imageView.setImageResource(mThumbIds[position]);
        return imageView;
    }

    */

    public View getView(int position, View convertView, ViewGroup parent) {

        View view;

        if (convertView == null) {

            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.album_image, null);
        } 

        else {

            view = convertView;
        }

        ImageView imgView = (ImageView) view.findViewById(R.id.icon_image);
        imgView.setLayoutParams(new LinearLayout.LayoutParams(85, 85));
        imgView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imgView.setBackgroundResource(R.drawable.a);

        TextView albNameView = (TextView) view.findViewById(R.id.icon_text);
        albNameView.setText("as");

        return view;
    }


    // references to our images
    public Integer[] mThumbIds = { R.drawable.a };

}
Arun C
  • 9,035
  • 2
  • 28
  • 42
  • I think the parent of imageview is a linear layout so. Did you try it? – Arun C Apr 19 '13 at 02:07
  • how can I add and remove from the imageadapter. I have a picture in the imageadapter I want add the same every time with a different name in the textview and remove that picture and the update imageadapter – jrdnsingh89 Apr 19 '13 at 02:36
  • it is simple you need to change the data in your Adapter (say mThumbIds) then invalidate the adapter See http://stackoverflow.com/questions/4198425/updating-the-list-view-when-the-adapter-data-changes – Arun C Apr 19 '13 at 02:40
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/28482/discussion-between-jrdnsingh89-and-arun-c-thomas) – jrdnsingh89 Apr 19 '13 at 02:44
0

'imgView' is the child of ConvertView which is inflated from R.layout.album_image which is nothing but a LinearLayout. So while setting layout params it needs to be parents ViewGroup type. So LinearLayout.LayoutParams works fine.

Suppose you wanted to set the size of 'ConvertView' then in that case it needs to be GridView.LayoutParams, becoz 'ConvertView' is being added to parent which is GridView.

Hope u got it..!!

Mani
  • 767
  • 1
  • 10
  • 20