请帮助,对于测验应用程序,我的问题是我无法将显示数据从我的数据库随机随机文本视图。这是我的代码:
我该如何解决,谢谢:)
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";
}
}
感谢您的帮助!!
答案 0 :(得分:1)
这是因为
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);
并按下按钮点击以下工作
mySQLiteAdapter.close();// close previously opened
public void onClick(View v)
{
mySQLiteAdapter.openToRead();
textview.setText(mySQLiteAdapter.getRandomQuestion());
mySQLiteAdapter.close();
}