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};
}