获取光标内容

Getting cursor content

我正在尝试获取游标的内容,但我在游标中包含的 2 个值中只有一个值

这是我的代码

public List<Countries> getAllCountries() {
    List<Countries> country = new ArrayList<>();

    // Select All Query
    String selectQuery = "SELECT  * FROM " + BDD.TABLE_COUNTRIES;

    SQLiteDatabase db = dataBase.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            Countries c = new Countries();
            c.setName(cursor.getString(cursor.getColumnIndex(BDD.NAME_COUNTRY)));
            c.setCode(cursor.getInt(cursor.getColumnIndex(BDD.CODE)));


            country.add(c);
        } while (cursor.moveToNext());
    }

    // close db connection
    db.close();

    // return notes list
    return country;
}

我只能得到这一行`c.setName(cursor.getString(cursor.getColumnIndex(BDD.NAME_COUNTRY)));

我试过反转线,但它没有改变 这是获取光标内容的代码

final ListView mListView = (ListView)findViewById(R.id.listView);


    //Création d'un livre
   final Countries country = new Countries("Cameroun", 232);

    ajout.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View v){

            //On ouvre la base de données pour écrire dedans
            DbM.open();
            //On insère le livre que l'on vient de créer
            DbM.addCountry(country);

            int countryFromBdd = DbM.getNumber();
            List<Countries> values = DbM.getAllCountries();

            if(countryFromBdd != 0){

                final ArrayAdapter<Countries> adapter = new ArrayAdapter<Countries>(CountryDataBaseManagement.this,
                        android.R.layout.simple_list_item_1, values);
                mListView.setAdapter(adapter);

            }
        }
    });

我不知道如何让这一行显示在列表视图中`c.setCode(cursor.getInt(cursor.getColumnIndex(BDD.CODE)));

感谢您的帮助`

您不需要 getColumnIndex。您可以将 Cursor 与索引一起使用,例如 0、1、2 等。

尝试改变:

        Countries c = new Countries();
        c.setName(cursor.getString(cursor.getColumnIndex(BDD.NAME_COUNTRY)));
        c.setCode(cursor.getInt(cursor.getColumnIndex(BDD.CODE)));

        Countries c = new Countries();
        c.setName(cursor.getString(0));
        c.setCode(cursor.getInt(1));

如果您的列顺序相同。