Why my expandable listview display a wrong list..

JunCainJunCain ✭✭✭Member ✭✭✭
edited June 11 in Xamarin.Android

I make an expandable listview with a tablelayout, in the first click it will display a right list but in the second click and so on it will display a wrong list..

ExpandableSendOrderAdapter.cs

 public class ExpandableSendOrderAdapter : BaseExpandableListAdapter
    {
        private Activity activity;
        private List<string> listHeader;
        private Dictionary<string, List<SendOrdersModel>> listChild;
        //List<SendOrdersModel> sendOrders;
        public ExpandableSendOrderAdapter(Activity _activity, List<string> _listHeader, Dictionary<string, List<SendOrdersModel>> _listChild)
        {
            activity = _activity;
            listHeader = _listHeader;
            listChild = _listChild;
        }

        public override Java.Lang.Object GetChild(int groupPosition, int childPosition)
        {
            return listChild[listHeader[groupPosition]][childPosition].ToString();
        }

        public override long GetChildId(int groupPosition, int childPosition)
        {
            return childPosition;
        }

        public override View GetChildView(int groupPosition, int childPosition, bool isLastChild, View convertView, ViewGroup parent)
        {
            string childText = (string)GetChild(groupPosition, childPosition);
            if (convertView == null)
            {
                convertView = activity.LayoutInflater.Inflate(Resource.Layout.SendOrderTextView, null);
            }
            string[] childDate = childText.Split("_");
            TextView orderid = (TextView)convertView.FindViewById(Resource.Id.orderId);
            orderid.Text = "Order ID: " + childDate[0];

            TextView agentid = (TextView)convertView.FindViewById(Resource.Id.agent);
            agentid.Text = "Agent Name: " + childDate[1];

            TextView custid = (TextView)convertView.FindViewById(Resource.Id.cust);
            custid.Text = "Customer Name: " + childDate[2];

            TableLayout orders = (TableLayout)convertView.FindViewById(Resource.Id.Orders);
            //string str = childDate[3].Trim();

            DisplayMetrics metrics = activity.Resources.DisplayMetrics;
            int h = (int)TypedValue.ApplyDimension(ComplexUnitType.Dip, 30, metrics);
            int p = (int)TypedValue.ApplyDimension(ComplexUnitType.Dip, 5, metrics);
            int _p = (int)TypedValue.ApplyDimension(ComplexUnitType.Dip, 1, metrics);
            int padd = (int)TypedValue.ApplyDimension(ComplexUnitType.Dip, 10, metrics);
            string[] strArray = childDate[3].Split(";");
            int strCount = strArray.Count();
            int count = orders.ChildCount - 1;
            foreach (string product in strArray)
            {
                string[]  _product = product.Split("|");
                if (count < strCount)
                {
                    TableRow tr = new TableRow(activity);
                    tr.SetBackgroundResource(Resource.Drawable.tble_row_bg);
                    tr.SetPadding(p, _p, _p, _p);
                    tr.LayoutParameters = new TableLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, h);
                    tr.LayoutParameters = new TableRow.LayoutParams(ViewGroup.LayoutParams.MatchParent, h);
                    foreach (string _prodArr in _product)
                    {

                        TextView td = new TextView(activity);
                        td.LayoutParameters = new TableRow.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent);
                        td.SetBackgroundResource(Resource.Drawable.tble_cell_bg);
                        td.SetTextSize(ComplexUnitType.Dip, 14);
                        td.SetPadding(padd, p, padd, p);
                        td.Text = _prodArr.ToString();
                        tr.AddView(td);

                    }
                    orders.AddView(tr);
                }

            }

            TextView status = (TextView)convertView.FindViewById(Resource.Id.status);
            if(childDate[4] == "0")
            {
                status.Text = "Status: Unconfirmed";
            }
            else
            {
                status.Text = "Status: Confirmed";
            }

            TextView orderdate = (TextView)convertView.FindViewById(Resource.Id.orderDate);
            orderdate.Text = "Ordered Date: " + childDate[5];

            TextView senddate = (TextView)convertView.FindViewById(Resource.Id.senddate);
            senddate.Text = "Sent: " + childDate[6];
            return convertView;
        }

        public override int GetChildrenCount(int groupPosition)
        {
            return listChild[listHeader[groupPosition]].Count;
        }

        public override Java.Lang.Object GetGroup(int groupPosition)
        {
            return listHeader[groupPosition];
        }

        public override int GroupCount
        {
            get { return listHeader.Count; }
        }

        public override long GetGroupId(int groupPosition)
        {
            return groupPosition;
        }

        public override View GetGroupView(int groupPosition, bool isExpanded, View convertView, ViewGroup parent)
        {
            string headTitle = (string)GetGroup(groupPosition);

            convertView = convertView ?? activity.LayoutInflater.Inflate(Resource.Layout.SendOrderHeader, null);
            var headerLabel = (TextView)convertView.FindViewById(Resource.Id.SendOrderHead);
            headerLabel.Text = headTitle;

            return convertView;
        }

        public override bool HasStableIds
        {
            get { return false; }
        }

        public override bool IsChildSelectable(int groupPosition, int childPosition)
        {
            return true;
        }

        class ViewHolderItem : Java.Lang.Object { }
    }

