简体   繁体   English

带有图像的xamarin android ListView

[英]xamarin android ListView with image

Im tring to load data from database and display on a ListView. 试图从数据库加载数据并显示在ListView上。 The list view contains 2 textview and 1 image view. 列表视图包含2个文本视图和1个图像视图。 Im using SQlite.net. 我正在使用SQlite.net。 Where is my databse: 我的数据库在哪里:

Database 数据库

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; }
}

And where is my function to fill the 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);
    }

The only problem is the image dont show and I have this error from the output: 唯一的问题是图像不显示,并且输出中出现此错误:

[BitmapFactory] Unable to decode stream: java.io.FileNotFoundException: /android.graphics.Bitmap@42087da0: open failed: ENOENT (No such file or directory)
[System.out] resolveUri failed on bad bitmap uri: android.graphics.Bitmap@42087da0

I already done this application with java for android and I fix that problem with the this code but I dont know how to "translate" the code from java to c#. 我已经使用Java for android完成了该应用程序,并使用此代码解决了该问题,但是我不知道如何将代码从Java“翻译”为c#。

Where is the java code below the adap: 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;
            }
        }
    });

Somebody can help me "translate" this code to c#? 有人可以帮助我将此代码“翻译”为C#吗? or use another code. 或使用其他代码。 Thanks from the help and sorry my english. 感谢您的帮助,对不起我的英语。

EDITED 2 编辑2

Ok where is the code translated to c# 确定代码在哪里翻译成C#

class ViewBinder : Java.Lang.Object, SimpleCursorAdapter.IViewBinder
    {     
        public bool SetViewValue (View view, Android.Database.ICursor cursor, int i)
        {
            if (view.Id == Resource.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;
            }
        }     
    }

But now I got an error in this line: 但是现在我在这一行出现了一个错误:

byte[] Blobdata = cursor.GetBlob (i);

Error: 错误:

android.graphics.Bitmap cannot be cast to byte[]

I think im getting this error becouse in my database im not using Blob but byte array. 我认为我在数据库中收到此错误是因为我没有使用Blob而是字节数组。 Somebody know how I can retrive byte array from database or how i can use Blob data type? 有人知道我如何从数据库中检索字节数组,或者如何使用Blob数据类型?

You can store a byte-array in a BLOB. 您可以将字节数组存储在BLOB中。

This is how I fetch it: 这是我的获取方式:

using (SqliteDataReader reader = command.ExecuteReader())
{
    while (reader.Read())
    {
        images.Add(new Image((byte[])reader["Image"]));
    }
}

I can then use my Image property ImageByteArray to convert it to a Bitmap, similar to the way you did. 然后,可以使用我的Image属性ImageByteArray将其转换为Bitmap,类似于您的方法。

Bitmap image = BitmapFactory.DecodeByteArray(data, 0, barray.Length);

The Bitmap is then ready to show in an ImageView . 然后, Bitmap准备好在ImageView显示。

imageView.SetImageBitmap(image);

As you can see, our methods are very much alike. 如您所见,我们的方法非常相似。 So to me it seems cursor.GetBlob (i) returns something unexpected. 所以在我看来, cursor.GetBlob (i)返回了意外的内容。

public abstract byte[] getBlob (int columnIndex) 公共抽象字节[] getBlob(int columnIndex)

Added in API level 1 Returns the value of the requested column as a byte array. 在API级别1中添加。以字节数组形式返回请求的列的值。

The result and whether this method throws an exception when the column value is null or the column type is not a blob type is implementation-defined. 结果以及此方法是否在列值为null或列类型不是blob类型时引发异常是实现定义的。

Parameters: columnIndex the zero-based index of the target column. 参数:columnIndex目标列的从零开始的索引。

Returns: the value of that column as a byte array. 返回:该列的值作为字节数组。

( http://developer.android.com/reference/android/database/Cursor.html#getBlob(int) ) http://developer.android.com/reference/android/database/Cursor.html#getBlob(int)

According to the description from the method, it seems your column does not hold a BLOB/byte-array. 根据方法的描述,您的列似乎不包含BLOB /字节数组。

Use var Blobdata = cursor.GetBlob (i); 使用var Blobdata = cursor.GetBlob (i); to find out what is in it, and adjust accordingly. 找出其中的内容,并据此进行调整。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM