1

I'm beginner in C#, I have a xamarin.android project, and I want to view the data of my table in a gridview.

Using this code for 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" />

The problem is that when I'm debugging the project the field 'data1' has the data and the result of if statement is true, but the line

gv.Adapter = adapter;

does not work so I recieve this line instead of getting my data.

System.Collections.Generic.List[MainAppHotelXamarin.FoodTable]
tbp
  • 75
  • 1
  • 7
  • Why are working with JavaList? Chnage it to List tvShows = new List(); from using System.Collections.Generic; – Stefanija Sep 13 '17 at 14:26
  • I get this error: **cannot convert from 'System.Collections.Generic.List to 'System.Collections.Generic.IEnumerable**, on this line: `tvShows.Add(data1);` @Merian – tbp Sep 16 '17 at 07:54

1 Answers1

0

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

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 List<FoodTable>, they are incompatible.

Modify your code :

//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, usage like this :

tvShows.AddRange(data1);

If you want to display the data of your FoodTable in a GridView, you could Implement custom adapter for your class and here is an example.

EDIT :

Here is an example for ListView adapter, it's similar with GridView adapter, you could use this to implement your feature.

EDIT 2 :

You could create a custom layout item to display whatever view type you want to show.

For example, create a layout to display four TextView :

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>

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

Use it in your code :

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

Effect like this.

Eren Shen
  • 8,468
  • 1
  • 12
  • 37
  • Thanks so much. I edited these lines: `List tvShows = new List();` , `tvShows.AddRange(data1);` and `if (tvShows.Count > 0)`. Now there's not any error but I couldn't use the **create_a_grid_view** project because it's for viewing images in a gridview and mine is `string`. Would you give me any link or idea for this issue? @York Shen - MSFT – tbp Sep 14 '17 at 09:41
  • Shen -MSFT Thanks for your solution. The **second edit** works for me very well. – tbp Sep 18 '17 at 09:15