C# 带图像的xamarin android列表视图
Im尝试从数据库加载数据并显示在ListView上。 列表视图包含2个文本视图和1个图像视图。我正在使用SQlite.net。 我的数据库在哪里: 数据库C# 带图像的xamarin android列表视图,c#,android,sqlite,listview,xamarin,C#,Android,Sqlite,Listview,Xamarin,Im尝试从数据库加载数据并显示在ListView上。 列表视图包含2个文本视图和1个图像视图。我正在使用SQlite.net。 我的数据库在哪里: 数据库 public class contacts { [PrimaryKey, AutoIncrementAttribute, Column("id")] public int id {get; set;} public string name {get; set;} public string number { g
public class contacts
{
[PrimaryKey, AutoIncrementAttribute, Column("id")]
public int id {get; set;}
public string name {get; set;}
public string number { get; set; }
public byte[] photo{ get; set; }
}
我填充listview的函数在哪里:
private void refreshListView()
{
var db = new SQLiteConnection (MainActivity._DatabaseConnectString);
MatrixCursor mMatrixCursor = new MatrixCursor (new String[]{ "_id", "name", "number", "photo" });
SimpleCursorAdapter adap;
adap = new SimpleCursorAdapter (
this,
Resource.Layout.lv_layout,
null,
new String[]{ "name", "number", "photo" },
new int[]{ Resource.Id.tv_name, Resource.Id.tv_number, Resource.Id.iv_photo }, 0);
IEnumerable<contacts> table = db.Query<contacts> ("select * from contacts");
foreach (var contact in table) {
mMatrixCursor.AddRow (new Java.Lang.Object[] {contact.id,
contact.name, contact.number, BitmapFactory.DecodeByteArray (contact.photo, 0, contact.photo.Length)
});
}
lview.Adapter = adap;
adap.SwapCursor (mMatrixCursor);
}
我已经用java for android完成了这个应用程序,我用这段代码解决了这个问题,但我不知道如何将代码从java“翻译”成c
其中是adap下面的java代码:
adap.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
@Override
public boolean setViewValue(View view, Cursor cursor, int i) {
if (view.getId() == R.id.iv_photo) {
byte[] Blobdata = cursor.getBlob(i);
Bitmap bitmap = Bitmap.createScaledBitmap(BitmapFactory.decodeByteArray(
Blobdata, 0, Blobdata.length), 150, 150, false);
((ImageView) view).setImageBitmap(bitmap);
return true;
} else {
return false;
}
}
});
有人能帮我把这段代码“翻译”成c#?或者使用其他代码。
谢谢你的帮助,对不起我的英语
编辑2
好的,代码在哪里被翻译成c#
但现在我在这行中遇到了一个错误:
byte[] Blobdata = cursor.GetBlob (i);
错误:
android.graphics.Bitmap cannot be cast to byte[]
我想我得到这个错误是因为在我的数据库中我没有使用Blob,而是使用字节数组。有人知道如何从数据库检索字节数组,或者如何使用Blob数据类型吗?您可以在Blob中存储字节数组 我就是这样取的:
using (SqliteDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
images.Add(new Image((byte[])reader["Image"]));
}
}
然后,我可以使用我的Image
属性ImageByteArray
将其转换为位图,类似于您所做的方式
Bitmap image = BitmapFactory.DecodeByteArray(data, 0, barray.Length);
然后,位图
就可以在图像视图
中显示了
imageView.SetImageBitmap(image);
正如你所看到的,我们的方法非常相似。所以在我看来,cursor.GetBlob(i)似乎返回了一些意想不到的结果
公共抽象字节[]getBlob(int columnIndex)
在API级别1中添加
以字节数组的形式返回请求列的值
结果以及当列值为null或列类型不是blob类型时,此方法是否引发异常由实现定义
参数:
columnIndex目标列的从零开始的索引
返回:
该列作为字节数组的值
()
根据该方法的描述,您的列似乎不包含BLOB/字节数组
使用
var Blobdata=cursor.GetBlob(i)编码>以找出其中的内容,并进行相应调整。您可以将字节数组存储在BLOB中
我就是这样取的:
using (SqliteDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
images.Add(new Image((byte[])reader["Image"]));
}
}
然后,我可以使用我的Image
属性ImageByteArray
将其转换为位图,类似于您所做的方式
Bitmap image = BitmapFactory.DecodeByteArray(data, 0, barray.Length);
然后,位图
就可以在图像视图
中显示了
imageView.SetImageBitmap(image);
正如你所看到的,我们的方法非常相似。所以在我看来,cursor.GetBlob(i)
似乎返回了一些意想不到的结果
公共抽象字节[]getBlob(int columnIndex)
在API级别1中添加
以字节数组的形式返回请求列的值
结果以及当列值为null或列类型不是blob类型时,此方法是否引发异常由实现定义
参数:
columnIndex目标列的从零开始的索引
返回:
该列作为字节数组的值
()
根据该方法的描述,您的列似乎不包含BLOB/字节数组
使用
var Blobdata=cursor.GetBlob(i)
找出其中的内容,并进行相应调整。我尝试使用ar Blobdata=cursor.GetBlob(I)代码>但仍然会得到相同的错误。你知道如何在类中使用Blob吗?我只在数据库中使用Blob,在代码的其他地方我使用位图或字节数组。如果将cursor.GetBlob
替换为cursor.GetString
,它会返回什么?我尝试了,但无法编译,因为需要将字符串转换为字节数组,所以我尝试了byte[]Blobdata=convert.FromBase64String(cursor.GetString(I))代码>和不工作。我只是有问题,当我添加到数据库时,我使用字节数组,当我只想显示一个时,我转换为位图,工作正常。问题是光标没有getBytes
。您可以尝试的第二部分。让我知道它是否有效!我尝试使用ar Blobdata=cursor.GetBlob(I)代码>但仍然会得到相同的错误。你知道如何在类中使用Blob吗?我只在数据库中使用Blob,在代码的其他地方我使用位图或字节数组。如果将cursor.GetBlob
替换为cursor.GetString
,它会返回什么?我尝试了,但无法编译,因为需要将字符串转换为字节数组,所以我尝试了byte[]Blobdata=convert.FromBase64String(cursor.GetString(I))代码>和不工作。我只是有问题,当我添加到数据库时,我使用字节数组,当我只想显示一个时,我转换为位图,工作正常。问题是光标没有getBytes
。您可以尝试的第二部分。让我知道它是否有效!我尝试使用ar Blobdata=cursor.GetBlob(I)代码>但仍然会得到相同的错误。你知道如何在类中使用Blob吗?我只在数据库中使用Blob,在代码的其他地方我使用位图或字节数组。如果将cursor.GetBlob
替换为cursor.GetString
,它会返回什么?我尝试了,但无法编译,因为需要将字符串转换为字节数组,所以我尝试了byte[]Blobdata=convert.FromBase64String(cursor.GetString(I))代码>和不工作。我只是有问题,当我添加到数据库时,我使用字节数组,当我只想显示一个时,我转换为位图,工作正常。问题是光标没有getBytes
。您可以尝试的第二部分。让我知道它是否有效!