4

我无法用数据库项填充 ListView。

sqlite中用于获取表的功能:

public Android.Database.ICursor getFornecedores()
        {
            Android.Database.ICursor temp = null;
            try
            {
                sqlQuery = "SELECT * FROM Fornecedor;";
                temp = sqlTemp.RawQuery(sqlQuery, null);
                if (!(temp != null))
                    Console.WriteLine("Deu pau");
            }
            catch(SQLiteException e)
            {
                Console.WriteLine("Erro = " + e.Message);
            }
            return temp;
        }

我尝试填充listView的功能:

public void loadListView()
        {
            Android.Database.ICursor fornecedores;
            DataBaseHandler db = new DataBaseHandler();
            fornecedores = db.getFornecedores();
            SimpleCursorAdapter adapter;

            cadastroListView.Adapter = new SimpleCursorAdapter(this, Android.Resource.Layout.SimpleDropDownItem1Line, fornecedores,
                                        new string[] { "nome" }, new int[] { Android.Resource.Id.Text1 });


        }

请帮帮我!谢谢!

4

1 回答 1

0
        List<string> mItems = new List<string>();
        var listview = FindViewById<ListView>(Resource.Id.listview);

 //fetching data from database. I am using sqlite.cs to fetch data from database.
 //You can use your technique which you are using.

        using (var conn = new SQLite.SQLiteConnection(dbPath))
        {
            try
            {
                var cmd = new SQLite.SQLiteCommand(conn);
                cmd.CommandText = "select * from history";
                int i = 0;
                var r = cmd.ExecuteQuery<hist>();
                foreach (var item in r)
                {
                    mItems.Add(item.word);
                }

            }
            catch (Exception e)
            {

            }
        }

        ArrayAdapter<string> adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, mItems);

        listview.Adapter = adapter;
于 2017-07-10T16:52:06.950 回答