我想使用webapi poplayte tablelayout但收到错误:
“指定的孩子已经有父母。你必须首先在孩子的父母上调用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>
这是我的活动代码 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 :(得分:0)
通过进行以下更改来解决问题
TableRow tableRow = new TableRow(this);
TableRow.LayoutParams layoutParams = new TableRow.LayoutParams(
ViewGroup.LayoutParams.MatchParent,
ViewGroup.LayoutParams.MatchParent);
将此代码添加到Foreach循环并添加:
tableRow.RemoveAllViews();