Forum Xamarin.Android
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

How to Refresh List View?

Xammy10Xammy10 Member ✭✭✭

I want to update the items on the list view whenever i click the item, I already did the update part but it's not changing instantly (Realtime).
I need to kill the whole application for it to update, BTW, I'm using http request to get and post my items.

Thanks in advance :)

Answers

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭
    I think you should post some code
  • Xammy10Xammy10 Member ✭✭✭
    edited June 2018

    public class AdapterLop : BaseAdapter
    {
    private List mItem = new List();
    private Context context;

        public AdapterLop(Context context, List<Model> mItems)
        {
            mItem = mItems;
            mItem.AddRange(mItems);
            this.context = context;
            this.NotifyDataSetChanged();
        }
    
        public override int Count
        {
            get { return mItem.Count; }
        }
    
        public override long GetItemId(int position)
        {
            return position;
        }
    
        public override Model this[int position]
        {
            get
            {
                return mItem[position];
            }
        }
    
        public override View GetView(int position, View convertView, ViewGroup parent)
        {
            View listItem = convertView;
            ListView lv = MainActivity.list_item;
            listItem = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.list_layout, parent, false);
            TextView txt1 = listItem.FindViewById<TextView>(Resource.Id.api1);
            TextView txt2 = listItem.FindViewById<TextView>(Resource.Id.api2);
            TextView txt3 = listItem.FindViewById<TextView>(Resource.Id.api3);
            TextView txt4 = listItem.FindViewById<TextView>(Resource.Id.api4);
            TextView txt5 = listItem.FindViewById<TextView>(Resource.Id.api5);
    
            txt1.Text = mItem[position].apiSched;
            txt2.Text = mItem[position].apiTimeFrom;
            txt3.Text = mItem[position].apiTimeTo;
            txt4.Text = mItem[position].apiCap.ToString();
            txt5.Text = mItem[position].apiDate;
    
            listItem.Click += async delegate
            {
                var items = new Model
                {
                    apiSched= mItem[position].apiSched,
                    apiTimeFrom= mItem[position].apiTimeFrom,
                    apiTimeTo= mItem[position].apitTimeTo, 
                    apiCap= mItem[position].apiCap - 1,
                    apiDate= mItem[position].apiDate
                };
                var client = new HttpClient();
                var data = JsonConvert.SerializeObject(items);
                var content = new StringContent(data, Encoding.UTF8, "application/json");
                var response = await client.PostAsync("http://192.168.1.69:4321/api/api/api/put"+ mItem[position].apiID, content);
                Toast.MakeText(parent.Context, response.StatusCode.ToString(), ToastLength.Short).Show();
            };
            return listItem;
        }
    }
    

    Sorry, my event is inside of my adapter, don't mind it

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    you receive a response but you don't refresh your adapter...

  • Xammy10Xammy10 Member ✭✭✭

    yeah, the changes will only show if I kill the application and start it again

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    I don't know what is "response" but I think it returns a list of "Model"

    You should do something like

    var response = await client.PostAsync("http://192.168.1.69:4321/api/api/api/put"+ mItem[position].apiID, content)
    mItem = response;
    this.NotifyDataSetChanged();
    
  • Xammy10Xammy10 Member ✭✭✭
    edited June 2018

    @AlessandroCaliaro said:
    I don't know what is "response" but I think it returns a list of "Model"

    You should do something like

    var response = await client.PostAsync("http://192.168.1.69:4321/api/api/api/put"+ mItem[position].apiID, content)
    mItem = response;
    this.NotifyDataSetChanged();

    Idk why but I already try the "NotifyDataSetChanged()" many times but that code is not working on me.

    BTW, the response is the data I get from my api and I converted it to string

  • ChristopheBERNARDChristopheBERNARD BEMember ✭✭✭✭✭

    hello all,
    @enohara try perhpas to add on the custom adapter (AdapterLop ) => interface => view.ionclicklistener
    inherid from it

    modify the call on the listItem.Click += async delegate
    by
    listItem.setOnClickListener(this);

    it's an idea, i've notice on an app i've worked sometimes the event isn't generate ...and i can click on button but not generate ...
    try and tell us ...

  • Xammy10Xammy10 Member ✭✭✭

    @ChristopheBERNARD said:
    hello all,
    @enohara try perhpas to add on the custom adapter (AdapterLop ) => interface => view.ionclicklistener
    inherid from it

    modify the call on the listItem.Click += async delegate
    by
    listItem.setOnClickListener(this);

    it's an idea, i've notice on an app i've worked sometimes the event isn't generate ...and i can click on button but not generate ...
    try and tell us ...

    I don't get it, BTW, I already separate my adapter and my click even.

Sign In or Register to comment.