Android 带图标和文本的gridview图像

Android 带图标和文本的gridview图像,android,image,gridview,text,Android,Image,Gridview,Text,@@Branislav-我也像你说的那样做了。它对我很有效,但当没有图像超过9N文本时,它也开始在第9个图像和文本的位置向我显示相同的第一个或ant随机图像和文本 public class ImageAdapter extends BaseAdapter { private Context mContext; public ImageAdapter(Context c) { mContext = c; } public int getCount() { return mThu

@@Branislav-我也像你说的那样做了。它对我很有效,但当没有图像超过9N文本时,它也开始在第9个图像和文本的位置向我显示相同的第一个或ant随机图像和文本

public class ImageAdapter extends BaseAdapter {
private Context mContext;

public ImageAdapter(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) {
    View v;
    if (convertView == null) {  // if it's not recycled, initialize some attributes
        LayoutInflater li = getLayoutInflater();
        v = li.inflate(R.layout.mainmenu, null);
        TextView tv = (TextView)v.findViewById(R.id.icon_text);
        tv.setText(mTextsIds[position]);
        ImageView iv = (ImageView)v.findViewById(R.id.icon_image);
        iv.setImageResource(mThumbIds[position]);
    } else {
        v = (View) convertView;
    }
    return v;
}



  private Integer[] mThumbIds = {
        R.drawable.hotel1, R.drawable.rest,R.drawable.dubaicityinfoicon_new,R.drawable.history1,R.drawable.geography,R.drawable.infoicon,R.drawable.infoicon,R.drawable.infoicon,R.drawable.parkmain,

};

// references to our texts
private String[] mTextsIds = {
        "Hotels","Restaurants","City Info","Dubai History","Geography Of Dubai","Useful Information","Embassies in Duabai","Museum's","park"
};

在这方面,它是完美的,直到博物馆,但公园,它不工作;它显示数组mThumbIds中的任意随机图像和关联文本。是的,图像是可绘制的。

使用Map或HashMap将您的值配对

HashMap<Integer, String> store = new HashMap<Integer, String>();
store.put(R.drawable.hotel1, "Hotels");

您可以为此创建自定义适配器类

public class MyAdapter extends ArrayAdapter<BindData> {
    private LayoutInflater inflater;
    private int layoutId;

    public MyAdapter(Context context, int layoutId, BindData[] objects) {
        super(context, 0, objects);
        this.inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.layoutId = layoutId;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if (convertView == null) {
            convertView = inflater.inflate(layoutId, parent, false);
            holder = new ViewHolder();
            holder.textView = (TextView) convertView.findViewById(R.id.textview);
            holder.imageView = (ImageView) convertView.findViewById(R.id.imageview);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        //BindData data = getItem(position);
        holder.textView.setText(titles[position]);
        holder.imageView.setImageBitmap(imgs[position]);
        return convertView;
    }
}
mDatas在哪里

public class BindData {
    Bitmap b;
    String title;
    BindData(Bitmap bitmap, String s) {
        this.b = bitmap;
        this.title = s;
    }
}
private BindData[] mDatas;
    static class ViewHolder {
        TextView textView;
        ImageView imageView;
    }

@@nik博士-我做了sme和tey在u said教程中做的一样。仍然面临问题@@xjaphx-你能详细告诉我如何使用这个吗??bcz我是android新手
public class BindData {
    Bitmap b;
    String title;
    BindData(Bitmap bitmap, String s) {
        this.b = bitmap;
        this.title = s;
    }
}
private BindData[] mDatas;
    static class ViewHolder {
        TextView textView;
        ImageView imageView;
    }