can't view sqlite table data in gridview (C#), System.Collections.Generic.List
我是C的初学者,我有一个xamarin.android项目,我想在网格视图中查看表的数据。
将此代码用于我的活动:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | public class MenuFoodActivity : Activity { string dpPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal),"HotelDb.db3"); GridView gv; ArrayAdapter adapter; JavaList<String> tvShows = new JavaList<string>(); protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); SetContentView(Resource.Layout.MenuFood); gv = FindViewById<GridView>(Resource.Id.gridViewMenu); adapter = new ArrayAdapter(this, Android.Resource.Layout.SimpleListItem1, tvShows); Retrieve(); } private void Retrieve() { var db = new SQLiteConnection(dpPath); var data = db.Table<FoodTable>(); var data1 = (from values in data select new FoodTable { Shenase = values.Shenase, Types = values.Types, Names = values.Names, Costs = values.Costs }).ToList<FoodTable>(); tvShows.Add(data1); if (tvShows.Size() > 0) { gv.Adapter = adapter; } else { Toast.MakeText(this,"not found.", ToastLength.Short).Show(); } } } |
这个是AXML文件
1 2 3 4 5 6 7 8 | <GridView android:minWidth="25px" android:minHeight="25px" android:layout_width="wrap_content" android:layout_height="200dip" android:id="@+id/gridViewMenu" android:background="#aaa" android:layout_marginTop="10dip" /> |
问题是,在调试项目时,字段"data1"具有数据,if语句的结果为true,但行
1 | gv.Adapter = adapter; |
不起作用,所以我收到这行而不是得到我的数据。
1 | System.Collections.Generic.List[MainAppHotelXamarin.FoodTable] |
System.Collections.Generic.List[MainAppHotelXamarin.FoodTable]
您将
修改代码:
1 2 |
在
1 | tvShows.AddRange(data1); |
如果要在
编辑:
下面是
编辑2:
您可以创建一个自定义布局项来显示您想要显示的任何视图类型。
例如,创建一个布局以显示四个
IX.AXML:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="40dp" android:orientation="horizontal" android:padding="8dp" > <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:id="@+id/Shenase" android:layout_weight="1"/> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:id="@+id/Types" android:layout_weight="1" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:id="@+id/Names" android:layout_weight="1" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:id="@+id/Costs" android:layout_weight="1" /> </LinearLayout> |
创建一个
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | public class GridViewAdapter : BaseAdapter<FoodTable> { private MainActivity mContext; private List<FoodTable> tvShows; public GridViewAdapter(MainActivity context, List<FoodTable> tvShows) { this.mContext = context; this.tvShows = tvShows; } public override FoodTable this[int position] { get { return tvShows[position]; } } public override int Count => tvShows.Count; public override long GetItemId(int position) { return position; } public override View GetView(int position, View convertView, ViewGroup parent) { FoodTable item = tvShows[position]; View view = convertView; // re-use an existing view, if one is available if (view == null) view = mContext.LayoutInflater.Inflate(Resource.Layout.item, null); view.FindViewById<TextView>(Resource.Id.Shenase).Text = item.Types; view.FindViewById<TextView>(Resource.Id.Types).Text = item.Types; view.FindViewById<TextView>(Resource.Id.Names).Text = item.Names; view.FindViewById<TextView>(Resource.Id.Costs).Text = item.Costs; return view; } } |
在代码中使用它:
1 |
像这样的效果。