从适配器 xamarin 修改父布局的 textview android
Modify textview of parent layout from adapter xamarin android
抱歉,我正在使用 google 翻译。
您好,我正在使用 visual studio 2017 在 xamarin android 中开发一个应用程序来接收请求,我将信息从片段发送到列表 以存储项目的选择,这是转储到适配器以在另一个片段中显示它。
我的问题是父布局加载了一个自定义列表视图,其中我有 2 个按钮来增加和减少项目的数量这可以正常工作,同样在父布局中它有两个文本视图来计算项目的数量和总价值order ,我希望当增加数量时,这也会从按钮功能所在的适配器更新父布局的文本视图。
如何从适配器访问父布局并修改其值。
布局
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="#FFFFFF">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:orientation="vertical"
android:layout_weight="5">
<TextView
android:text="Cesta de Pedido"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView1"
android:gravity="center"
android:textSize="20dip"
android:textColor="@color/primaryDark" />
<ListView
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/List" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:orientation="vertical"
android:layout_weight="1"
android:background="#000000">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:id="@+id/itemItems"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="6dp"
android:text="(0) Items"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="left"
android:layout_weight="3"
android:textColor="#FFFFFF"/>
<Button
android:id="@+id/btnClear"
android:layout_width="50dp"
android:layout_height="40dp"
android:text="Clear"
android:textAppearance="?android:attr/textAppearanceSmall"
android:background="@drawable/Blue_button"
style="@style/button_text"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/itemTotal"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="6dp"
android:text="S/. 0.00"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="left"
android:layout_weight="3"
android:textColor="#FFFFFF"/>
<Button
android:id="@+id/btnSave"
android:layout_width="50dp"
android:layout_height="40dp"
android:text="Guardar"
android:textAppearance="?android:attr/textAppearanceSmall"
android:background="@drawable/Blue_button"
style="@style/button_text"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
列表视图个性化
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="8dp">
<ImageView
android:id="@+id/Image"
android:layout_width="72dp"
android:layout_height="72dp"
android:layout_alignParentLeft="true"
android:layout_marginRight="8dp"
android:src="@drawable/Pedidos"
android:scaleType="fitXY"/>
<TextView
android:id="@+id/Articulo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignTop="@id/Image"
android:layout_toRightOf="@id/Image"
android:layout_marginTop="0dip"
android:text="This is Line1"
android:lines="2"
android:maxLines="2"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/colorTexto"/>
<TextView
android:id="@+id/MonedaU"
android:layout_width="25dp"
android:layout_height="wrap_content"
android:layout_below="@id/Articulo"
android:layout_marginTop="0dip"
android:text="S/."
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_toRightOf="@id/Image"/>
<TextView
android:id="@+id/PrecioU"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/Articulo"
android:layout_marginTop="0dip"
android:text="This is line2"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_toRightOf="@id/MonedaU"
android:singleLine="true"/>
<TextView
android:id="@+id/MonedaT"
android:layout_width="25dp"
android:layout_height="wrap_content"
android:layout_below="@id/Articulo"
android:layout_marginTop="20dip"
android:text="S/."
android:textAppearance="?android:attr/textAppearanceListItem"
android:layout_toRightOf="@id/Image"
android:textStyle="bold"
android:textColor="@color/colorResaltado"/>
<TextView
android:id="@+id/Importe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/Articulo"
android:layout_marginTop="20dip"
android:text="This is line3"
android:textAppearance="?android:attr/textAppearanceListItem"
android:layout_toRightOf="@id/MonedaT"
android:textStyle="bold"
android:textColor="@color/colorResaltado"/>
<Button
android:id="@+id/btnMin"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_below="@id/Articulo"
android:layout_toLeftOf="@+id/Cantidad"
android:text="-"
android:textAppearance="?android:attr/textAppearanceSmall"
android:background="@drawable/red_button"
style="@style/button_text" />
<EditText
android:id="@+id/Cantidad"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/btnMax"
android:layout_marginTop="5dip"
android:layout_below="@id/Articulo"
android:textColor="#000000"
android:background="#FFFFFF"
android:text="0"
android:singleLine="true"
android:maxLines="1"
android:inputType="number"
android:gravity="center"
android:textAlignment="center"/>
<Button
android:id="@+id/btnMax"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_below="@id/Articulo"
android:layout_toLeftOf="@+id/btnDel"
android:text="+"
android:textAppearance="?android:attr/textAppearanceSmall"
android:background="@drawable/red_button"
android:layout_gravity="right"
style="@style/button_text" />
<ImageButton
android:id="@+id/btnDel"
android:src="@drawable/Trash_"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_below="@id/Articulo"
android:layout_alignParentRight="true"
style="@style/button_text" />
</RelativeLayout>
片段
public class WishFragment : Fragment
{
public WishFragment MyFragment()
{
return this;
}
public WishFragment()
{
RetainInstance = true;
}
private ListView WishlistView;
WishAdapter adapter;
List<EN_WishDetalle> List_Wish = new List<EN_WishDetalle>();
public override View OnCreateView(LayoutInflater inflater, Android.Views.ViewGroup container, Android.OS.Bundle savedInstanceState)
{
base.OnCreateView(inflater, container, savedInstanceState);
//HasOptionsMenu = true;
var view = inflater.Inflate(Resource.Layout.Main_Wish, null);
WishlistView = view.FindViewById<ListView>(Resource.Id.List);
EditText txtCantidad = view.FindViewById<EditText>(Resource.Id.Cantidad);
List_Wish = Variables.WishDetalle;
adapter = new WishAdapter(Activity, List_Wish);
WishlistView.Adapter = adapter;
WishlistView = view.FindViewById<ListView>(Resource.Id.List);
Button buttonMax = view.FindViewById<Button>(Resource.Id.btnMax);
Button buttonMin = view.FindViewById<Button>(Resource.Id.btnMin);
ImageButton buttonDel = view.FindViewById<ImageButton>(Resource.Id.btnDel);
TextView txtContador = view.FindViewById<TextView>(Resource.Id.itemItems);
TextView txtTotal = view.FindViewById<TextView>(Resource.Id.itemTotal);
WishlistView.ItemClick += buttonMax_ItemClick;
txtContador.Text ="( " + List_Wish.Count.ToString() + " ) Items";
decimal suma = List_Wish.Sum(item => Convert.ToDecimal(item.importetotal));
txtTotal.Text = "S/. " + suma.ToString("N");
//WishlistView.ItemClick += buttonMin_ItemClick;
//WishlistView.ItemClick += buttonDel_ItemClick;
return view;
}
void buttonMax_ItemClick(object sender,AdapterView.ItemClickEventArgs e)
{
}
void buttonMin_ItemClick(object sender, AdapterView.ItemClickEventArgs x)
{
}
void buttonDel_ItemClick(object sender, AdapterView.ItemClickEventArgs z)
{
}
}
适配器
public class WishAdapter : BaseAdapter<EN_WishDetalle>
{
List<EN_WishDetalle> sList;
Context sContext;
public WishAdapter(Context context, List<EN_WishDetalle> list)
{
sList = list;
sContext = context;
}
public override EN_WishDetalle this[int position]
{
get
{
return sList[position];
}
}
public override int Count
{
get
{
return sList.Count;
}
}
public override long GetItemId(int position)
{
return position;
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
var item = sList[position];
View view = convertView; // re-use an existing view, if one is available
MyViewHolder holder;
if (view != null)
{
holder = view.Tag as MyViewHolder;
holder.buttonDel.Tag = position;
//holder.buttonMax.Tag = position;
//holder.buttonMin.Tag = position;
}
else
{ // otherwise create a new one
holder = new MyViewHolder();
LayoutInflater inflater =
(LayoutInflater)sContext.GetSystemService(Context.LayoutInflaterService);
view = inflater.Inflate(Resource.Layout.item_Wish, null);
holder.txtPrecioU = view.FindViewById<TextView>(Resource.Id.PrecioU);
holder.txtArticulo = view.FindViewById<TextView>(Resource.Id.Articulo);
holder.txtCantidad = view.FindViewById<TextView>(Resource.Id.Cantidad);
holder.txtImporte = view.FindViewById<TextView>(Resource.Id.Importe);
holder.txtMonedaU = view.FindViewById<TextView>(Resource.Id.MonedaU);
holder.txtMonedaT = view.FindViewById<TextView>(Resource.Id.MonedaT);
holder.Art = view.FindViewById<ImageView>(Resource.Id.Image);
holder.buttonMax = view.FindViewById<Button>(Resource.Id.btnMax);
holder.buttonMin = view.FindViewById<Button>(Resource.Id.btnMin);
holder.buttonDel = view.FindViewById<ImageButton>(Resource.Id.btnDel);
holder.buttonMax.Click += delegate
{
sList[position].cantidad = sList[position].cantidad + 1;
sList[position].importetotal = Convert.ToString( sList[position].cantidad * Convert.ToDecimal(item.punitario));
holder.txtCantidad.SetText(Convert.ToString(sList[position].cantidad), TextView.BufferType.Normal);
string format_importe = (sList[position].cantidad * Convert.ToDecimal(item.punitario)).ToString("N");
holder.txtImporte.SetText(format_importe, TextView.BufferType.Normal);
};
holder.buttonMin.Click += delegate
{
if (sList[position].cantidad > 1) {
sList[position].cantidad = sList[position].cantidad - 1;
sList[position].importetotal = Convert.ToString(sList[position].cantidad * Convert.ToDecimal(item.punitario));
holder.txtCantidad.SetText(Convert.ToString(sList[position].cantidad), TextView.BufferType.Normal);
string format_importe = (sList[position].cantidad * Convert.ToDecimal(item.punitario)).ToString("N");
holder.txtImporte.SetText(format_importe, TextView.BufferType.Normal);
}
};
holder.buttonDel.Click += delegate
{ // we get the tag here for buttonDel
int x = (int)holder.buttonDel.Tag;
Android.App.AlertDialog.Builder dialog = new AlertDialog.Builder(sContext);
AlertDialog alert = dialog.Create();
alert.SetTitle("GumisaAPP");
alert.SetMessage("Eliminar item : (" + x.ToString() + ") - " + sList[position].codigo + sList[position].articulo);
alert.SetIcon(Resource.Drawable.Alerta);
alert.SetButton("OK", (c, ev) =>
{
sList.RemoveAt(position);
NotifyDataSetChanged();
});
alert.SetButton2("CANCEL", (c, ev) =>
{
});
alert.Show();
};
holder.buttonDel.Tag = position;
view.Tag = holder;
}
holder.txtPrecioU.Text = Convert.ToDecimal(item.punitario).ToString("N");
holder.txtArticulo.Text = item.articulo;
holder.txtCantidad.Text = item.cantidad.ToString();
holder.txtImporte.Text = Convert.ToDecimal(item.importetotal).ToString("N");
holder.txtMonedaU.Text = item.moneda;
holder.txtMonedaT.Text = item.moneda;
holder.Art.SetImageResource(Android.Resource.Color.Transparent);
if (sList[position].imagenproducto == "")
{
holder.Art.SetImageResource(Resource.Drawable.NoDisponible);
}
else
{
Android.Net.Uri myUri = (Android.Net.Uri.Parse(sList[position].imagenproducto));
//Art.SetImageURI(myUri);
holder.Art.SetImageURI(myUri);
}
return view;
}
public class MyViewHolder : Java.Lang.Object
{
public TextView txtPrecioU { get; set; }
public TextView txtArticulo { get; set; }
public TextView txtCantidad { get; set; }
public TextView txtImporte { get; set; }
public TextView txtMonedaU { get; set; }
public TextView txtMonedaT { get; set; }
public ImageView Art { get; set; }
public Button buttonMax { get; set; }
public Button buttonMin { get; set; }
public ImageButton buttonDel { get; set; }
}
}
enter image description here
enter image description here
希望你能指导我的方法,谢谢你的时间。
是的,您可以使用 event
来实现这一点,就像 ToolmakerSteve 提到的那样。
我做了一个简单的demo,给主页面传一个int
参数,你可以参考下面的代码:
1.in 你的适配器,定义变量 MyEvent
:
public class WishAdapter : BaseAdapter<EN_WishDetalle>
{
// add EventHandler here
public event EventHandler<int> MyEvent;
//other code
public WishAdapter(Context context, List<EN_WishDetalle> list)
{
sList = list;
sContext = context;
}
//other code
}
2.in class WishAdapter
的方法 GetView
,为按钮添加代码 buttonMax
:
holder.buttonMax.Click += delegate
{
// other code
// invoke here
MyEvent?.Invoke(this, 1);
};
3.In WishFragment
,这样做:
adapter = new WishAdapter(Activity, List_Wish);
WishlistView.Adapter = adapter;
adapter.MyEvent += Adapter_MyEvent;
方法Adapter_MyEvent
是:
private void Adapter_MyEvent(object sender, int e)
{ // here we can get the passed parameter e
Debug.WriteLine("The passed parameter : " + e);
}
抱歉,我正在使用 google 翻译。
您好,我正在使用 visual studio 2017 在 xamarin android 中开发一个应用程序来接收请求,我将信息从片段发送到列表
布局
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="#FFFFFF">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:orientation="vertical"
android:layout_weight="5">
<TextView
android:text="Cesta de Pedido"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView1"
android:gravity="center"
android:textSize="20dip"
android:textColor="@color/primaryDark" />
<ListView
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/List" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:orientation="vertical"
android:layout_weight="1"
android:background="#000000">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:id="@+id/itemItems"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="6dp"
android:text="(0) Items"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="left"
android:layout_weight="3"
android:textColor="#FFFFFF"/>
<Button
android:id="@+id/btnClear"
android:layout_width="50dp"
android:layout_height="40dp"
android:text="Clear"
android:textAppearance="?android:attr/textAppearanceSmall"
android:background="@drawable/Blue_button"
style="@style/button_text"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/itemTotal"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="6dp"
android:text="S/. 0.00"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="left"
android:layout_weight="3"
android:textColor="#FFFFFF"/>
<Button
android:id="@+id/btnSave"
android:layout_width="50dp"
android:layout_height="40dp"
android:text="Guardar"
android:textAppearance="?android:attr/textAppearanceSmall"
android:background="@drawable/Blue_button"
style="@style/button_text"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
列表视图个性化
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="8dp">
<ImageView
android:id="@+id/Image"
android:layout_width="72dp"
android:layout_height="72dp"
android:layout_alignParentLeft="true"
android:layout_marginRight="8dp"
android:src="@drawable/Pedidos"
android:scaleType="fitXY"/>
<TextView
android:id="@+id/Articulo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignTop="@id/Image"
android:layout_toRightOf="@id/Image"
android:layout_marginTop="0dip"
android:text="This is Line1"
android:lines="2"
android:maxLines="2"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/colorTexto"/>
<TextView
android:id="@+id/MonedaU"
android:layout_width="25dp"
android:layout_height="wrap_content"
android:layout_below="@id/Articulo"
android:layout_marginTop="0dip"
android:text="S/."
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_toRightOf="@id/Image"/>
<TextView
android:id="@+id/PrecioU"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/Articulo"
android:layout_marginTop="0dip"
android:text="This is line2"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_toRightOf="@id/MonedaU"
android:singleLine="true"/>
<TextView
android:id="@+id/MonedaT"
android:layout_width="25dp"
android:layout_height="wrap_content"
android:layout_below="@id/Articulo"
android:layout_marginTop="20dip"
android:text="S/."
android:textAppearance="?android:attr/textAppearanceListItem"
android:layout_toRightOf="@id/Image"
android:textStyle="bold"
android:textColor="@color/colorResaltado"/>
<TextView
android:id="@+id/Importe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/Articulo"
android:layout_marginTop="20dip"
android:text="This is line3"
android:textAppearance="?android:attr/textAppearanceListItem"
android:layout_toRightOf="@id/MonedaT"
android:textStyle="bold"
android:textColor="@color/colorResaltado"/>
<Button
android:id="@+id/btnMin"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_below="@id/Articulo"
android:layout_toLeftOf="@+id/Cantidad"
android:text="-"
android:textAppearance="?android:attr/textAppearanceSmall"
android:background="@drawable/red_button"
style="@style/button_text" />
<EditText
android:id="@+id/Cantidad"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/btnMax"
android:layout_marginTop="5dip"
android:layout_below="@id/Articulo"
android:textColor="#000000"
android:background="#FFFFFF"
android:text="0"
android:singleLine="true"
android:maxLines="1"
android:inputType="number"
android:gravity="center"
android:textAlignment="center"/>
<Button
android:id="@+id/btnMax"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_below="@id/Articulo"
android:layout_toLeftOf="@+id/btnDel"
android:text="+"
android:textAppearance="?android:attr/textAppearanceSmall"
android:background="@drawable/red_button"
android:layout_gravity="right"
style="@style/button_text" />
<ImageButton
android:id="@+id/btnDel"
android:src="@drawable/Trash_"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_below="@id/Articulo"
android:layout_alignParentRight="true"
style="@style/button_text" />
</RelativeLayout>
片段
public class WishFragment : Fragment
{
public WishFragment MyFragment()
{
return this;
}
public WishFragment()
{
RetainInstance = true;
}
private ListView WishlistView;
WishAdapter adapter;
List<EN_WishDetalle> List_Wish = new List<EN_WishDetalle>();
public override View OnCreateView(LayoutInflater inflater, Android.Views.ViewGroup container, Android.OS.Bundle savedInstanceState)
{
base.OnCreateView(inflater, container, savedInstanceState);
//HasOptionsMenu = true;
var view = inflater.Inflate(Resource.Layout.Main_Wish, null);
WishlistView = view.FindViewById<ListView>(Resource.Id.List);
EditText txtCantidad = view.FindViewById<EditText>(Resource.Id.Cantidad);
List_Wish = Variables.WishDetalle;
adapter = new WishAdapter(Activity, List_Wish);
WishlistView.Adapter = adapter;
WishlistView = view.FindViewById<ListView>(Resource.Id.List);
Button buttonMax = view.FindViewById<Button>(Resource.Id.btnMax);
Button buttonMin = view.FindViewById<Button>(Resource.Id.btnMin);
ImageButton buttonDel = view.FindViewById<ImageButton>(Resource.Id.btnDel);
TextView txtContador = view.FindViewById<TextView>(Resource.Id.itemItems);
TextView txtTotal = view.FindViewById<TextView>(Resource.Id.itemTotal);
WishlistView.ItemClick += buttonMax_ItemClick;
txtContador.Text ="( " + List_Wish.Count.ToString() + " ) Items";
decimal suma = List_Wish.Sum(item => Convert.ToDecimal(item.importetotal));
txtTotal.Text = "S/. " + suma.ToString("N");
//WishlistView.ItemClick += buttonMin_ItemClick;
//WishlistView.ItemClick += buttonDel_ItemClick;
return view;
}
void buttonMax_ItemClick(object sender,AdapterView.ItemClickEventArgs e)
{
}
void buttonMin_ItemClick(object sender, AdapterView.ItemClickEventArgs x)
{
}
void buttonDel_ItemClick(object sender, AdapterView.ItemClickEventArgs z)
{
}
}
适配器
public class WishAdapter : BaseAdapter<EN_WishDetalle>
{
List<EN_WishDetalle> sList;
Context sContext;
public WishAdapter(Context context, List<EN_WishDetalle> list)
{
sList = list;
sContext = context;
}
public override EN_WishDetalle this[int position]
{
get
{
return sList[position];
}
}
public override int Count
{
get
{
return sList.Count;
}
}
public override long GetItemId(int position)
{
return position;
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
var item = sList[position];
View view = convertView; // re-use an existing view, if one is available
MyViewHolder holder;
if (view != null)
{
holder = view.Tag as MyViewHolder;
holder.buttonDel.Tag = position;
//holder.buttonMax.Tag = position;
//holder.buttonMin.Tag = position;
}
else
{ // otherwise create a new one
holder = new MyViewHolder();
LayoutInflater inflater =
(LayoutInflater)sContext.GetSystemService(Context.LayoutInflaterService);
view = inflater.Inflate(Resource.Layout.item_Wish, null);
holder.txtPrecioU = view.FindViewById<TextView>(Resource.Id.PrecioU);
holder.txtArticulo = view.FindViewById<TextView>(Resource.Id.Articulo);
holder.txtCantidad = view.FindViewById<TextView>(Resource.Id.Cantidad);
holder.txtImporte = view.FindViewById<TextView>(Resource.Id.Importe);
holder.txtMonedaU = view.FindViewById<TextView>(Resource.Id.MonedaU);
holder.txtMonedaT = view.FindViewById<TextView>(Resource.Id.MonedaT);
holder.Art = view.FindViewById<ImageView>(Resource.Id.Image);
holder.buttonMax = view.FindViewById<Button>(Resource.Id.btnMax);
holder.buttonMin = view.FindViewById<Button>(Resource.Id.btnMin);
holder.buttonDel = view.FindViewById<ImageButton>(Resource.Id.btnDel);
holder.buttonMax.Click += delegate
{
sList[position].cantidad = sList[position].cantidad + 1;
sList[position].importetotal = Convert.ToString( sList[position].cantidad * Convert.ToDecimal(item.punitario));
holder.txtCantidad.SetText(Convert.ToString(sList[position].cantidad), TextView.BufferType.Normal);
string format_importe = (sList[position].cantidad * Convert.ToDecimal(item.punitario)).ToString("N");
holder.txtImporte.SetText(format_importe, TextView.BufferType.Normal);
};
holder.buttonMin.Click += delegate
{
if (sList[position].cantidad > 1) {
sList[position].cantidad = sList[position].cantidad - 1;
sList[position].importetotal = Convert.ToString(sList[position].cantidad * Convert.ToDecimal(item.punitario));
holder.txtCantidad.SetText(Convert.ToString(sList[position].cantidad), TextView.BufferType.Normal);
string format_importe = (sList[position].cantidad * Convert.ToDecimal(item.punitario)).ToString("N");
holder.txtImporte.SetText(format_importe, TextView.BufferType.Normal);
}
};
holder.buttonDel.Click += delegate
{ // we get the tag here for buttonDel
int x = (int)holder.buttonDel.Tag;
Android.App.AlertDialog.Builder dialog = new AlertDialog.Builder(sContext);
AlertDialog alert = dialog.Create();
alert.SetTitle("GumisaAPP");
alert.SetMessage("Eliminar item : (" + x.ToString() + ") - " + sList[position].codigo + sList[position].articulo);
alert.SetIcon(Resource.Drawable.Alerta);
alert.SetButton("OK", (c, ev) =>
{
sList.RemoveAt(position);
NotifyDataSetChanged();
});
alert.SetButton2("CANCEL", (c, ev) =>
{
});
alert.Show();
};
holder.buttonDel.Tag = position;
view.Tag = holder;
}
holder.txtPrecioU.Text = Convert.ToDecimal(item.punitario).ToString("N");
holder.txtArticulo.Text = item.articulo;
holder.txtCantidad.Text = item.cantidad.ToString();
holder.txtImporte.Text = Convert.ToDecimal(item.importetotal).ToString("N");
holder.txtMonedaU.Text = item.moneda;
holder.txtMonedaT.Text = item.moneda;
holder.Art.SetImageResource(Android.Resource.Color.Transparent);
if (sList[position].imagenproducto == "")
{
holder.Art.SetImageResource(Resource.Drawable.NoDisponible);
}
else
{
Android.Net.Uri myUri = (Android.Net.Uri.Parse(sList[position].imagenproducto));
//Art.SetImageURI(myUri);
holder.Art.SetImageURI(myUri);
}
return view;
}
public class MyViewHolder : Java.Lang.Object
{
public TextView txtPrecioU { get; set; }
public TextView txtArticulo { get; set; }
public TextView txtCantidad { get; set; }
public TextView txtImporte { get; set; }
public TextView txtMonedaU { get; set; }
public TextView txtMonedaT { get; set; }
public ImageView Art { get; set; }
public Button buttonMax { get; set; }
public Button buttonMin { get; set; }
public ImageButton buttonDel { get; set; }
}
}
enter image description here
enter image description here
希望你能指导我的方法,谢谢你的时间。
是的,您可以使用 event
来实现这一点,就像 ToolmakerSteve 提到的那样。
我做了一个简单的demo,给主页面传一个int
参数,你可以参考下面的代码:
1.in 你的适配器,定义变量 MyEvent
:
public class WishAdapter : BaseAdapter<EN_WishDetalle>
{
// add EventHandler here
public event EventHandler<int> MyEvent;
//other code
public WishAdapter(Context context, List<EN_WishDetalle> list)
{
sList = list;
sContext = context;
}
//other code
}
2.in class WishAdapter
的方法 GetView
,为按钮添加代码 buttonMax
:
holder.buttonMax.Click += delegate
{
// other code
// invoke here
MyEvent?.Invoke(this, 1);
};
3.In WishFragment
,这样做:
adapter = new WishAdapter(Activity, List_Wish);
WishlistView.Adapter = adapter;
adapter.MyEvent += Adapter_MyEvent;
方法Adapter_MyEvent
是:
private void Adapter_MyEvent(object sender, int e)
{ // here we can get the passed parameter e
Debug.WriteLine("The passed parameter : " + e);
}