2017-07-07 16 views
0

webapiを使用してテーブルレイアウトをポップアップしたいが、エラーが発生する: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); 
を変更10

このコードをForeachループに追加し、追加:

tableRow.RemoveAllViews(); 
関連する問題