SendOrders.cs

 public class SendOrders : Android.Support.V7.App.AppCompatActivity
    {
        private int orderId, status;
        private string orders, agent, cust, orderDate, _sendDate;
        private ExpandableListView expandableList;
        ExpandableSendOrderAdapter ExpandableAdapter;
        private List<SendOrdersModel> OrdersModels = new List<SendOrdersModel>();
        private List<string> listDataHeader = new List<string>();
        private Dictionary<string, List<SendOrdersModel>> listDataChild = new Dictionary<string, List<SendOrdersModel>>();
        //int previousGroup = -1;
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.Send_Orders);
            Android.Support.V7.Widget.Toolbar toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
            SupportActionBar.Title = "Sent Orders";
            SupportActionBar.SetDisplayHomeAsUpEnabled(true);
            SupportActionBar.SetHomeButtonEnabled(true);
            expandableList = FindViewById<ExpandableListView>(Resource.Id.myexpandable);

            GetOrder();

            ExpandableAdapter = new ExpandableSendOrderAdapter(this, listDataHeader, listDataChild);
            expandableList.SetAdapter(ExpandableAdapter);


        }

        public override bool OnOptionsItemSelected(IMenuItem item)
        {
            if (item.ItemId == Android.Resource.Id.Home)
                Finish();
            return base.OnOptionsItemSelected(item);
        }

        //Retreive data
        private void GetOrder()
        {
            OrdersModels.Clear();
            DBAdapter db = new DBAdapter(this);
            db.openDB();
            ICursor cur = db.GetOrdersData();
            while (cur.MoveToNext())
            {
                orderId = cur.GetInt(0);
                agent = cur.GetString(1);
                cust = cur.GetString(2);
                orders = cur.GetString(3);
                status = cur.GetInt(4);
                orderDate = cur.GetString(5);
                _sendDate = cur.GetString(6);

                SendOrderBinding();
            }
            db.closeDB();
        }

        // Binding data
        private void SendOrderBinding()
        {
            SendOrdersModel ordersModel = new SendOrdersModel()
            {
                OrderId = orderId,
                Agent = agent,
                Cust = cust,
                Orders = orders,
                status = status,
                orderDate = orderDate,
                Sendate = _sendDate
            };
            OrdersModels.OrderBy(i => i.OrderId).ToList();
            OrdersModels.Add(ordersModel);
            if (OrdersModels.Count > 0)
            {
                // Do something here
                foreach (var item in OrdersModels)
                {
                    //add senddate to header list
                    if (!listDataHeader.Contains(item.Sendate))
                    {
                        listDataHeader.Add(item.Sendate);
                    }
                }

                //add all value 
                foreach (var items in listDataHeader)
                {
                    //literate header date
                    List<SendOrdersModel> list = new List<SendOrdersModel>();
                    foreach (var _items in OrdersModels)
                    {
                        //literate child data if have same data put it to the same list
                        if (_items.Sendate.Equals(items))
                        {
                            list.Add(_items);
                        }
                    }
                    try
                    {
                        listDataChild.Add(items, list);
                    }
                    catch(Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
            }
        }
    }
Tagged:

Best Answer

  • JunCainJunCain ✭✭✭ ✭✭✭
    Accepted Answer
    <TextView
            android:id="@+id/Orders"
            android:background="@drawable/leftBorder"
            android:textColor="#ffffff"
            android:paddingTop="10dp"
            android:paddingBottom="10dp"
            android:paddingLeft="30dp"
            android:lineSpacingMultiplier="1.50"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="2dp"
            android:layout_below="@id/cust"/>
    
    
    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
      <item android:bottom="-5dp"
            android:left="10dp"
            android:right="-5dp"
            android:top="-5dp">
        <shape android:shape="rectangle" >
          <stroke
              android:width="2dp"
              android:color="#000000" />
        </shape>
      </item>
      <item android:bottom="5dp"
            android:left="20dp"
            android:right="5dp"
            android:top="5dp">
        <shape android:shape="rectangle">
          <solid android:color="#000000" />
          <padding android:left="30dp" android:right="20dp" android:bottom="15dp" android:top="15dp" />
        </shape>
      </item>
    
    </layer-list>
    
      public override View GetChildView(int groupPosition, int childPosition, bool isLastChild, View convertView, ViewGroup parent)
            {
                string childText = (string)GetChild(groupPosition, childPosition);
                if (convertView == null)
                {
                    convertView = activity.LayoutInflater.Inflate(Resource.Layout.SendOrderTextView, null);
                }
                string[] childDate = childText.Split("_");
                TextView orderid = (TextView)convertView.FindViewById(Resource.Id.orderId);
                orderid.Text = "Order ID: " + childDate[0];
    
                TextView agentid = (TextView)convertView.FindViewById(Resource.Id.agent);
                agentid.Text = "Agent Name: " + childDate[1];
    
                TextView custid = (TextView)convertView.FindViewById(Resource.Id.cust);
                custid.Text = "Customer Name: " + childDate[2];
    
                TextView orders = (TextView)convertView.FindViewById(Resource.Id.Orders);
                string[] strArray = childDate[3].Split(";");
                string items = "";
                foreach (string product in strArray)
                {
                    string[] _product = product.Split("|");
                    int count = 0;
                    foreach (string _prodArr in _product)
                    {
                        if (items != "" && count % 4 == 1)
                        {
                            items += _prodArr.ToString() + " ";
                        }
                        else if (items != "" && count % 4 == 2)
                        {
                            items += _prodArr.ToString() + " ";
                        }
                        else if (items != "" && count % 4 == 3)
                        {
                            items += " ₱" + _prodArr.ToString() + ".00";
                        }
                        else if (items != "" && count % 4 == 0)
                        {
                            items += "\n" + _prodArr.ToString() + " ";
                        }
                        else
                        {
                            items += _prodArr.ToString() + " ";
                        }
    
                        count++;
                    }
                    orders.Text = items;
                }
    
                TextView status = (TextView)convertView.FindViewById(Resource.Id.status);
                if (childDate[4] == "0")
                {
                    status.Text = "Status: Unconfirmed";
                }
                else
                {
                    status.Text = "Status: Confirmed";
                }
    
                TextView orderdate = (TextView)convertView.FindViewById(Resource.Id.orderDate);
                orderdate.Text = "Ordered Date: " + childDate[5];
    
                TextView senddate = (TextView)convertView.FindViewById(Resource.Id.senddate);
                senddate.Text = "Sent: " + childDate[6];
                return convertView;
            }
    

Answers

  • JunCainJunCain ✭✭✭ Member ✭✭✭

    SendOrderTextView.axml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TextView
            android:id="@+id/orderId"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="2dp"/>
        <TextView
            android:id="@+id/agent"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="2dp"
            android:layout_below="@id/orderId"/>
        <TextView
            android:id="@+id/cust"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="2dp"
            android:layout_below="@id/agent"/>
        <TableLayout
            android:id="@+id/Orders"
            android:background="@drawable/solidBorder"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/cust"
            android:stretchColumns="*"
            style="@style/TableMP"
            android:isScrollContainer="true">
            <TableRow
                android:background="@drawable/tble_row_bg"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:padding="5dp">
                <TextView
                    android:background="@drawable/tble_cell_bg"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:paddingLeft="10dp"
                    android:paddingRight="10dp"
                    android:paddingTop="5dp"
                    android:paddingBottom="5dp"
                    android:textColor="@drawable/myColors"
                    android:text="Product Name" />
                <TextView
                    android:background="@drawable/tble_cell_bg"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:paddingLeft="10dp"
                    android:paddingRight="10dp"
                    android:paddingTop="5dp"
                    android:paddingBottom="5dp"
                    android:textColor="@drawable/myColors"
                    android:text="Unit" />
                <TextView
                    android:background="@drawable/tble_cell_bg"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:paddingLeft="10dp"
                    android:paddingRight="10dp"
                    android:paddingTop="5dp"
                    android:paddingBottom="5dp"
                    android:textColor="@drawable/myColors"
                    android:text="Quantity"/>
                <TextView
                    android:background="@drawable/tble_cell_bg"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:paddingLeft="10dp"
                    android:paddingRight="10dp"
                    android:paddingTop="5dp"
                    android:paddingBottom="5dp"
                    android:textColor="@drawable/myColors"
                    android:text="Price"/>
            </TableRow>
        </TableLayout>
        <TextView
            android:id="@+id/status"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="2dp"
            android:layout_below="@id/Orders"/>
        <TextView
            android:id="@+id/orderDate"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="2dp"
            android:layout_below="@id/status"/>
        <TextView
            android:id="@+id/senddate"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="2dp"
            android:layout_below="@id/orderDate"/>
    </RelativeLayout>
    

    SendOrderHeader.axml

    <?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">
    
        <TextView
            android:id="@+id/SendOrderHead"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:justificationMode="inter_word"
            android:gravity="center_horizontal"
            android:layout_margin="2dp"/>
    </LinearLayout>
    
  • JunCainJunCain ✭✭✭ Member ✭✭✭

    my expandable listview

    first click

    second click

  • LeonLuLeonLu Xamurai Member, Xamarin Team Xamurai

    First of all, you should print the value of listDataHeader and listDataChild in visual studio console, make sure data correctly. Then please did not use TableLayout, just show the child data directly.If it is normal.

  • LeonLuLeonLu Xamurai Member, Xamarin Team Xamurai

    Are there any updates for this issue? If the reply is helpful, please try to mark it as an answer, it will help others who have similar issue.

  • JunCainJunCain ✭✭✭ Member ✭✭✭

    I remove the tablelayout and customize my textview that will looks like a tablelayout.

  • JunCainJunCain ✭✭✭ Member ✭✭✭
    Accepted Answer
    <TextView
            android:id="@+id/Orders"
            android:background="@drawable/leftBorder"
            android:textColor="#ffffff"
            android:paddingTop="10dp"
            android:paddingBottom="10dp"
            android:paddingLeft="30dp"
            android:lineSpacingMultiplier="1.50"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="2dp"
            android:layout_below="@id/cust"/>
    
    
    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
      <item android:bottom="-5dp"
            android:left="10dp"
            android:right="-5dp"
            android:top="-5dp">
        <shape android:shape="rectangle" >
          <stroke
              android:width="2dp"
              android:color="#000000" />
        </shape>
      </item>
      <item android:bottom="5dp"
            android:left="20dp"
            android:right="5dp"
            android:top="5dp">
        <shape android:shape="rectangle">
          <solid android:color="#000000" />
          <padding android:left="30dp" android:right="20dp" android:bottom="15dp" android:top="15dp" />
        </shape>
      </item>
    
    </layer-list>
    
      public override View GetChildView(int groupPosition, int childPosition, bool isLastChild, View convertView, ViewGroup parent)
            {
                string childText = (string)GetChild(groupPosition, childPosition);
                if (convertView == null)
                {
                    convertView = activity.LayoutInflater.Inflate(Resource.Layout.SendOrderTextView, null);
                }
                string[] childDate = childText.Split("_");
                TextView orderid = (TextView)convertView.FindViewById(Resource.Id.orderId);
                orderid.Text = "Order ID: " + childDate[0];
    
                TextView agentid = (TextView)convertView.FindViewById(Resource.Id.agent);
                agentid.Text = "Agent Name: " + childDate[1];
    
                TextView custid = (TextView)convertView.FindViewById(Resource.Id.cust);
                custid.Text = "Customer Name: " + childDate[2];
    
                TextView orders = (TextView)convertView.FindViewById(Resource.Id.Orders);
                string[] strArray = childDate[3].Split(";");
                string items = "";
                foreach (string product in strArray)
                {
                    string[] _product = product.Split("|");
                    int count = 0;
                    foreach (string _prodArr in _product)
                    {
                        if (items != "" && count % 4 == 1)
                        {
                            items += _prodArr.ToString() + " ";
                        }
                        else if (items != "" && count % 4 == 2)
                        {
                            items += _prodArr.ToString() + " ";
                        }
                        else if (items != "" && count % 4 == 3)
                        {
                            items += " ₱" + _prodArr.ToString() + ".00";
                        }
                        else if (items != "" && count % 4 == 0)
                        {
                            items += "\n" + _prodArr.ToString() + " ";
                        }
                        else
                        {
                            items += _prodArr.ToString() + " ";
                        }
    
                        count++;
                    }
                    orders.Text = items;
                }
    
                TextView status = (TextView)convertView.FindViewById(Resource.Id.status);
                if (childDate[4] == "0")
                {
                    status.Text = "Status: Unconfirmed";
                }
                else
                {
                    status.Text = "Status: Confirmed";
                }
    
                TextView orderdate = (TextView)convertView.FindViewById(Resource.Id.orderDate);
                orderdate.Text = "Ordered Date: " + childDate[5];
    
                TextView senddate = (TextView)convertView.FindViewById(Resource.Id.senddate);
                senddate.Text = "Sent: " + childDate[6];
                return convertView;
            }
    
  • LeonLuLeonLu Xamurai Member, Xamarin Team Xamurai
    edited June 21

    Ok, thanks for your sharing, if you used customize textview, is this issue disappear? If so, please mark your reply as answer, it will help others who have simialr issue.

  • JunCainJunCain ✭✭✭ Member ✭✭✭

    yes..

  • LeonLuLeonLu Xamurai Member, Xamarin Team Xamurai

    @JunCain you forget to mark it. :)

  • JunCainJunCain ✭✭✭ Member ✭✭✭

    o, sorry Sir..

  • johnrudejohnrude Member

    thank you..

Sign In or Register to comment.