0

我想创建一个网格视图,其中总共有 7 列。网格中的第一行是文本,它是动态的,随后的行包含图像,它们也是动态的。第一次我尝试单独使用动态图像并且成功但是在添加文本视图之后我得到了错误。

我的网格视图适配器就像

private Context mContext;
private int[] colors = new int[] { Color.WHITE, 0x30aaaaaa };
private int[] dotColors = new int[7];
private int[] sta= new int[100];
private TasksDataSource datasource;
private int con=1000;
TextView textview1;
public Integer[] mThumbIds = new Integer[con];
String[] dday = {"S","M","T","W","T","F","S"};
int[] day7;

public ImageAdapter(Context context, int[] content,int[] content2){
    sta = content;
    day7=content2;
    datasource = new TasksDataSource(context); //here
    datasource.open(); 
    mContext=context;
}s

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    int[] st = new int[sta.length];
    int ii=0,jj=0;
    View myView  = null;
    if(convertView==null) {
        LayoutInflater li = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        myView = li.inflate(R.layout.gridmenu, null);
    }else{
        myView = convertView;
    }

    TextView tv = (TextView) myView.findViewById(R.id.grid_item_text);
    if(position<7) {
        tv.setText(dday[position]);
        tv.setTextColor(day7[position]);
    }
    else {
        ImageView imageView =(ImageView) myView.findViewById(R.id.grid_item_image);
        imageView.setImageResource(sta[position]);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
    }

    return myView;
}

我的网格布局是这样的

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/GridItem"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:gravity="center_horizontal">

    <TextView android:id="@+id/grid_item_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:layout_centerHorizontal="true">
    </TextView>

    <ImageView android:id="@+id/grid_item_image"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
    </ImageView>

</RelativeLayout>

我得到的问题是图像不正确。他们失去了。处理职位时我的代码是否正确?

4

1 回答 1

0

创建一个带有文本视图的布局并将其膨胀并使用此适配器...

    public static class ImageAdapter extends BaseAdapter {
                Context MyContext;
                List<Buttons> list;
                private LayoutInflater mInflater;
                int pos;

                public ImageAdapter(Context _MyContext, List<Buttons> _list) {
                    MyContext = _MyContext;
                    mInflater = LayoutInflater.from(_MyContext);
                    list = _list;
                }

                public int getCount() {
                    return list.size();
                }

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

                    if (convertView == null) {
                        convertView = mInflater.inflate(R.layout.grid_item, null);
                        holder = new ViewHolder();

                        holder.text= (Textview) convertView
                                .findViewById(R.id.grid_item_text);
                        holder.image = (ImageView) convertView.findViewById(R.id.grid_item_image);
                        holder.image.setImageResource(list.get(position).getIconid());
                        holder.text.setText(list.get(position).getText);
                    } else {
                        convertView = mInflater.inflate(R.layout.grid_item, null);
                        holder = new ViewHolder();
                        holder.text= (Textview) convertView
                                .findViewById(R.id.grid_item_text);
                        holder.image = (ImageView) convertView
                                .findViewById(R.id.grid_item_image);
                        holder.image.setImageResource(list.get(position).getIconid());
holder.text.setText(list.get(position).getText);
                    }
                    convertView.setBackgroundDrawable(null);
                    return convertView;
                }

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

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

                static class ViewHolder {
                    ImageView image;
                    Textview text;
                }
            }
于 2013-04-29T09:58:18.500 回答