2017-09-13 59 views
0

我是C#的初学者,我有一个xamarin.android项目,我想查看我的表的数据网格视图。无法在gridview(C#)中查看sqlite表格数据,System.Collections.Generic.List

使用此代码为我的活动:

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文件

<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语句的结果是对的,但行

gv.Adapter = adapter; 

不起作用,所以我收到这行代替获取我的数据。

​​
+0

为什么使用JavaList ?将它列入列表 tvShows = new List ();从使用System.Collections.Generic; – Merian

+0

我得到这个错误:**无法从'System.Collections.Generic.List 转换为'System.Collections.Generic.IEnumerable **,在这一行:'tvShows.Add(data1) ;'@美利安 – tbp

回答

0

System.Collections.Generic.List[MainAppHotelXamarin.FoodTable]

您定义的tvShows类型为JavaList<string>,所以当你使用tvShows.Add(data1)法,data1类型应该是string型。但是你的data1的类型是List<FoodTable>,它们是不兼容的。

修改代码:如果你要显示的数据

tvShows.AddRange(data1); 

//Change the type form string to FoodTable 
List<FoodTable> tvShows = new List<FoodTable>(); 

当您添加您的List数据1至List tvShows,你可以使用List.AddRange方法,使用这样的您的FoodTableGridView中,您可以为您的班级实施custom adapter,这里是example

编辑:

HereListView适配器为例,它与GridView适配器类似,您可以使用它来实现你的功能。

编辑2:

您可以创建一个自定义布局项目,以显示你想显示的任何视图类型。

例如,创建一个布局以显示四位TextView

item.axml

<?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> 

创建GridViewAdapter

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

使用它在你的代码:

adapter = new GridViewAdapter(this, tvShows);// not use ArrayAdapter 

类似this的效果。

+0

非常感谢。我编辑了这些行:'List tvShows = new List ();','tvShows.AddRange(data1);'和'if(tvShows.Count> 0)'。现在没有任何错误,但我不能使用** create_a_grid_view **项目,因为它用于在gridview中查看图像,而我的是'string'。你会给我任何关于这个问题的链接或想法吗? @York Shen - MSFT – tbp

+0

@tbp,你解决了你的问题吗? –

+0

Shen -MSFT感谢您的解决方案。 **第二编辑**非常适合我。 – tbp