0

I have a custom listview as following code:

listView.Adapter = new HomeScreenAdapter(this, tableItems);

How do i make the screen to show the last item on the listview and not show the start of the list everytime an item is added to the custom listview?

HomeScreenAdapter.cs

namespacetest {
public class HomeScreenAdapter : BaseAdapter<Model.TableItem> {
    List<Model.TableItem> items;
    Activity context;
    public HomeScreenAdapter(Activity context, List<Model.TableItem> items)
        : base()
    {
        this.context = context;
        this.items = items;
    }
    public override long GetItemId(int position)
    {
        return position;
    }
    public override Model.TableItem this[int position]
    {
        get { return items[position]; }
    }
    public override int Count
    {
        get { return items.Count; }
    }
    public override View GetView(int position, View convertView, ViewGroup parent)
    {
        var item = items[position];

        View view = convertView;
        if (view == null) // no view to re-use, create new
            view = context.LayoutInflater.Inflate(Resource.Layout.content_main, null);
        view.FindViewById<TextView>(Resource.Id.Text1).Text = item.Heading;
            int expiryrangeint = Convert.ToInt32(item.ExpiryRange);
            if (expiryrangeint < 30)
                view.FindViewById<TextView>(Resource.Id.Text1).SetBackgroundColor(Android.Graphics.Color.Red);
            else if(expiryrangeint > 30 && expiryrangeint < 90)
                view.FindViewById<TextView>(Resource.Id.Text1).SetBackgroundColor(Android.Graphics.Color.DarkGray);
            else if (expiryrangeint > 90 && expiryrangeint < 180)
                view.FindViewById<TextView>(Resource.Id.Text1).SetBackgroundColor(Android.Graphics.Color.Yellow);
            else
                view.FindViewById<TextView>(Resource.Id.Text1).SetBackgroundColor(Android.Graphics.Color.White);
            //textview1.SetBackgroundColor(Android.Graphics.Color.Red);

            view.FindViewById<TextView>(Resource.Id.Text2).Text = item.SubHeading;
        view.FindViewById<TextView>(Resource.Id.Text3).Text=item.SubSubHeading;

        return view;
    }
}
}
Thomas Koh
  • 213
  • 5
  • 17

1 Answers1

0

Sequencing done on your existing app needs to be rewritten , if database is changed.

Also, you need to update the app.

Reference : Android custom listview display last item on first position

EDIT

For auto scroll case that you mentioned in comment :

 yourListView.setSelection(yourListAdapter.getCount() - 1);

Reference : Listview Scroll to the end of the list after updating the list

Raj
  • 707
  • 6
  • 23
  • i mean it will auto scroll to the last item and not from the first item,every time when a new item is added,the screen will show the first item on top of the listview and not go straight to the bottom to show the last item added.When the list get larger and larger,i have to manually scroll down to the bottom to see if the new item has been added. – Thomas Koh Aug 29 '18 at 09:39