gpt4 book ai didi

c# - 如何在 android Xamarin 中动态填充表格布局?

转载 作者:行者123 更新时间:2023-11-30 00:28:17 26 4
gpt4 key购买 nike

我想使用 webapi popualte 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>

这是我的 Activity 代码所有OrderRestaurentActivity

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);
}
}

}

最佳答案

只需进行以下更改即可解决

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

将此代码添加到 Foreach 循环中并添加:

tableRow.RemoveAllViews();

关于c# - 如何在 android Xamarin 中动态填充表格布局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44968413/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com