我正在尝试使用图像和文本填充gridview。首先,我得到的代码只使用图像(12张图像)。该图像适配器代码是:
class ImageAdapter : BaseAdapter
{
public DataSet participants;
public User MyUser;
private readonly Context context;
public ImageAdapter(Context c)
{
context = c;
}
public override int Count
{
get { return participants.Tables[0].Rows.Count; }
}
public override Java.Lang.Object GetItem(int position)
{
return null;
}
public override long GetItemId(int position)
{
return 0;
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
ImageView imageView;
if (convertView == null)
{
// if it's not recycled, initialize some attributes
imageView = new ImageView(context);
imageView.LayoutParameters = new AbsListView.LayoutParams(340, 340);
imageView.SetScaleType(ImageView.ScaleType.CenterCrop);
imageView.SetPadding(8, 8, 8, 8);
}
else
{
imageView = (ImageView)convertView;
}
string participantid = participants.Tables[0].Rows[position][0].ToString();
myservice.aservice myService = new service;
DataSet MyPhotoDataSet = myService.GetPhoto(MyUser.Username, MyUser.Password, participantid );
byte[] imageBytes = (byte[])MyPhotoDataSet.Tables[0].Rows[0][0];
Bitmap bitmap = BitmapFactory.DecodeByteArray(imageBytes, 0, imageBytes.Length);
imageView.SetImageBitmap(bitmap);
// imageView.SetImageResource(thumbIds[position]);
return imageView;
}
}
这没有问题。我得到了gridview中显示的所有图像。我需要添加一个textview来与单元格中的每个图像一起使用,并使用以下链接中的代码作为指导:Xamarin: Create a custom grid View
我将GetView更改为:
public override View GetView(int position, View convertView, ViewGroup parent)
{
View view;
if (convertView == null)
{
// if it's not recycled, initialize some attributes
view = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.Cellview, parent, false);
}
else
{
view = convertView;
}
var myimageView = view.FindViewById<ImageView>(Resource.Id.my_image_vieww);
var textview = view.FindViewById<TextView>(Resource.Id.my_text_view);
myimageView.LayoutParameters = new AbsListView.LayoutParams(340, 340);
myimageView.SetScaleType(ImageView.ScaleType.CenterCrop);
myimageView.SetPadding(8, 8, 8, 8);
string participantid = participants.Tables[0].Rows[position][0].ToString();
myservice.aservice myService = new service;
DataSet MyPhotoDataSet = myService.GetPhoto(MyUser.Username, MyUser.Password, participantid );
byte[] imageBytes = (byte[])MyPhotoDataSet.Tables[0].Rows[0][0];
Bitmap bitmap = BitmapFactory.DecodeByteArray(imageBytes, 0, imageBytes.Length);
myimageView.SetImageBitmap(bitmap);
textview.Text = participants.Tables[0].Rows[position][1].ToString();
// imageView.SetImageResource(thumbIds[position]);
return myimageView;
}
}
这是Cellview.xml:
?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:layout_height="250dp"
android:scaleType="centerCrop"
android:id="@+id/my_image_vieww" />
<TextView
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/my_text_view" />
</LinearLayout>
第一次没有问题,在第二次执行时convertview
不为null并执行view = convertView
行,这与“just images version”相同。在那一点,行textview = view.FindViewById<TextView>(Resource.Id.my_text_view)
返回null,我无法弄清楚为什么。我错过了什么?
好的...经过一些研究(http://www.mkyong.com/android/android-gridview-example/)和很多人的建议,我得到了一些非常好的东西。我将GetView方法更改为:
public override View GetView(int position, View convertView, ViewGroup parent)
{
LayoutInflater inflater = (LayoutInflater)context.GetSystemService(Context.LayoutInflaterService);
View gridView;
gridView = new View(context);
gridView = inflater.Inflate(Resource.Layout.Cellview, null);
TextView textView = (TextView)gridView.FindViewById(Resource.Id.my_text_view);
textView.Text = participants.Tables[0].Rows[position][1].ToString();
ImageView imageView = (ImageView)gridView.FindViewById(Resource.Id.my_image_view);
byte[] imageBytes = (byte[])participants.Tables[0].Rows[position][2];
Bitmap bitmap = BitmapFactory.DecodeByteArray(imageBytes, 0, imageBytes.Length);
imageView.SetImageBitmap(bitmap);
return gridView;
}
我还将Cellview.xml稍微更改为:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="250dp"
android:scaleType="centerCrop"
android:id="@+id/my_image_view" />
<TextView
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:paddingTop="20px"
android:id="@+id/my_text_view" />
</LinearLayout>
有一个很好的响应显示。感谢所有帮助过的人。