Xamarin从库中选择图像并将其显示在Listview适配器中 [英] Xamarin Selecting An Image From Gallery And Displaying It In The Listview Adapter

查看:75
本文介绍了Xamarin从库中选择图像并将其显示在Listview适配器中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个问题.我有一个listview,listview适配器和一个listview所在的活动.每个listview列都有一个按钮,它允许您从图库中选择图像.

I have few questions. I have a listview,listview adapter and an activity that the listview is in. And there is a button for each listview column and it lets you choose an image from the gallery.

因此所有列表视图的对象都在适配器中定义,您不能从适配器创建画廊选择.因此,我通过访问活动来创建它.

So all listview's objects are defined in the Adapter and you cant create the gallery selection from the Adapter. So im creating it with accessing to the activity.

1-)我可以将持有人对象传递给OnActivityResult方法吗?这样可以解决所有问题

1-) Can i pass my holder objects to OnActivityResult method? That would solve all the problems

2-)那么我该如何选择图像,然后将其传递给适配器?我尝试对图像 uri 使用静态变量,但它没有同步.在我的适配器类中

2-) So how do i choose the image and then pass it to my adapter? I tried using a static variable for image uri but its not synchronizated. In my adapter class

public class Duzenle_Adapter
{
private LayoutInflater inflater;
public static Android.Net.Uri static_uri;
 public Duzenle_Adapter(Context context, int resource, List<Yemek_Liste> objects,Duzenle_Activity d) : base(context, resource, objects)
    {
        this.c = context;
        this.resource = resource;
        this.yemekler = objects;
        this.d = d;
    }

    public override View GetView(int position, View convertView, ViewGroup parent)
    {
        if (inflater == null)
        {
            inflater = (LayoutInflater)c.GetSystemService(Context.LayoutInflaterService);
        }
        if (convertView == null)
        {
            convertView = inflater.Inflate(resource, parent, false);
        }
        //List<Yemek_Liste> yemekler = new List<Yemek_Liste>();
        //yemekler = db.selectItem();
        Tutan_Duzenle tut = new Tutan_Duzenle(convertView);
        tut.img.SetImageResource(yemekler[position].Get_ImageID());//default image


        if (!tut.res_degis.HasOnClickListeners)
        {
            tut.res_degis.Click += delegate
            {

                d.tikla();
                tut.img.SetImageURI(static_uri);
            };
        }
   }

在活动课中;

public class Duzenle_Activity:Activity
{
  ......

  protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
    {
        base.OnActivityResult(requestCode, resultCode, data);

        if (resultCode == Result.Ok)
        {

            Settings_Adapter.static_uri = data.Data;


        }
    }
    public void tikla()
    {
        var imageIntent = new Intent();
        imageIntent.SetType("image/*");
        imageIntent.SetAction(Intent.ActionGetContent);
        StartActivityForResult(
            Intent.CreateChooser(imageIntent, "Select photo"), 0);

    }
}

因此,当图库打开时,它会调用该函数,适配器尝试将image uri设置为none.

So it calls the function when the gallery opens up the adapter tries to set image uri to none.

非常感谢.抱歉,帖子很复杂

Thanks in the advance. Sorry for the complicated post

还是有等待"tikla"方法完成的东西?那也可以解决问题.

Or is there anyway to wait for the "tikla" method to complete? That could solve the problem too.

或者我们可以自定义onActivityResult吗?我们可以将参数作为参数发送给持有人吗?

Edit 2: Or can we customize the onActivityResult ? we can send the holder object as paramaters maybe?

推荐答案

[Activity(Label = "Duzenle_Activity", MainLauncher = true, Icon = "@drawable/icon")]
public class Duzenle_Activity : Activity
{
    ListView listView1;
    List<Yemek> objects = new List<Yemek>();

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

        SetContentView(Resource.Layout.ChooseImageLayout);

        listView1 = FindViewById<ListView>(Resource.Id.listView1);
        for (int i = 0; i < 20; i++)
        {
            objects.Add(new Yemek { ButtonText = $"Select {i}" });
        }

        listView1.Adapter = new Duzenle_Adapter(this, objects);


    }

    public void tikla()
    {
        var imageIntent = new Intent();
        imageIntent.SetType("image/*");
        imageIntent.SetAction(Intent.ActionGetContent);
        StartActivityForResult(
            Intent.CreateChooser(imageIntent, "Select photo"), 0);

    }

    protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
    {
        base.OnActivityResult(requestCode, resultCode, data);

        if (resultCode == Result.Ok)
        {
            Duzenle_Adapter adapter = ((Duzenle_Adapter)listView1.Adapter);
            adapter.SelectedYemek.ImageUri = data.Data;
            adapter.NotifyDataSetChanged();
            //or listView1.Invalidate();
        }
    }
}

public class Yemek
{
    public string ButtonText { get; set; }
    public Android.Net.Uri ImageUri { get; set; }
}

public class Duzenle_Adapter : BaseAdapter<Yemek>
{
    List<Yemek> yemeklerList;
    Duzenle_Activity activity;
    int selectedPosition=-1;

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

    public override Yemek this[int position]
    {
        get
        {
            return yemeklerList[position];
        }
    }

    public Duzenle_Adapter(Context context, List<Yemek> objects)
    {
        this.activity = (Duzenle_Activity)context;
        this.yemeklerList = objects;
    }

    public override View GetView(int position, View convertView, ViewGroup parent)
    {
        if (convertView == null) // no view to re-use, create new
            convertView = activity.LayoutInflater.Inflate(Resource.Layout.yemekler_cell, parent, false);

        var yemekler = yemeklerList[position];

        var btn = convertView.FindViewById<Button>(Resource.Id.button1);
        btn.Text = yemekler.ButtonText;
        btn.Tag = position; //store item position in Tag

        if (!btn.HasOnClickListeners)
            btn.Click += Btn_Click;
        var imgView = convertView.FindViewById<ImageView>(Resource.Id.imageView1);
        if (yemeklerList[position].ImageUri == null)
            imgView.SetImageResource(Resource.Drawable.Icon);
        else
            imgView.SetImageURI(yemeklerList[position].ImageUri);

        return convertView;
    }

    private void Btn_Click(object sender, EventArgs e)
    {
        selectedPosition = (int)((Button)sender).Tag; //store selected item position
        activity.tikla();
    }

    public override long GetItemId(int position)
    {

        return yemeklerList[position].GetHashCode(); //not used
    }

    public Yemek SelectedYemek
    {
        get
        {
            return yemeklerList[selectedPosition];
        }
    }
}

yemekler_cell.axml

yemekler_cell.axml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <Button
        android:text="Select Image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button1" />
    <ImageView
        android:src="@android:drawable/ic_menu_gallery"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView1"
        android:layout_gravity="center" />
</LinearLayout>

ChooseImageLayout.axml

ChooseImageLayout.axml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:minWidth="25px"
    android:minHeight="25px">
    <ListView
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/listView1" />
</LinearLayout>

这篇关于Xamarin从库中选择图像并将其显示在Listview适配器中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