Xamarin从图库中选择图像并在Listview适配器中显示它

时间:2016-11-17 11:05:12

标签: android listview xamarin

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

因此,所有listview的对象都在适配器中定义,您无法从适配器创建库选择。所以我通过访问活动来创建它。

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

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

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

    }
}

因此它在图库打开时调用该函数,适配器尝试将图像uri设置为无。

先谢谢你。对不起复杂的帖子

编辑:或者无论如何都要等待&#34; tikla&#34;方法完成?这也可以解决问题。

编辑2:或者我们可以自定义onActivityResult吗?我们可以发送持有者对象作为参数吗?

1 个答案:

答案 0 :(得分:1)

[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

<?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

<?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>

enter image description here