列表视图不显示数据库Android中的数据

时间:2016-03-04 15:17:16

标签: android sqlite listview

我在创建数据库和在列表视图中显示数据时遇到了问题,而且我对核心概念还不清楚。

我编码的ListView没有显示任何内容。

DataBase Helper类(创建和管理数据库的类):

public class DBController extends SQLiteOpenHelper{

    private static final String DATABASE_NAME = "quiz.db";
    private static final String TABLE_NAME = "questions";
    //DATABASE COLUMNS
    private static final String COL1 = "que";
    private static final String COL2 = "opta";
    private static final String COL3 = "optb";
    private static final String COL4 = "optc";
    private static final String COL5 = "ans";
    public DBController(Context applicationcontext) {
        super(applicationcontext, DATABASE_NAME, null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase database) {
        String query;
        query = "CREATE TABLE IF NOT EXISTS "+TABLE_NAME+" ( _id INTEGER PRIMARY KEY AUTOINCREMENT, "+COL1+" TEXT, "+COL2+" TEXT, "+COL3+" TEXT,"+COL4+" TEXT,"+COL5+" TEXT);";
        database.execSQL(query);
    }
    @Override
    public void onUpgrade(SQLiteDatabase database, int version_old, int current_version) {
        String query;
        query = "DROP TABLE IF EXISTS questions";
        database.execSQL(query);
        onCreate(database);
    }

    public void insertQuestion(Datas data)
    {
        this.getWritableDatabase().rawQuery("INSERT INTO "+TABLE_NAME+"("+COL1+","+COL2+","+COL3+","+COL4+","+COL5+") VALUES('"+data.getQuestion()+"','"+data.getOptionA()+"','"+data.getOptionB()+"','"+data.getOptionC()+"','"+data.getAnswer()+"');", null);
    }
    public Cursor getAllData()
    {
        return this.getWritableDatabase().rawQuery("SELECT * FROM "+TABLE_NAME, null);
    }
}

我添加到显示 ListView 代码如下,

我只是添加了 活动类 OnCreate()方法:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_questions);
        DBController db = new DBController(getApplicationContext());
        int from[] = {android.R.id.text1,android.R.id.text2};
        String to[] = {"que","ans"};
        SimpleCursorAdapter sdp = new SimpleCursorAdapter(this,android.R.layout.simple_list_item_1,db.getAllData(),from,to,0);
        ListView lv =(ListView) findViewById(R.id.listView1);
        lv.setAdapter(sdp);
    }

2 个答案:

答案 0 :(得分:0)

ReactDOMServer.renderToString导致问题。

getAllData()方法的正确代码是,

db.getAllData()

光标定位是个问题。

答案 1 :(得分:0)

使用此

更改您的DBController
public class DBController extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "quiz.db";
    private static final String TABLE_NAME = "questions";
    //DATABASE COLUMNS
    private static final String COL1 = "que";
    private static final String COL2 = "opta";
    private static final String COL3 = "optb";
    private static final String COL4 = "optc";
    private static final String COL5 = "ans";

    public DBController(Context applicationcontext) {
        super(applicationcontext, DATABASE_NAME, null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase database) {
        String query;
        query = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " (_id INTEGER PRIMARY KEY AUTOINCREMENT, " + COL1 + " TEXT, " + COL2 + " TEXT, " + COL3 + " TEXT," + COL4 + " TEXT," + COL5 + " TEXT);";
        database.execSQL(query);
    }

    @Override
    public void onUpgrade(SQLiteDatabase database, int version_old, int current_version) {
        String query;
        query = "DROP TABLE IF EXISTS questions";
        database.execSQL(query);
        onCreate(database);
    }

    public void insertQuestion(Datas data) {


        ContentValues values = new ContentValues();
        values.put(COL1, data.getQuestion());
        values.put(COL2, data.getOptionA());
        values.put(COL3, data.getOptionB());
        values.put(COL4, data.getOptionC());
        values.put(COL5, data.getAnswer());

        this.getWritableDatabase().insert(TABLE_NAME, null, values);


    }

    public Cursor getAllData() {

        String selectQuery = "SELECT * from " + TABLE_NAME;

        Cursor cursor = this.getWritableDatabase().rawQuery(selectQuery, null);
        return cursor;
    }

使用

更改您的活动
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_questions);
        try {
            DBController db = new DBController(getApplicationContext());

            db.insertQuestion(new Datas("How to Show Data in ListView through Database?", "A", "B", "C", "123"));

            int i[] = {android.R.id.text1, android.R.id.text2};
            String str[] = {"que", "ans"};
            SimpleCursorAdapter sdp = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, db.getAllData(), str, i, 0);
            ListView lv = (ListView) findViewById(R.id.listView1);
            lv.setAdapter(sdp);
        } catch (Exception e) {
            Toast.makeText(getApplication(), e.toString(), Toast.LENGTH_LONG).show();
        }

如果要显示列表项的正确布局,请使用Custom ArrayAdapter。