简体   繁体   中英

GridView doesn't display data

I'm a beginner in C# and Xamarin. I have this code but I don't know what's wrong with it, that doesn't display data in gridview.

this is the code of my activity:

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

and this one is for axml file:

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

every thing seems fine and I can enter to the if statment but there's nothing in my gridview. Anyone knows what's the problem with this code? Thanks beforehand.

I've tried to use

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

but they didn't work for me.

Try putting the List<> in the GridView directly:

tvShows.Add(data1);

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

cannot implicitly convert type 'System.Collections.Generic.List' to 'Android.Widget.IListAdapter'

You defined the type of tvShows as JavaList<string> , so when you use tvShows.Add(data1) method, the type of data1 should be string type . But your data1's type is FoodTable , they are incompatible.

As AT-2017 said, use List<> will solve this problem, usage like this :

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

When you add a your list data1 to list tvShows , you could use List.AddRange method :

tvShows.AddRange(data1);

If you want to display the data of your FoodTable in a GridView , you could implement a custom adapter :

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

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

Use it in your code :

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

Effect like this .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM