Ich muss etwas Text unter jedem Bild in der Gridview einfügen. Kann mir jemand dabei helfen? Ich habe versucht, eine Textansicht in die getView(), aber keinen Erfolg zu bringen. Ich habe meinen Versuch, dies zu tun, auskommentiert.Kann jemand Text unter jedem Bild in einer Gridview setzen?
Das ist mein Bild Adaptercode:
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
//import android.widget.TextView;
public class ImageAdapter extends BaseAdapter{
private Context mContext;
//private String[] texts = {"aaa", "bbb", "ccc", "ddd", "eee", "fff", "eee", "hhh", "iii"};
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() { //Constructor
return getmThumbIds().length; //+9
}
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;
//TextView tv;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85)); //Sets height and width for the view - Could use setAdjustViewBounds(boolean).
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);//Image should be cropped towards centre
imageView.setPadding(8, 8, 8, 8); //Sets padding for all sides
//tv = new TextView(mContext);
//tv.setLayoutParams(new GridView.LayoutParams(85, 85));
} else {
imageView = (ImageView) convertView;
//tv = (TextView) convertView;
//tv.setText(texts[position]);
//return tv;
}
imageView.setImageResource(getmThumbIds()[position]); //Chooses image from the array
return imageView;
}
public void setmThumbIds(Integer[] mThumbIds) {
this.mThumbIds = mThumbIds;
}
public Integer[] getmThumbIds() {
return mThumbIds;
}
// references to our images
Integer[] mThumbIds = {
R.drawable.puma_boots, R.drawable.cricket_balls,
R.drawable.darts, R.drawable.paintballing_gloves,
R.drawable.ice_skates, R.drawable.skate_ramp,
R.drawable.manu_top, R.drawable.rugby_ball,
R.drawable.puma_boots, R.drawable.cricket_balls,
R.drawable.darts, R.drawable.paintballing_gloves,
R.drawable.ice_skates, R.drawable.skate_ramp,
R.drawable.manu_top, R.drawable.tennis_racket,
R.drawable.puma_boots, R.drawable.cricket_balls,
R.drawable.darts, R.drawable.paintballing_gloves,
R.drawable.ice_skates, R.drawable.skate_ramp,
R.drawable.rugby_ball, R.drawable.tennis_racket
};
}
Meinen Sie, nach der return-Anweisung, im Inneren eine xml mit dem Relative Layout verweisen? Ich bin mir nicht sicher, wie ich das programmieren soll. – Ben
@Ben: Hier ist ein Auszug aus einem meiner Bücher, die das Erstellen von benutzerdefinierten Adaptern wie folgt beschreibt: http://commonsware.com/Android/excerpt.pdf Es diskutiert sie im Zusammenhang mit 'ListView', aber der gleiche Ansatz sollte arbeite auch für 'GridView'. – CommonsWare