Wednesday 20 January 2016

xamarin - can't view sqlite table data in gridview (C#), System.Collections.Generic.List



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 tvShows = new JavaList();


protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.MenuFood);
gv = FindViewById(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();
var data1 = (from values in data
select new FoodTable
{
Shenase = values.Shenase,
Types = values.Types,
Names = values.Names,
Costs = values.Costs

}).ToList();

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



     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]

Answer




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




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



Modify your code :



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


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 :




android:layout_width="fill_parent"
android:layout_height="40dp"
android:orientation="horizontal"
android:padding="8dp"
>

android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/Shenase"
android:layout_weight="1"/>
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/Types"
android:layout_weight="1" />
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/Names"
android:layout_weight="1" />
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/Costs"
android:layout_weight="1" />




Create a GridViewAdapter :



public class GridViewAdapter : BaseAdapter
{
private MainActivity mContext;
private List tvShows;

public GridViewAdapter(MainActivity context, List 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(Resource.Id.Shenase).Text = item.Types;
view.FindViewById(Resource.Id.Types).Text = item.Types;
view.FindViewById(Resource.Id.Names).Text = item.Names;
view.FindViewById(Resource.Id.Costs).Text = item.Costs;

return view;
}
}


Use it in your code :



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


Effect like this.


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...