【问题标题】:random display of data in database that display in textview随机显示在文本视图中显示的数据库中的数据
【发布时间】:2013-12-13 08:27:51
【问题描述】:

请帮助,对于测验应用程序,我的问题是我无法将显示数据从我的数据库随机显示到文本视图。这是我的代码:

如何解决,谢谢:)

      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 适配器

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";
}

}

提前感谢您的帮助!

【问题讨论】:

  • 问题是显示所有文本(问题),我希望每次单击按钮时都会出现下一个问题并以随机顺序显示。谢谢。
  • 你能解决它吗?谢谢:)

标签: android database-connection


【解决方案1】:

这是因为

TextView.setText(contentRead);

这将使所有日期进入文本视图。请删除该行

此外,您只想显示随机问题。所以这些行是不必要的。

/*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();
}

【讨论】:

  • 现在发生了什么?是否会显示一个问题?
  • 它显示一个问题:) 但是当我点击按钮时应用程序停止:(
  • 它只显示一个问题,每次我点击按钮应用程序都会停止:'(
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-01-26
  • 1970-01-01
  • 2021-07-05
  • 2015-02-25
  • 2013-02-15
  • 1970-01-01
  • 2014-06-02
相关资源
最近更新 更多