Xamarin Visual Studio 2017 带有图像和文本视图的 Gridview
Xamarin Visual Studio 2017 Gridview with images and textview
我正在尝试用图像和文本填充网格视图。首先,我获得了仅处理图像(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 以配合单元格中的每个图像,并使用以下 link 中的代码作为指导:
我将 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
不为空,执行view = convertView
行,与"just images version"相同。那时 textview = view.FindViewById<TextView>(Resource.Id.my_text_view)
returns 行为空,我不明白为什么。我错过了什么?
好的...经过一些研究 (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>
有一个不错的响应显示。感谢所有帮助过我的人。
我正在尝试用图像和文本填充网格视图。首先,我获得了仅处理图像(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 以配合单元格中的每个图像,并使用以下 link 中的代码作为指导:
我将 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
不为空,执行view = convertView
行,与"just images version"相同。那时 textview = view.FindViewById<TextView>(Resource.Id.my_text_view)
returns 行为空,我不明白为什么。我错过了什么?
好的...经过一些研究 (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>
有一个不错的响应显示。感谢所有帮助过我的人。