locked
why my listview display only the first inputted data? RRS feed

  • Question

  • User382724 posted

    CustomList.cs

    using Android.Views;
    using Android.Widget;
    using System.Collections.Generic;
    using Java.Lang;
    
    
    namespace MhylesOrderingApp
    {
        public class CustomList : BaseAdapter<SmsSent>
        {
            List<SmsSent> smslist;
            public CustomList(List<SmsSent> smslist)
            {
                this.smslist = smslist;
            }
            public override SmsSent this[int position]
            {
                get
                {
                    return smslist[position];
                }
            }
    
            public override int Count
            {
                get
                {
                    return smslist.Count;
                }
            }
    
            public override long GetItemId(int position)
            {
                return position;
            }
    
            public override View GetView(int position, View convertView, ViewGroup parent)
            {
                var view = convertView;
                if (view == null)
                {
                    view = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.sentSMSRow, parent, false);
                    var phoneNum = view.FindViewById<TextView>(Resource.Id.phoneNum);
                    var txtCustomer = view.FindViewById<TextView>(Resource.Id.txtCustomer);
                    var txtAddress = view.FindViewById<TextView>(Resource.Id.txtAddress);
                    var txtCustomerNo = view.FindViewById<TextView>(Resource.Id.txtCustomerNo);
                    var txtOrderedProduct = view.FindViewById<TextView>(Resource.Id.txtOrderedProduct);
                    var txtProductQty = view.FindViewById<TextView>(Resource.Id.txtProductQty);
                    var txtUnit = view.FindViewById<TextView>(Resource.Id.txtUnit);
                    var txtDate = view.FindViewById<TextView>(Resource.Id.txtDate);
                    var txtResult = view.FindViewById<TextView>(Resource.Id.txtResult);
                    view.Tag = new ViewHolder()
                    {
                        PhoneNo = phoneNum,
                        CustomerName = txtCustomer,
                        Address = txtAddress,
                        CustomerNo = txtCustomerNo,
                        Order = txtOrderedProduct,
                        Quantity = txtProductQty,
                        Unit = txtUnit,
                        sentDate = txtDate,
                        sentResult = txtResult
                    };  
                }
                var holder = (ViewHolder)view.Tag;
                holder.PhoneNo.Text = smslist[position].phoneNum;
                holder.CustomerName.Text = smslist[position].smsCustomer;
                holder.Address.Text = smslist[position].smsAddress;
                holder.CustomerNo.Text = smslist[position].smsCustNo;
                holder.Order.Text = smslist[position].smsOrderProd;
                holder.Quantity.Text = smslist[position].smsProdQty;
                holder.Unit.Text = smslist[position].smsUnit;
                holder.sentDate.Text = smslist[position].smsdate;
                holder.sentResult.Text = smslist[position].smsResult;
                return view;
            }
        }
    }
    

    MainActivity.cs

    using Android.App;
    using Android.OS;
    using Android.Support.V7.App;
    using Android.Runtime;
    using Android.Widget;
    using Android.Content.PM;
    using Android.Telephony;
    using System;
    using Android;
    using Android.Content;
    using Android.Support.V4.Content;
    using Android.Support.V4.App;
    using Android.Util;
    using Android.Support.Design.Widget;
    using Android.Views;
    using Android.Text;
    using System.Threading;
    using Android.Views.Animations;
    using System.Collections.Generic;
    using System.Collections;
    using Android.Graphics;
    using Android.Text.Style;
    using System.Linq;
    
    namespace MhylesOrderingApp
    {
        [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
        public class MainActivity : Android.Support.V7.App.AppCompatActivity, ActivityCompat.IOnRequestPermissionsResultCallback
        {      
            static readonly int REQUEST_SENDSMS = 0;
            private SmsManager _smsManager;
            private BroadcastReceiver _smsSentBroadcastReceiver, _smsDeliveredBroadcastReceiver;
            View layout;
            public static string myResult;
            static View hiddenLayout;
            static View scrollView;
            //static ListView listView;
            //static string myResult { get; set; }
            static ListView smsList;
            public static EditText phoneNumTxt, smsCustomerTxt, smsAddressTxt, smsCustNoTxt, smsOrderProdTxt, smsProdQtyTxt, smsUnitTxt, txtDate;
            public static string smsDate;
    #pragma warning disable CS0618 // Type or member is obsolete
            static Android.App.ProgressDialog progress;
    #pragma warning restore CS0618 // Type or member is obsolete
    
            protected override void OnCreate(Bundle savedInstanceState)
            {
                base.OnCreate(savedInstanceState);
    
                SetContentView(Resource.Layout.activity_main);
    
                SupportActionBar.Title = "Mhyles Ordering Messenger App";
    
                layout = FindViewById(Resource.Id.sample_main_layout);
                hiddenLayout = FindViewById(Resource.Id.hidden_layout);
                scrollView = FindViewById(Resource.Id.scrollViewing);
    
                smsList = FindViewById<ListView>(Resource.Id.lstView);
    
    
                var smsBtn = FindViewById<Button>(Resource.Id.btnSend);
                phoneNumTxt = FindViewById<EditText>(Resource.Id.phoneNum);
                smsDate = "";
                smsCustomerTxt = FindViewById<EditText>(Resource.Id.txtCustomer);
                smsAddressTxt = FindViewById<EditText>(Resource.Id.txtAddress);
                smsCustNoTxt = FindViewById<EditText>(Resource.Id.txtCustomerNo);
                smsOrderProdTxt = FindViewById<EditText>(Resource.Id.txtOrderedProduct);
                smsProdQtyTxt = FindViewById<EditText>(Resource.Id.txtProductQty);
                smsUnitTxt = FindViewById<EditText>(Resource.Id.txtUnit);
    
                var btnNewOders = FindViewById<Button>(Resource.Id.btnNewOrders);
    
                DateTime now = DateTime.Now.ToLocalTime();
                smsDate = now.ToString();
                smsBtn.SetBackgroundResource(Resource.Drawable.buttonBackground);
                btnNewOders.SetBackgroundResource(Resource.Drawable.buttonDesign);
                phoneNumTxt.SetBackgroundResource(Resource.Drawable.EditTextStyle);
                smsCustomerTxt.SetBackgroundResource(Resource.Drawable.EditTextStyle);
                smsAddressTxt.SetBackgroundResource(Resource.Drawable.EditTextStyle);
                smsCustNoTxt.SetBackgroundResource(Resource.Drawable.EditTextStyle);
                smsOrderProdTxt.SetBackgroundResource(Resource.Drawable.EditTextStyle);
                smsProdQtyTxt.SetBackgroundResource(Resource.Drawable.EditTextStyle);
                smsUnitTxt.SetBackgroundResource(Resource.Drawable.EditTextStyle);
    
                //smsList.Adapter = new CustomList(SentItems.SentItem);
    
    #pragma warning disable CS0618 // Type or member is obsolete
                progress = new Android.App.ProgressDialog(this);
    #pragma warning restore CS0618 // Type or member is obsolete
                progress.Indeterminate = true;
                progress.SetProgressStyle(Android.App.ProgressDialogStyle.Spinner);
                progress.SetMessage("Sending.. Please wait!");
                progress.SetCancelable(false);
                _smsManager = SmsManager.Default;
    
                btnNewOders.Click += (s, e) =>
                {
                    clearInput();
                    if (hiddenLayout.Visibility == ViewStates.Visible)
                    {
                        AlphaAnimation disappearAnimation = new AlphaAnimation(1, 0);
                        disappearAnimation.Duration = 2000;
                        hiddenLayout.StartAnimation(disappearAnimation);
                        hiddenLayout.Visibility = ViewStates.Invisible;
                        scrollView.SetBackgroundResource(0);
                        if(hiddenLayout.Visibility == ViewStates.Invisible)
                        {
    
                            Handler handler = new Handler();
                            Action action = () =>
                            {
                                hiddenLayout.Visibility = ViewStates.Gone;
                            };
                            handler.PostDelayed(action, 2000);
                        }
                    }
                    else
                    {
                        AlphaAnimation disappearAnimation = new AlphaAnimation(0, 1);
                        disappearAnimation.Duration = 2000;
                        hiddenLayout.StartAnimation(disappearAnimation);
                        hiddenLayout.Visibility = ViewStates.Visible;
                        scrollView.SetBackgroundResource(Resource.Drawable.TextViewStyle);
                    }
                };
                smsBtn.Click += (s, e) =>
                {
                    progress.Show(); 
                    if (TextUtils.IsEmpty(smsDate) || TextUtils.IsEmpty(phoneNumTxt.Text) || TextUtils.IsEmpty(smsCustomerTxt.Text)
                        || TextUtils.IsEmpty(smsAddressTxt.Text) || TextUtils.IsEmpty(smsCustNoTxt.Text) || TextUtils.IsEmpty(smsOrderProdTxt.Text)
                        || TextUtils.IsEmpty(smsProdQtyTxt.Text) || TextUtils.IsEmpty(smsUnitTxt.Text))
                    {
                        new Thread(new ThreadStart(delegate
                        {
                            RunOnUiThread(() => Toast.MakeText(this, "Please fill out all the fields", ToastLength.Long).Show());
                            RunOnUiThread(() => progress.Hide());
                        })).Start();                  
                        return;
                    }
                    else
                    {
                        var phone = phoneNumTxt.Text;
                        var message = smsDate + "|" + smsCustomerTxt.Text + "|" + smsAddressTxt.Text + "|" + smsCustNoTxt.Text
                                      + "|" + smsOrderProdTxt.Text + "|" + smsProdQtyTxt.Text + "|" + smsUnitTxt.Text;
                        var piSent = PendingIntent.GetBroadcast(this, 0, new Intent("SMS_SENT"), 0);
                        var piDelivered = PendingIntent.GetBroadcast(this, 0, new Intent("SMS_DELIVERED"), 0);
                        if ((int)Build.VERSION.SdkInt < 23)
                        {                        
                            _smsManager.SendTextMessage(phone, null, message, piSent, piDelivered);
                            new Thread(new ThreadStart(delegate
                            {
                                RunOnUiThread(() => Toast.MakeText(this, "Sending Orders error! please restart the app", ToastLength.Long).Show());
                                RunOnUiThread(() => progress.Hide());
                            })).Start();                     
                            return;
                        }
                        else
                        {
                            if (ActivityCompat.CheckSelfPermission(this, Manifest.Permission.SendSms) != (int)Permission.Granted)
                            {                          
                                RequestSendSMSPermission();            
                            }
                            else
                            {
                                _smsManager.SendTextMessage(phone, null, message, piSent, piDelivered);
                            }
                        }
                    }
                };           
            }
            public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
            {
                Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
                base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
            }
            public void RequestSendSMSPermission()
            {
                Log.Info("MainActivity", "Message permission has NOT been granted. Requesting permission.");
                if (ActivityCompat.ShouldShowRequestPermissionRationale(this, Manifest.Permission.SendSms))
                {
                    Log.Info("MainActivity", "Displaying message permission rationale to provide additional context.");
                    Snackbar.Make(layout, "Message permission is needed to send SMS.",
                        Snackbar.LengthIndefinite).SetAction("OK", new Action<View>(delegate (View obj) {
                            ActivityCompat.RequestPermissions(this, new String[] { Manifest.Permission.SendSms }, REQUEST_SENDSMS);
                        })).Show();
                }
                else
                {
                    ActivityCompat.RequestPermissions(this, new String[] { Manifest.Permission.SendSms }, REQUEST_SENDSMS);
                }
            }
            public static void clearInput()
            {
    
                phoneNumTxt.Text = "";
                smsCustomerTxt.Text = "";
                smsAddressTxt.Text = "";
                smsCustNoTxt.Text = "";
                smsOrderProdTxt.Text = "";
                smsProdQtyTxt.Text = "";
                smsUnitTxt.Text = "";
    
            }
            protected override void OnResume()
            {                      
                base.OnResume();
                _smsSentBroadcastReceiver = new SMSSentReceiver();
                _smsDeliveredBroadcastReceiver = new SMSDeliveredReceiver();
                RegisterReceiver(_smsSentBroadcastReceiver, new IntentFilter("SMS_SENT"));
                RegisterReceiver(_smsDeliveredBroadcastReceiver, new IntentFilter("SMS_DELIVERED"));         
            }
            protected override void OnPause()
            {
                base.OnPause();       
                UnregisterReceiver(_smsSentBroadcastReceiver);
                UnregisterReceiver(_smsDeliveredBroadcastReceiver);
            }
            //[BroadcastReceiver(Exported = true, Permission = "//receiver/@android:android.permission.SEND_SMS")]
            [BroadcastReceiver]
            public class SMSSentReceiver : BroadcastReceiver
            {
                public override void OnReceive(Context context, Intent intent)
                {
                    switch ((int)ResultCode)
                    {
                        case (int)Result.Ok:
                            Toast.MakeText(Application.Context, "SMS has been sent", ToastLength.Short).Show();
                            myResult = "Orders Sent";
                            progress.Hide();
                            break;
                        case (int)SmsResultError.GenericFailure:
                            Toast.MakeText(Application.Context, "Generic Failure", ToastLength.Short).Show();
                            myResult = "Orders Send Faild";
                            progress.Hide();
                            break;
                        case (int)SmsResultError.NoService:
                            Toast.MakeText(Application.Context, "No Service", ToastLength.Short).Show();
                            myResult = "Orders Send Faild";
                            progress.Hide();
                            break;
                        case (int)SmsResultError.NullPdu:
                            Toast.MakeText(Application.Context, "Null PDU", ToastLength.Short).Show();
                            myResult = "Orders Send Faild";
                            progress.Hide();
                            break;
                        case (int)SmsResultError.RadioOff:
                            Toast.MakeText(Application.Context, "Radio Off", ToastLength.Short).Show();
                            myResult = "Orders Send Faild";
                            progress.Hide();
                            break;
                        default:
                            break;
                    }
                    if (hiddenLayout.Visibility == ViewStates.Visible)
                    {
                        AlphaAnimation disappearAnimation = new AlphaAnimation(1, 0);
                        disappearAnimation.Duration = 2000;
                        hiddenLayout.StartAnimation(disappearAnimation);
                        hiddenLayout.Visibility = ViewStates.Invisible;
                        scrollView.SetBackgroundResource(0);
                        if (hiddenLayout.Visibility == ViewStates.Invisible)
                        {
    
                            Handler handler = new Handler();
                            Action action = () =>
                            {
                                hiddenLayout.Visibility = ViewStates.Gone;
                            };
                            handler.PostDelayed(action, 2000);
                        }
                    }
                    else
                    {
                        AlphaAnimation disappearAnimation = new AlphaAnimation(0, 1);
                        disappearAnimation.Duration = 2000;
                        hiddenLayout.StartAnimation(disappearAnimation);
                        hiddenLayout.Visibility = ViewStates.Visible;
                        scrollView.SetBackgroundResource(Resource.Drawable.TextViewStyle);
                    }
    
                    smsList.Adapter = new CustomList(SentItems.SentItem);
    
    
    
                }
    
    
            }
            //[BroadcastReceiver(Exported = true, Permission = "//receiver/@android:android.permission.SEND_SMS")]
            [BroadcastReceiver]
            public class SMSDeliveredReceiver : BroadcastReceiver
            {
                public override void OnReceive(Context context, Intent intent)
                {
                    switch ((int)ResultCode)
                    {
                        case (int)Result.Ok:
                            Toast.MakeText(Application.Context, "SMS Delivered", ToastLength.Short).Show();
                            progress.Hide();
                            myResult = "Orders Sent";
                            break;
                        case (int)Result.Canceled:
                            Toast.MakeText(Application.Context, "SMS not delivered", ToastLength.Short).Show();
                            progress.Hide();
                            myResult = "Orders Send Faild";
                            break;
                    }
                }
            }
        }
    }
    
    Saturday, March 9, 2019 8:14 AM

Answers

  • User371688 posted

    In fact , you don't need the class SentItems

     public class SentItems : MainActivity
    

    You can try the following code:

          private static List<SmsSent> smsData = new List<SmsSent>();
    
    
            private static void  refreshData() {
            SmsSent sms2 = new SmsSent()
            {
                phoneNum = phoneNumTxt.Text.ToString(),
                smsCustomer = smsCustomerTxt.Text.ToString(),
                smsAddress = smsAddressTxt.Text.ToString(),
                smsCustNo = smsCustNoTxt.Text.ToString(),
                smsOrderProd = smsOrderProdTxt.Text.ToString(),
                smsProdQty = smsProdQtyTxt.Text.ToString(),
                smsUnit = smsUnitTxt.Text.ToString(),
                smsdate = smsDate,
                smsResult = myResult,
            };
            smsData.OrderBy(i => i.smsdate).ToList();
            smsData.Add(sms2);
    
            smsList.Adapter = new CustomList(smsData);
        }
    

    you can call method refreshData like this:

      refreshData();
    

    instead of

    smsList.Adapter = new CustomList(SentItems.SentItem);
    

    The effect is as follows

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, March 12, 2019 3:36 AM

All replies

  • User382724 posted

    SentItems.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    using Android.App;
    using Android.Content;
    using Android.OS;
    using Android.Runtime;
    using Android.Views;
    using Android.Widget;
    
    namespace MhylesOrderingApp
    {
        public class SentItems:MainActivity
        {
            public static List<SmsSent> SentItem
            {
                get; private set;
            }
            static SentItems()
            {
                var item = new List<SmsSent>();
                AddItem(item);
    
                SentItem = item.OrderBy(i => i.smsdate).ToList();
            }
    
            private static void AddItem(List<SmsSent> item)
            {
    
                item.Add(new SmsSent()
                {
                    phoneNum = phoneNumTxt.Text.ToString(),
                    smsCustomer = smsCustomerTxt.Text.ToString(),
                    smsAddress = smsAddressTxt.Text.ToString(),
                    smsCustNo = smsCustNoTxt.Text.ToString(),
                    smsOrderProd = smsOrderProdTxt.Text.ToString(),
                    smsProdQty = smsProdQtyTxt.Text.ToString(),
                    smsUnit = smsUnitTxt.Text.ToString(),
                    smsResult = myResult.ToString(),
                    smsdate = smsDate.ToString()
    
    
                });
    
    
            }
    
        }
    }
    

    SmsSent.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    using Android.App;
    using Android.Content;
    using Android.OS;
    using Android.Runtime;
    using Android.Views;
    using Android.Widget;
    
    namespace MhylesOrderingApp
    {
        public class ViewHolder : Java.Lang.Object
        {
            public TextView PhoneNo { get; set; } 
            public TextView CustomerName { get; set; }
            public TextView Address { get; set; }
            public TextView CustomerNo { get; set; }
            public TextView Order { get; set; }
            public TextView Quantity { get; set; }
            public TextView Unit { get; set; }
            public TextView sentDate { get; set; }
            public TextView sentResult { get; set; }
        }
    }
    

    ViewHolder.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    using Android.App;
    using Android.Content;
    using Android.OS;
    using Android.Runtime;
    using Android.Views;
    using Android.Widget;
    
    namespace MhylesOrderingApp
    {
        public class ViewHolder : Java.Lang.Object
        {
            public TextView PhoneNo { get; set; } 
            public TextView CustomerName { get; set; }
            public TextView Address { get; set; }
            public TextView CustomerNo { get; set; }
            public TextView Order { get; set; }
            public TextView Quantity { get; set; }
            public TextView Unit { get; set; }
            public TextView sentDate { get; set; }
            public TextView sentResult { get; set; }
        }
    }
    
    Saturday, March 9, 2019 8:16 AM
  • User382724 posted

    activity_main.axml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:id="@+id/sample_main_layout">
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/btnNewOrders"
            android:textColor="#249e3b"
            android:text="Send New Orders"/>
        <ScrollView 
               android:layout_width="fill_parent"
               android:layout_height="wrap_content"
               android:layout_margin="2dp"
               android:padding="2dp"
               android:id="@+id/scrollViewing">
            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:id="@+id/hidden_layout"
                android:visibility="gone"
                android:orientation="vertical">
                <android.support.design.widget.TextInputLayout
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/txtInputLayoutRecipNum">
                    <EditText
                        android:id="@+id/phoneNum"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:inputType="phone"
                        style="@style/edit_margin"
                        android:hint="Recipient Number"/>
                </android.support.design.widget.TextInputLayout>
                <android.support.design.widget.TextInputLayout
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/txtInputLayoutCustNam">
                    <EditText
                        android:id="@+id/txtCustomer"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        style="@style/edit_margin"
                        android:hint="Customer Name"/>
                </android.support.design.widget.TextInputLayout>
                <android.support.design.widget.TextInputLayout
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/txtInputLayoutCustAdd">
                    <EditText
                        android:id="@+id/txtAddress"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        style="@style/edit_margin"
                        android:lines="5"
                        android:maxLines="10"
                        android:gravity="left|top"
                        android:inputType="text|textMultiLine"
                        android:hint="Customer Address"/>
                </android.support.design.widget.TextInputLayout>
                <android.support.design.widget.TextInputLayout
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/txtInputLayoutCustNo">
                    <EditText
                        android:id="@+id/txtCustomerNo"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:inputType="phone"
                        style="@style/edit_margin"
                        android:hint="Customer No."/>
                </android.support.design.widget.TextInputLayout>
                <android.support.design.widget.TextInputLayout
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/txtInputLayoutOrdProd">
                    <EditText
                        android:id="@+id/txtOrderedProduct"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        style="@style/edit_margin"
                        android:hint="Ordered Product Name"/>
                </android.support.design.widget.TextInputLayout>
                <android.support.design.widget.TextInputLayout
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/txtInputLayoutQty">
                    <EditText
                        android:id="@+id/txtProductQty"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        style="@style/edit_margin"
                        android:hint="Quantity"/>
                </android.support.design.widget.TextInputLayout>
                <android.support.design.widget.TextInputLayout
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/txtInputLayoutUnit">
                    <EditText
                        android:id="@+id/txtUnit"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        style="@style/edit_margin"
                        android:hint="Unit"/>
                </android.support.design.widget.TextInputLayout>
                <Button
                    android:id="@+id/btnSend"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textColor="#d9dae0"
                    android:text="Send Orders" />
            </LinearLayout>
        </ScrollView>
    
            <ListView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="2dp"
                android:gravity="center"
                android:id="@+id/lstView">
            </ListView>
    
    </LinearLayout>
    

    sentSMSRow.axml

    <?xml version="1.0" encoding="utf-8"?>  
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
        android:orientation="vertical"  
        android:layout_width="fill_parent"  
        android:layout_height="fill_parent"
        android:gravity="center">  
        <TextView  
            android:id="@+id/phoneNum"  
            android:text="PhoneNo"
            android:textAppearance="?android:attr/textAppearanceLarge"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:paddingLeft="5dp" />  
        <TextView  
            android:id="@+id/txtCustomer"
            android:text="CustomerName"
            android:textAppearance="?android:attr/textAppearanceSmall"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:paddingLeft="5dp" />  
        <TextView  
            android:id="@+id/txtAddress"  
            android:text="Address"
            android:textAppearance="?android:attr/textAppearanceSmall"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:paddingLeft="5dp" />  
        <TextView  
            android:id="@+id/txtCustomerNo"  
            android:text="CustomerNo"
            android:textAppearance="?android:attr/textAppearanceSmall"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:paddingLeft="5dp" />  
        <TextView  
            android:id="@+id/txtOrderedProduct"
            android:text="Order"
            android:textAppearance="?android:attr/textAppearanceSmall"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:paddingLeft="5dp" /> 
        <TextView  
            android:id="@+id/txtProductQty" 
            android:text="Quantity"
            android:textAppearance="?android:attr/textAppearanceSmall"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:paddingLeft="5dp" />  
        <TextView  
            android:id="@+id/txtUnit"  
            android:text="Unit"
            android:textAppearance="?android:attr/textAppearanceSmall"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:paddingLeft="5dp" /> 
        <TextView  
            android:id="@+id/txtResult"  
            android:text="Result"
            android:textAppearance="?android:attr/textAppearanceSmall"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:paddingLeft="5dp" />  
        <TextView  
            android:id="@+id/txtDate"  
            android:text="Date"
            android:textAppearance="?android:attr/textAppearanceSmall"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:paddingLeft="5dp" />  
    </LinearLayout>   
    
    Saturday, March 9, 2019 8:18 AM
  • User382724 posted

    I want my listview like this

    I done doing this in built-in listview but the problem is I can't customize the styles, that is why I create a custom listview, and I faced this kind of issue, when I put a hardcode listview values and put this smsList.Adapter = new CustomList(SentItems.SentItem); inside OnCreate(Bundle savedInstanceState) method, all my hardcoded listview values will display but, when I put this smsList.Adapter = new CustomList(SentItems.SentItem); inside public override void OnReceive() method. it will return an error like this ~~Android.Views.WindowManagerBadTokenException: on custom listview~~. But when I removed the hardcoded listview values and used this item.Add(new SmsSent() { phoneNum = phoneNumTxt.Text.ToString(), smsCustomer = smsCustomerTxt.Text.ToString(), smsAddress = smsAddressTxt.Text.ToString(), smsCustNo = smsCustNoTxt.Text.ToString(), smsOrderProd = smsOrderProdTxt.Text.ToString(), smsProdQty = smsProdQtyTxt.Text.ToString(), smsUnit = smsUnitTxt.Text.ToString(), smsResult = myResult.ToString(), smsdate = smsDate.ToString()

                });
    

    and put this smsList.Adapter = new CustomList(SentItems.SentItem); inside public override void OnReceive() method, the error will gone but it will display only the first inputted data. And when I put this smsList.Adapter = new CustomList(SentItems.SentItem); inside OnCreate(Bundle savedInstanceState) method it will return an error like this this ~~Android.Views.WindowManagerBadTokenException: on custom listview~~

    Saturday, March 9, 2019 8:22 AM
  • User371688 posted

    In fact , you don't need the class SentItems

     public class SentItems : MainActivity
    

    You can try the following code:

          private static List<SmsSent> smsData = new List<SmsSent>();
    
    
            private static void  refreshData() {
            SmsSent sms2 = new SmsSent()
            {
                phoneNum = phoneNumTxt.Text.ToString(),
                smsCustomer = smsCustomerTxt.Text.ToString(),
                smsAddress = smsAddressTxt.Text.ToString(),
                smsCustNo = smsCustNoTxt.Text.ToString(),
                smsOrderProd = smsOrderProdTxt.Text.ToString(),
                smsProdQty = smsProdQtyTxt.Text.ToString(),
                smsUnit = smsUnitTxt.Text.ToString(),
                smsdate = smsDate,
                smsResult = myResult,
            };
            smsData.OrderBy(i => i.smsdate).ToList();
            smsData.Add(sms2);
    
            smsList.Adapter = new CustomList(smsData);
        }
    

    you can call method refreshData like this:

      refreshData();
    

    instead of

    smsList.Adapter = new CustomList(SentItems.SentItem);
    

    The effect is as follows

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, March 12, 2019 3:36 AM
  • User382724 posted

    Thanks a lot Sir @jezh :smiley: now it works fine...

    Tuesday, March 12, 2019 10:14 AM