2017-09-16 102 views
0

我是C#和Xamarin的初學者。我有這個代碼,但我不知道它有什麼問題,它不會在gridview中顯示數據。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 statment但沒有什麼在我的gridview。任何人都知道這個代碼有什麼問題? 預先感謝。

我試着使用

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

,但他們並沒有爲我工作。

回答

0

嘗試把List<>直接在GridView控件

tvShows.Add(data1); 

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

這會導致錯誤**不能隱式地將類型'System.Collections.Generic.List '轉換爲'Android.Widget.IListAdapter'** @ AT-2017 –

+0

請嘗試一件事。把這個清單傳給這個 - ** JavaList tvShows = new JavaList (); **。 –

+0

你的代碼看起來幾乎沒問題。請訪問此鏈接,看看是否提供任何幫助 - http://www.codeproject.com/Articles/800908/Displaying-Data-with-an-Adapter-in-Xamarin-Android –

0

不能隱式轉換類型 'System.Collections.Generic.List' 到 'Android.Widget.IListAdapter'

您將tvShows的類型定義爲JavaList<string>,因此當您使用tvShows.Add(data1)方法時,data1的類型應爲string type。但您的data1's類型爲FoodTable,它們不兼容。

截至-2017說,使用List<>會解決這個問題,這樣的用法:

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

當您添加列表data1列出tvShows,你可以使用List.AddRange方法:

tvShows.AddRange(data1); 

如果要在GridView中顯示FoodTable的數據,可以執行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影響。

+0

@ z.r,你是否解決了你的問題? –