Home > Enterprise >  Modify textview of parent layout from adapter xamarin android
Modify textview of parent layout from adapter xamarin android

Time:05-17

Sorry, I'm using google translate. Hello, I am developing an application in xamarin android with visual studio 2017 to take requests where I send information from a fragment to a List<EN_WishDetail> to store the selection of items and this is dumped to an adapter to display it in another fragment. my problem is that the parent layout loads a custom listview where I have 2 buttons to increase and decrease the quantities of the items this works correctly, likewise in the parent layout it has two textviews to count the number of items and the total value of the order , I want that when increasing the amount this also updates the textview of the parent layout from the adapter where the button functions are. How can I access the parent layout from the adapter and modify its value.

layout

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

listview personalized

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

Fragment

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

    }

}

adapter

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

I hope you can guide me the way to follow, thanks for your time.

CodePudding user response:

Yes, you can use event to achieve this just as ToolmakerSteve mentioned.

I made a simple demo to pass a int parameter to the main page, you can refer to the following code:

1.in your Adapter,define variable 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 method GetView of class WishAdapter , add code for button buttonMax:

  holder.buttonMax.Click  = delegate
   {
     // other code

      // invoke here
      MyEvent?.Invoke(this, 1);

    };

3.In WishFragment,do like this:

    adapter = new WishAdapter(Activity, List_Wish);
    WishlistView.Adapter = adapter;
    adapter.MyEvent  = Adapter_MyEvent; 

And method Adapter_MyEvent is:

    private void Adapter_MyEvent(object sender, int e)
    {   // here we can get the passed parameter e
        Debug.WriteLine("The passed parameter : "   e);
    }
  • Related