CURSORADAPTER / QUIZ APP错误代码?

时间:2015-05-03 04:36:50

标签: java android sqlite

我想亲切地请求你的帮助,我需要用sqlite数据库创建一个测验应用程序,其中答案进入列表视图而没有按钮。我尝试使用游标适配器,但没有工作。这是我的代码:

public class DbHelper extends SQLiteOpenHelper{

private static final String DB_NAME = "questions.sqlite";
private static final int DB_SCHEME_VERSION = 1;

public DbHelper(Context context) {
    super(context, DB_NAME, null, DB_SCHEME_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {

    db.execSQL(DataBaseManager.CREATE_TABLE);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

 }
}

DataBaseManager.class

public class DataBaseManager {
public static final String TABLE_NAME = "Questions";

public static final String CN_ID = "_id";
public static final String CN_QUESTION = "question";
public static final String CN_A1 = "a1";
public static final String CN_A2 = "a2";
public static final String CN_A3 = "a3";
public static final String CN_A4 = "a4";
public static final String CN_A5 = "a5";
public static final String CN_AC = "ac";

public static final String CREATE_TABLE = "create table " + TABLE_NAME + "("
        + CN_ID + " integer primary key autoincrement,"
        + CN_QUESTION + "text,"
        + CN_A1 + " text,"
        + CN_A2 + " text,"
        + CN_A3 + " text,"
        + CN_A4 + " text,"
        + CN_A5 + " text,"
        + CN_AC + " text);";

private DbHelper helper;
private SQLiteDatabase db;

public DataBaseManager(Context context)
{
    helper = new DbHelper(context);
    db = helper.getWritableDatabase();
}

public ContentValues generateContentValues(String question, String a1, String a2, String a3, String a4, String a5, String ac)

{
    ContentValues values = new ContentValues();
    values.put(CN_QUESTION, question);
    values.put(CN_A1, a1);
    values.put(CN_A2, a2);
    values.put(CN_A3, a3);
    values.put(CN_A4, a4);
    values.put(CN_A5, a5);
    values.put(CN_AC, ac);

    return values;
}

public void insert(String question, String a1, String a2, String a3, String a4, String a5, String ac) {


    db.insert(TABLE_NAME, null, generateContentValues(question,a1,a2,a3,a4,a5,ac));
}

public Cursor chargeCursorQuestions() {
    String[] columns = new String[]{CN_ID,CN_QUESTION,CN_A1,CN_A2,CN_A3,CN_A4,CN_A5,CN_AC};
    return db.query(TABLE_NAME,columns,null,null,null,null,null);
 }
}

MainActivity.class

public class MainActivity extends ActionBarActivity {

private DataBaseManager manager;
private Cursor cursor;
private ListView list;
private SimpleCursorAdapter adapter;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    manager = new DataBaseManager(this);
    list = (ListView) findViewById(R.id.listView);
    manager.insert("1. ¿what is my name?", "a) Jhonn", "b) Joseph", "c) Ross", "d) Camil", "e) Gunter", "D) CAMIL");

    String[] from = new String[]{manager.CN_QUESTION, manager.CN_A1, manager.CN_A2, manager.CN_A3, manager.CN_A4, manager.CN_A5, manager.CN_AC};
    int[] to = new int[] {android.R.id.text1};

    cursor = manager.chargeCursorQuestions();
    adapter = new SimpleCursorAdapter(this,android.R.layout.simple_list_item_1,cursor,from,to,0);
    list.setAdapter(adapter);

}

0 个答案:

没有答案