Xamarin Android C#:使用SQLite数据库中的数据填充ListView自定义行

时间:2017-02-01 19:47:24

标签: c# android sqlite listview xamarin

我要创建一个ListView,它从SQLite数据库(预先存在的db文件)提供数据。 ListView使用自定义行,这是困扰我的方面之一;如何提取数据然后将其分配给自定义行中的TextView?

我是Xamarin的新手,所以非常感谢你能给我的任何指导,谢谢!

运行时当前对话框错误:

Java.Lang.ClassNotFoundException: Didn't find class      "md5c4f65b5cf99ab8e97737acf0f8ec7efd.DBHelper" on path: DexPathList[[zip file "/data/app/AppName.Droid-1/base.apk"],nativeLibraryDirectories=[/data/app/AppName.Droid-1/lib/arm, /system/fake-libs, /data/app/AppName.Droid-1/base.apk!/lib/armeabi-v7a, /system/lib, /vendor/lib]]

' MainActivity':

public class EateriesActivity : Activity
{
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        Title = "Find An Eatery";
        SetContentView(Resource.Layout.Eateries);


        DBHelper db = new DBHelper();
        db.CreateDatabase();
        db.CreateTable();

        var items = db.GetData();
        var listView = FindViewById<ListView>(Resource.Id.EateryList_ListView);

        listView.Adapter = new ArrayAdapter<string>(this, Resource.Layout.Eatery_ListView_Row, items);
    }
}

ListViewAdapter(这是必要的吗?):

class ListViewAdapter : BaseAdapter<Eatery>
{
    private List<Eatery> myItems;
    private Context myContext;
    public ListViewAdapter(Context context, List<Eatery> items)
    {
        myItems = items;
        myContext = context;
    }
    public override int Count
    {
        get { return myItems.Count; }
    }

    public override long GetItemId(int position)
    {
        return position;
    }

    public override Eatery this[int position]
    {
        get
        {
            return myItems[position];
        }
    }

    public override View GetView(int position, View convertView, ViewGroup parent)
    {
        View row = convertView;

        if (row == null)
        {
            row = LayoutInflater.From(myContext).Inflate(Resource.Layout.Eatery_ListView_Row, null, false);
        }

        TextView placeName = row.FindViewById<TextView>(Resource.Id.placeName);
        placeName.Text = myItems[position].Name;

        TextView placeTown = row.FindViewById<TextView>(Resource.Id.placeTown);
        placeTown.Text = myItems[position].Town;

        return row;
    }
}

食堂:

public class Eatery
{
    [PrimaryKey]
    public string ID { get; set; }
    public string Image { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public string Address1 { get; set; }
    public string Town { get; set; }
    public string PostCode { get; set; }
    public string Cuisine { get; set; }
}

DBHelper:

public class DBHelper
{
    private static string DB_PATH = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
    private static string DB_NAME = "EateryDB.db";

    public void CreateDatabase()
    {
        string dbPath = Path.Combine(DB_PATH, DB_NAME);
        var db = new SQLiteConnection(new SQLite.Net.Platform.XamarinAndroid.SQLitePlatformAndroid(), dbPath);
    }

    public void CreateTable()
    {
        string dbPath = Path.Combine(DB_PATH, DB_NAME);
        var db = new SQLiteConnection(new SQLite.Net.Platform.XamarinAndroid.SQLitePlatformAndroid(), dbPath);
        db.CreateTable<Eatery>();
    }

    public List<string> GetData()
    {
        string dbPath = Path.Combine(DB_PATH, DB_NAME);
        var db = new SQLiteConnection(new SQLite.Net.Platform.XamarinAndroid.SQLitePlatformAndroid(), dbPath);
        List<string> data = new List<string>();
        foreach (var item in db.Table<Eatery>())
        {
            var place = item.Name.ToString(); item.Town.ToString();

            data.Add(place);
        }
        return data;
    }

1 个答案:

答案 0 :(得分:0)

基于您的两个版本的代码,我认为您需要的是将数据库中的数据添加到for (size_t c = 0; c < sizeof pois / sizeof pois[0]; ++c) { // Here you can use `pois[c]` }

然后你可以这样编码:

myItems

最后将这些数据添加到您的适配器,就像硬编码时那样:

List<Eatery> data = new List<Eatery>();
foreach (var item in db.Table<Eatery>())
{
     var place = item.Name.ToString();
     var location = item.Location.ToString();
     var rating = item.Rating;
     item.Town.ToString();
     data.Add(new Eatery() { PlaceName = place, PlaceLocation = location, PlaceRating = rating });
}