从SQLite获取数据到ArrayAdapter / ListView - Android C#Xamarin

时间:2017-06-03 08:26:23

标签: c# android sqlite listview android-arrayadapter

我正在尝试将SQLite表中的数据转换为List<string>,然后转换为ArrayAdapter<string>ListView。 如何使用ListView将以下代码中的数据提取到ArrayAdapter

DB Helper.cs:

  public List<string> getNoteList()
    {
        List<string> noteList = new List<string>();
        SQLiteDatabase db = this.ReadableDatabase;
        ICursor cursor = db.Query(DB_TABLE, new string[] { DB_COLUMN}, null, null, null, null, null);
        while (cursor.MoveToNext())
        {
            int index = cursor.GetColumnIndex(DB_COLUMN);
            noteList.Add(cursor.GetString(index));
        }
        return noteList;
    }

正如您所看到的,它被放入noteList,但我如何编码数组适配器以便noteList进入ListView

更新1: MainActivity.cs

public void LoadNoteList()
        {
            List<string> noteList = dbHelper.getNoteList();
            adapter = new ArrayAdapter<string>(this, Resource.Layout.list_black_text, noteList);
            lvNotes.Adapter = adapter;
        }

错误:

Error Image

2 个答案:

答案 0 :(得分:0)

将noteList的类型更改为ArrayList,并在ListView初始化后的位置添加跟随行

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, noteList);
listview.setAdapter(adapter);

答案 1 :(得分:0)

  

试试这个   这里cards是我的linked-list

final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_single_choice);
        for (MerchantCard card : cards) {
            arrayAdapter.add(card.getName());
        }

        ListView listView = (ListView) mView.findViewById(R.id.listView);
        listView.setAdapter(arrayAdapter);
        listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                selectedCardTypeId = cards.get(position).getId();
                Log.d("request", cards.get(position).getId() + " " + cards.get(position).getName());
            }
        });