请帮助,对于Quiz应用程序,我的问题是我无法将数据库中的显示数据随机显示到文本视图。这是我的代码:
我该如何解决,谢谢:)
private SQLiteAdapter mySQLiteAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.hahaha);
mySQLiteAdapter = new SQLiteAdapter(this);
mySQLiteAdapter.openToWrite();
mySQLiteAdapter.deleteAll();
mySQLiteAdapter.insert("Highly weathered soil of the tropics belong to the Soil Order");
mySQLiteAdapter.insert("The best soil texture class for growing irrigated lowland rice is ");
mySQLiteAdapter.insert("Which of the following crops can tolerate very strongly acidic soil conditions");
mySQLiteAdapter.insert("Acid sulfate soils are often found in areas where the parent material of the soil is derived from ");
mySQLiteAdapter.insert("Soil microorganisms which are capable of deriving energy for life processes only from the decomposition of organic compounds and incapable of using inorganic compounds as sole sources of energy are known as");
final TextView TextView = (TextView)findViewById(R.id.textView1);
Button btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener(){
public void onClick(View v)
{
TextView.setText(mySQLiteAdapter.getRandomQuestion());
}
});
mySQLiteAdapter.close();
/*
* Open the same SQLite database
* and read all it's content.
*/
mySQLiteAdapter = new SQLiteAdapter(this);
mySQLiteAdapter.openToRead();
String contentRead = mySQLiteAdapter.queueAll();
mySQLiteAdapter.close();
TextView.setText(contentRead);
}
对于SQLITE ADAPTER
public class SQLiteAdapter {
public static final String MYDATABASE_NAME = "MY_DATABASE";
public static final String MYDATABASE_TABLE = "MY_TABLE";
public static final int MYDATABASE_VERSION = 1;
public static final String KEY_CONTENT = "Content";
//create table MY_DATABASE (ID integer primary key, Content text not null);
private static final String SCRIPT_CREATE_DATABASE =
"create table " + MYDATABASE_TABLE + " ("
+ KEY_CONTENT + " text not null);";
private SQLiteHelper sqLiteHelper;
private SQLiteDatabase sqLiteDatabase;
private Context context;
public SQLiteAdapter(Context c){
context = c;
}
public SQLiteAdapter openToRead() throws android.database.SQLException {
sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
sqLiteDatabase = sqLiteHelper.getReadableDatabase();
return this;
}
public SQLiteAdapter openToWrite() throws android.database.SQLException {
sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
sqLiteDatabase = sqLiteHelper.getWritableDatabase();
return this;
}
public void close(){
sqLiteHelper.close();
}
public long insert(String content){
ContentValues contentValues = new ContentValues();
contentValues.put(KEY_CONTENT, content);
return sqLiteDatabase.insert(MYDATABASE_TABLE, null, contentValues);
}
public int deleteAll(){
return sqLiteDatabase.delete(MYDATABASE_TABLE, null, null);
}
public String queueAll(){
String[] columns = new String[]{KEY_CONTENT};
Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, columns,
null, null, null, null, null);
String result = "";
int index_CONTENT = cursor.getColumnIndex(KEY_CONTENT);
for(cursor.moveToFirst(); !(cursor.isAfterLast()); cursor.moveToNext()){
result = result + cursor.getString(index_CONTENT) + "\n";
}
return result;
}
public class SQLiteHelper extends SQLiteOpenHelper {
public SQLiteHelper(Context context, String name,
CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(SCRIPT_CREATE_DATABASE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
public CharSequence getRandomQuestion() {
Cursor c = sqLiteDatabase.query(MYDATABASE_TABLE + " ORDER BY RANDOM() LIMIT 1",
new String[] { KEY_CONTENT }, null, null, null, null, null);
if(c.moveToFirst())
return c.getString(c.getColumnIndex(KEY_CONTENT));
else
return "nothing";
}
}
我在这里先向您的帮助表示感谢!!
最佳答案
这是因为
TextView.setText(contentRead);
这将使所有日期进入textview。请删除行
另外,您只想显示随机问题。因此,这些行不是必需的。
/*mySQLiteAdapter = new SQLiteAdapter(this);
mySQLiteAdapter.openToRead();
String contentRead = mySQLiteAdapter.queueAll();
mySQLiteAdapter.close();
TextView.setText(contentRead);*/
我强烈建议您更改您的TextView名称。因为TextView是android中的保留字。
final TextView TextView = (TextView)findViewById(R.id.textView1);
请使用其他名称作为
final TextView textview = (TextView)findViewById(R.id.textView1);
并使您的Button的onclick如下所示
mySQLiteAdapter.close();// close previously opened
public void onClick(View v)
{
mySQLiteAdapter.openToRead();
textview.setText(mySQLiteAdapter.getRandomQuestion());
mySQLiteAdapter.close();
}