数据库
首页 > 数据库> > android-使用游标在sqlite中使用列值检索列名

android-使用游标在sqlite中使用列值检索列名

作者:互联网

我想使用光标在Listview中显示带有列值的列名.
我现在的代码仅显示列值.

public void openAndQueryDatabase() {

    db = openOrCreateDatabase( "mydatabase.db", SQLiteDatabase.CREATE_IF_NECESSARY , null ); 

    Cursor cursor = db.rawQuery("select * from "+ table + " where name='"+ name + "'",  null);


    int count = cursor.getColumnCount();

    if (cursor!=null )
    { 

        if  (cursor.moveToFirst())
        {
            do 
            {
                 for (int i =0 ; i< count; i++)
                {
                String data = cursor.getString(i);
                details.add(data);
                }
            } 
            while (cursor.moveToNext()); 
        } 

         }
}

解决方法:

在这种情况下,您应该使用hashmap

for (int i =0 ; i< count; i++)
{
    String data = cursor.getString(i);
    String column_name = cursor.getColumnName(i);

    HashMap<String,String> map = new HashMap<String,String>();
    map.put("column_value",data);
    map.put("column_name",column_name);

    details.add(map); //change the type of details from ArrayList<String> to arrayList<HashMap<String,String>>
}

标签:sqlite,android-cursor,android
来源: https://codeday.me/bug/20191029/1961527.html