2017-07-07 98 views
0

我想使用webapi populte tablelayout,但得到錯誤:如何動態填充android Xamarin中的表格佈局?

「指定的孩子已經有一個父母,你必須首先調用孩子的父母removeView()。

這裏是我的代碼

allOrderRestaurent.axml

<?xml version="1.0" encoding="utf-8"?> 
    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/tblAllOrderRestaurent"> 
    <TableRow 
     android:background="#ECEFF1"> 
    <ImageView 
     android:layout_width="80dp" 
     android:layout_height="80dp"  
     android:src="@drawable/icon" /> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"  
     android:text="Android Lollipop" 
     android:textSize="18dp" 
     android:layout_marginTop="25dp" 
     android:id="@+id/txtproductname" 
      /> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="21" 
     android:textSize="18dp" 
     android:id="@+id/txtproductprice" 
     android:layout_marginTop="25dp" /> 
    </TableRow> 
</TableLayout> 

這裏是我acitvity代碼 AllOrderRestaurentActivity

public List<UserCartModel> mItems; 
    TableLayout tablelayout; 
    TextView productname, price; 
    int sessionid; 
    protected override async void OnCreate(Bundle savedInstanceState) 
    { 
     base.OnCreate(savedInstanceState); 
     SetContentView(Resource.Layout.allorderRestaurent); 
     sessionid = Common.GetSessionValue(); 
     List<UserCartModel> mItems = await GetData(); 

     tablelayout = FindViewById<TableLayout>(Resource.Id.tblAllOrderRestaurent); 
     productname = FindViewById<TextView>(Resource.Id.txtproductname); 
     price = FindViewById<TextView>(Resource.Id.txtproductprice); 
     BindData(); 
     // Create your application here 
    } 

    public async Task<List<UserCartModel>> GetData() 
    { 
     HttpClient client = new HttpClient(); 
     string ac = Common.Url; 
     string url = ac + "RestaurentOderList?id=" + sessionid; 

     var request = HttpWebRequest.Create(url); 
     request.Method = "GET"; 

     var response = await client.GetAsync(url); 
     if (response.IsSuccessStatusCode) 
     { 
      var content = await response.Content.ReadAsStringAsync(); 
      mItems = JsonConvert.DeserializeObject<List<UserCartModel>>(content); 
     } 
     return mItems; 
    } 

    public void BindData() 
    { 
     try 
     { 
      TableRow tableRow = new TableRow(this); 
      TableRow.LayoutParams layoutParams = new TableRow.LayoutParams(
    ViewGroup.LayoutParams.MatchParent, 
    ViewGroup.LayoutParams.MatchParent); 

      foreach (var r in mItems) 
      { 

       productname = new TextView(this); 
       productname.Text = r.ProductName; 
       productname.LayoutParameters = layoutParams; 

       price = new TextView(this); 
       price.Text = Convert.ToString(r.Price); 
       price.LayoutParameters = layoutParams; 

       tableRow.AddView(price, 0); 
       tableRow.AddView(price, 1); 

       tablelayout.AddView(tableRow, 0); 
      } 
     } 
     catch (Exception exx) 
     { 
      Console.WriteLine("Error During Bind Table Layout All Order Restaurent" + exx.Message); 
     } 
    } 

} 

回答

0

通過使下面正好解決了它的變化

TableRow tableRow = new TableRow(this); 
TableRow.LayoutParams layoutParams = new TableRow.LayoutParams(
ViewGroup.LayoutParams.MatchParent, 
ViewGroup.LayoutParams.MatchParent); 

將此代碼添加到Foreach循環中並添加:

tableRow.RemoveAllViews();