我想创建一个网格视图,其中总共有 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>
我得到的问题是图像不正确。他们失去了。处理职位时我的代码是否正确?