GridView不显示数据

时间:2017-09-16 08:52:21

标签: c# gridview xamarin.android

我是C#和Xamarin的初学者。我有这个代码,但我不知道它有什么问题,它不会在gridview中显示数据。

这是我活动的代码:

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" />

每件事似乎都很好,我可以进入if声明,但我的网格视图中没有任何内容。任何人都知道这段代码有什么问题? 先谢谢。

我试过用

List<FoodTable> tvShows = new List<FoodTable>();
JavaList<FoodTable> tvShows = new JavaList<FoodTable>();
JavaList<String> tvShows = new JavaList<String>();

但他们不适合我。

2 个答案:

答案 0 :(得分:0)

尝试将List<>直接放在 GridView 中:

tvShows.Add(data1);

if (tvShows.Size() > 0)
{
    gv.Adapter = tvShows;
}

答案 1 :(得分:0)

  

无法隐式转换类型System.Collections.Generic.List&#39;到&#39; Android.Widget.IListAdapter&#39;

您将tvShows的类型定义为JavaList<string>,因此当您使用tvShows.Add(data1)方法时,data1的类型应为string type。但您的data1's类型为FoodTable,它们不兼容。

正如AT-2017所说,使用List<>将解决此问题,使用方式如下:

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

当您将列表data1添加到列表tvShows时,您可以使用List.AddRange方法:

tvShows.AddRange(data1);

如果您想在FoodTable中显示GridView的数据,可以实施custom adapter

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

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>

在您的代码中使用它:

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

效果如this

相关问题