help with refreshing listview after update

asusmaniasusmani Member ✭✭
edited June 23 in Xamarin.Android

Hi everyone.
I'm just about done with my app, but one last thing I cannot figure out is how to refresh my listview after an update is made. I would like to just get this done so I can wrap up my project!

So, I followed the Xamarin Android example on how to implement a Listview with a custom adapter and I have the data coming from a web service I have hosted on my server. I have a delete button at the row level of my listview. When I click it, the delete happens on the database, but I just cannot get me listview to reflect that deletion and update unless the user navigates off the screen and comes back to it....

Here is my code. If anyone can tell me where I am missing anything, it would be nice if you can help point that out.....

Here is my layout for the listview row template:

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#FFFFFF"
    android:padding="8dp">
    <LinearLayout
        android:id="@+id/IDDisclosuresPaint"
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="10dip">
        <TextView
            android:id="@+id/txtPaintID"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#FF7F3300"
            android:textSize="20dip"
            android:textStyle="italic"/>
        <TextView
            android:id="@+id/txtPaintLocation"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#FF7F3300"
            android:textSize="20dip"
            android:textStyle="italic" />
        <TextView
            android:id="@+id/txtPaintCondition"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#FF7F3300"
            android:textSize="20dip"
            android:textStyle="italic" />

        <Button
                android:text="Delete"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/cmdDiscPaintDelete"
                android:textStyle="bold" />
    </LinearLayout>
    <ImageView
        android:id="@+id/imgDiscPaint"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:padding="5dp"
        android:src="@drawable/icon"
        android:layout_alignParentRight="true" />
</RelativeLayout>

Below is my layout for the page that displays my layout:

<?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">
    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@android:id/list"
        android:layout_weight="1" />
    <LinearLayout
        android:id="@+id/linearLayoutPaint"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal">
        <Button
            android:text="Add Paint"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/cmdDisclosuresAddPaint"
            android:textStyle="bold"
            android:gravity="left"
            android:layout_marginBottom="10dp"
            android:layout_marginTop="10dp" />
        <Button
            android:text="Delete All"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/cmdDisclosuresDelAllPaint"
            android:textStyle="bold"
            android:gravity="right"
            android:layout_marginBottom="10dp"
            android:layout_marginTop="10dp" />
    </LinearLayout>

    <Button
        android:text="Save and Continue"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/cmdDisclosuresPaintNext"
        android:textStyle="bold"
        android:layout_marginLeft="50dp"
        android:layout_marginRight="50dp"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="10dp" />
</LinearLayout>

And here is my C# code for the listview screen:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using UpstreamInspections_Android.com.upstreaminspections;


namespace UpstreamInspections_Android
{
    [Activity(Label = "Paint History")]
    public class AdminDisclosuresPaint : ListActivity
    {
        UpstreamInspectionsWebServices AppSvc = new UpstreamInspectionsWebServices();
        public com.upstreaminspections.InspectionPaint[] PaintHistoryList;
        Common CM = new Common();

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

        }
        protected override void OnStart()
        {
            base.OnStart();
            initializeView();
        }
        public void initializeView()
        {
            try
            {
                //SetContentView(Resource.Layout.AdminDisclosures);
                PaintHistoryList = AppSvc.getInspectionPaint(Common.InspectionID);

                // Set our view from the layout resource
                SetContentView(Resource.Layout.AdminDisclosuresPaint);
                ListAdapter = new AdminDisclosuresPaintDataModel(this, PaintHistoryList);

                //initialize buttons
                Button BackButton = FindViewById<Button>(Resource.Id.cmdDisclosuresAddPaint);
                BackButton.Click += BackButton_Click;

                Button SaveButton = FindViewById<Button>(Resource.Id.cmdDisclosuresPaintNext);
                SaveButton.Click += SaveButton_Click;

                Button DelAllButton = FindViewById<Button>(Resource.Id.cmdDisclosuresDelAllPaint);
                DelAllButton.Click += DelAllButton_Click;
            }

            catch (Exception ex)
            {
                CM.AlertDialog(this, "Error", "Unable to load Paint list. Please contact IT support." + ex.Message, "OK");
            }
    }
        protected override void OnListItemClick(ListView l, View v, int position, long id)
        {
            var t = PaintHistoryList[position];
            //Android.Widget.Toast.MakeText(this, t.Inspection_ID, Android.Widget.ToastLength.Short).Show();
            this.NotifyAll();

            //StartActivity(typeof(AdminInspection));
        }
        private void BackButton_Click(object sender, EventArgs e)
        {
            StartActivity(typeof(AdminAddPaint));
        }
        private void SaveButton_Click(object sender, EventArgs e)
        {
            StartActivity(typeof(AdminDisclosuresExterior));
        }
        private void DelAllButton_Click(object sender, EventArgs e)
        {
            Android.App.AlertDialog.Builder dialog = new AlertDialog.Builder(this);
            AlertDialog alert = dialog.Create();
            alert.SetTitle("Upstream Inspections");
            alert.SetMessage("Are you sure you want to delete all uploaded Paint info?");
            //alert.SetIcon(Resource.Drawable.alert);
            alert.SetButton("OK", async (c, ev) =>
            {
                //ProgressBar Progress = FindViewById<ProgressBar>(Resource.Id.pbPaintListScreen);
                //Progress.Visibility = ViewStates.Visible;
                //Progress.Indeterminate = true;
                Task<string> SubmitTask = new Task<string>(DeleteAllPaint);
                SubmitTask.Start();
                //Progress.Visibility = ViewStates.Visible;

                string Result = await SubmitTask;

                if (Result == "0")
                {
                    CM.AlertDialog(this, "Upstream Inspections", "All paint history deleted successfully!", "OK");
                    //Progress.Visibility = ViewStates.Invisible;
                }
                else
                {
                    CM.AlertDialog(this, "Error", "Error updating the inspection. Please contact IT Support for help.", "OK");
                    //Progress.Visibility = ViewStates.Invisible;

                }
            });
            alert.SetButton2("CANCEL", (c, ev) =>
            {

            });
            alert.Show();
        }
        private string DeleteAllPaint()
        {
            String Result = AppSvc.UpdateInspectionDeleteAllPaint(Common.InspectionID, Common.UserID);
            return Result;
        }
    }
}

