我试图在Android gridview上显示来自sqlite数据库的图像

时间:2018-06-22 10:01:15

标签: c# android sqlite visual-studio-2017

我的代码可以显示资源文件夹中的图像,但是从sqlite表中检索图像时却无法正常工作。在适配器和模型中,类型为int。哪个工作正常,但现在我正在从Blob检索一个字节数组。不知道如何调整模型/适配器。欣赏提示:)

下面是itemmodel代码,适配器getview方法和主要活动,sqlite db中的字段定义为blob。

class MyItemModel
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int ImageSource  { get; set; }
}

class MyGridViewAdapter : BaseAdapter<MyItemModel>
{
    public ObservableCollection<MyItemModel> items;
    public Activity context;

    public MyGridViewAdapter(Activity context, ObservableCollection<MyItemModel> items)
    {
        this.items = items;
        this.context = context;
    }

    public override MyItemModel this[int position]
    {
        get
        {
            return items[position];
        }
    }

    public override int Count
    {
        get
        {
            return items.Count;
        }
    }

    public override long GetItemId(int position)
    {
        return position;
    }

    public override View GetView(int position, View convertView, ViewGroup parent)
    {
        View view = convertView;
        if (view == null)
        {
            view = context.LayoutInflater.Inflate(Resource.Layout.MyGridViewCell, null);
        }

        //view.FindViewById<ImageView>(Resource.Id.image).SetImageResource(items[position].ImageSource);
        var image = view.FindViewById<ImageView>(Resource.Id.image);
        image.SetImageResource(items[position].ImageSource); 

        //view.FindViewById<TextView>(Resource.Id.name).Text = items[position].Name;
        var name = view.FindViewById<TextView>(Resource.Id.name);
        name.Text = items[position].Name;

        return view;
    }
}

public class MainActivity : Activity
{
    ObservableCollection<MyItemModel> items = new ObservableCollection<MyItemModel>();


    //int count = 1;

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.Main);

        //set the toolbar
        var toolbar = FindViewById<Toolbar>(Resource.Id.toolbar);
        SetActionBar(toolbar);
        ActionBar.Title = "Let's cook!";

        //Start with creating events on the fav, last and search entry 
        GridView recipegridview = FindViewById<GridView>(Resource.Id.gridview);

        recipegridview.ItemClick += delegate (object sender, AdapterView.ItemClickEventArgs args) {
            Toast.MakeText(this, args.Position.ToString(), ToastLength.Short).Show();
            Console.WriteLine("Key is : "+ items[args.Position].Id);
        };

        //open the db connection and provide initial load of recipes in gridview.
        //check if db is there, if not create a blanc one
        DBRepository dbr = new DBRepository();
        var result = dbr.FirstTimeDB();
        if (result != "") //We got a message back - SQLite exception or new blanc db created
        {
            Toast.MakeText(this, result, ToastLength.Long).Show();
        }
        else
        {
            var records = dbr.GetAllRecords();
            if (records == null)
            {
                Toast.MakeText(this, "No recipes found!", ToastLength.Long).Show();
            }
            else
            {
                //process the recipes

                foreach (var recept in records)
                {
                    Console.WriteLine("Recept : " + recept.RName + "\n");
                    //add your items here.
                    //var bitmap = BitmapFactory.DecodeByteArray(recept.RImage, 0, recept.RImage.Length);

                    int imageint;
                    try
                    {
                        imageint = BitConverter.ToInt32(recept.RImage);
                    }
                    catch (System.Exception ex)
                    {
                       Console.WriteLine("Image could not be converted to int due to " + ex.Message);
                        imageint = Resource.Drawable.favorite;
                    }
                    //items.Add(new MyItemModel { Id = recept.RID, ImageSource = imageint, Name = recept.RName });
                    //items.Add(new MyItemModel { Id = recept.RID, ImageSource = Resource.Drawable.favorite, Name = recept.RName });
                    items.Add(new MyItemModel { Id = recept.RID, ImageSource = recept.RImage, Name = recept.RName });
                }
                MyGridViewAdapter adapter = new MyGridViewAdapter(this, items);
                recipegridview.Adapter = adapter;
            }
        }
    }

    public override bool OnCreateOptionsMenu(IMenu menu)
    {
        MenuInflater.Inflate(Resource.Menu.top_menus, menu);
        return base.OnCreateOptionsMenu(menu);
    }
    public override bool OnOptionsItemSelected(IMenuItem item)
    {
        Toast.MakeText(this, "Action selected: " + item.TitleFormatted,
            ToastLength.Short).Show();
        return base.OnOptionsItemSelected(item);
    }
}

0 个答案:

没有答案