Reputation: 287
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-) Can i pass my holder objects to OnActivityResult method? That would solve all the problems
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);
};
}
}
And in activity class ;
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);
}
}
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
Edit: Or is there anyway to wait for the "tikla" method to complete? That could solve the problem too.
Edit 2: Or can we customize the onActivityResult ? we can send the holder object as paramaters maybe?
Upvotes: 0
Views: 446
Reputation: 5370
[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>
Upvotes: 1