Here is my code for the custom adapter:

using UpstreamInspections_Android.com.upstreaminspections;
using Android.App;
using Android.Views;
using Android.Widget;
using Square.Picasso;
using System;

namespace UpstreamInspections_Android
{
    public class AdminDisclosuresPaintDataModel : BaseAdapter<InspectionPaint>
    {
        UpstreamInspections_Android.com.upstreaminspections.InspectionPaint[] PaintItems;
        Activity context;
        UpstreamInspectionsWebServices AppSvc = new UpstreamInspectionsWebServices();
        Common CM = new Common();
        public AdminDisclosuresPaintDataModel(Activity context, InspectionPaint[] PaintItems) : base()
        {
            this.context = context;
            this.PaintItems = PaintItems;
        }


        public override InspectionPaint this[int position]
        {
            get { return PaintItems[position]; }
        }

        public override long GetItemId(int position)
        {
            return position;
        }

        public override View GetView(int position, View convertView, ViewGroup parent)
        {

            View view = convertView; // re-use an existing view, if one is available
            if (view == null) // otherwise create a new one
                view = context.LayoutInflater.Inflate(Resource.Layout.AdminDisclosuresPaintCustom, null);
            view.FindViewById<TextView>(Resource.Id.txtPaintID).Text = PaintItems[position].ID;
            view.FindViewById<TextView>(Resource.Id.txtPaintLocation).Text = "Location: " + PaintItems[position].Description;
            view.FindViewById<TextView>(Resource.Id.txtPaintCondition).Text = "Condition: " + PaintItems[position].Condition;
            view.FindViewById<Button>(Resource.Id.cmdDiscPaintDelete).Click += (sender, e) =>            
            {
                int x = position;
                String DeleteResult = AppSvc.DeletePaint(PaintItems[position].ID);

                if (DeleteResult == "0")
                {
                    CM.AlertDialog(context, "Upstream Inspections", "Paint entries successfully deleted!", "OK");
                    this.NotifyDataSetChanged();
                }
                else
                {
                    CM.AlertDialog(context, "Upstream Inspections", "There was an error deleting paint history. Please contact IT Support for details.", "OK");
                }
            };

            //Picasso.With(context).Load("http://i.imgur.com/DvpvklR.png").Into(view.FindViewById<ImageView>(Resource.Id.Image));
            if (PaintItems[position].ImageURL == "" || PaintItems[position].ImageURL is null)
            {
                //Picasso.With(context).Load("http://i.imgur.com/DvpvklR.png").Into(view.FindViewById<ImageView>(Resource.Id.imgDiscPaint));
                view.FindViewById<ImageView>(Resource.Id.imgDiscPaint).SetImageResource(Resource.Drawable.icon);
            }
            else
            {
                Picasso.With(context).Load(PaintItems[position].ImageURL).Into(view.FindViewById<ImageView>(Resource.Id.imgDiscPaint));
                //Picasso.With(context).Load("https://upstreaminspections.com/UploadedImages/INSPECTION_ID_28451/20190612_131654.jpg").Into(view.FindViewById<ImageView>(Resource.Id.imgDiscPaint));
                //view.FindViewById<ImageView>(Resource.Id.imgDiscPaint).SetImageDrawable(@"https://upstreaminspections.com/UploadedImages/INSPECTION_ID_28451/20190612_131654.jpg");
            }     
            return view;
        }

        //Fill in cound here, currently 0
        public override int Count
        {
            get
            {
                return PaintItems.Length;
            }
        }

    }
    class AdminDisclosuresPaintDataModelViewHolder : Java.Lang.Object
    {
        //Your adapter views to re-use
        //public TextView Title { get; set; }
    }

}

Best Answer

Answers

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭
    I think it’s impossible to take a look all this code.

    Create a small project, post it on GitHub and post the link so someone could debug your small project and help you
  • asusmaniasusmani Member ✭✭

    Wow. Thanks a bunch LeonLu!! I really appreciate the help!
    That did it for me.

    :smile:

Sign In or Register to comment.