单击按钮时从sq lite中检索数据

时间:2012-03-21 09:31:05

标签: android database sqlite

我正在android中开发一个基于测验的应用程序,它从数据库中检索数据。我的问题是,当用户点击下一个按钮时,如何从数据库中检索数据,同时我希望相同的文本视图和布局是那里。

3 个答案:

答案 0 :(得分:2)

Here is a sample example code    

public class SimpleDBExActivity extends Activity {  
DataBaseHelper dbHelper;  
@Override  
public void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.main);  
    Button btnid = (Button)findViewById(R.id.btnID);  
    final TextView view = new TextView(this);  
    this.dbHelper=new DataBaseHelper(SimpleDBExActivity.this);  
   btnid.setOnClickListener(new OnClickListener() {  
    @Override  
    public void onClick(View arg0) {  
         String sms = "";  
        dbHelper.insert("12345", "some text");  
        ArrayList<String> resultsArr = dbHelper.getAllMsgs();  
        sms = resultsArr.get(0);  
        view.setText(sms);  
          setContentView(view);  
    }  
});  
}  }    

 **database helper class**     

package com.db;  
import java.util.ArrayList;  
import android.R.string;  
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;
import android.util.Log;

public class DataBaseHelper  extends SQLiteOpenHelper{

private static final String DB_NAME="SampleDB";
private static final String TABLE="SampleTABLE";
private static final int DB_VERSION=1;
private static final String COLUMN1="received_Time";
private static final String COLUMN2="col1";
private static final String COLUMN3="col2";
private  Context myContext;
private SQLiteDatabase myDataBase;
DataBaseHelper dbHelper;

public DataBaseHelper(Context context) {
    super(context, DB_NAME, null, DB_VERSION);
    myContext=context;
}

@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    String sql = "create table " + TABLE + "( "+ COLUMN1 + " integer , " + COLUMN2 + " text not null, "
            + COLUMN3 + " text not null);";
    Log.d("EventsData", "onCreate: " + sql);
    db.execSQL(sql);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub
    if (oldVersion >= newVersion)
        return;

    String sql = null;
    if (oldVersion == 1) 
        sql = "alter table " + TABLE + " add note text;";
    if (oldVersion == 2)
        sql = "";

    Log.d("EventsData", "onUpgrade  : " + sql);
    if (sql != null)
        db.execSQL(sql);
}
public void insert(String number, String message) {
    String number1 = "1234";
    String message1 = "message";
    dbHelper=new DataBaseHelper(myContext);
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(DataBaseHelper.COLUMN1, System.currentTimeMillis());
        values.put(DataBaseHelper.COLUMN2, number1);
        values.put(DataBaseHelper.COLUMN3, message1);
        System.out.println("number---"+number+"=="+message);
        db.insert(DataBaseHelper.TABLE, null, values);
        System.out.println("inserted success");
        db.close();
        if (myDataBase != null)
            myDataBase.close();

      }

  public String  getEvents() {
      String result = "";
        String select = "select * from "+TABLE+"";

        dbHelper=new DataBaseHelper(myContext);
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.rawQuery(select, null);
       if (cursor.moveToFirst()) {

            do {
                result = cursor.getInt(0)+":"+cursor.getString(1)+":"+cursor.getString(2);
            } while (cursor.moveToNext());

        }
       db.close();
        if (cursor != null && !cursor.isClosed()) {
            cursor.close();
        }
        return result;
      }
  @SuppressWarnings("null")
public ArrayList<String> getAllMsgs(){
      ArrayList<String> finalResArr = null;
      String result = "";
      String resultQuery = "select * from " + TABLE + "";
      dbHelper =  new DataBaseHelper(myContext);
      SQLiteDatabase db = dbHelper.getReadableDatabase();
      Cursor cursor = db.rawQuery(resultQuery, null);
      finalResArr = new ArrayList<String>();
      if (cursor.moveToFirst()) {

            do {
                result = cursor.getInt(0)+":"+cursor.getString(1)+":"+cursor.getString(2);
                finalResArr.add(result);
            } while (cursor.moveToNext());

        }
       db.close();
        if (cursor != null && !cursor.isClosed()) {
            cursor.close();
        }
    return finalResArr;

  }
 @Override
    public synchronized void close() {
      super.close();
        if (myDataBase != null)
            myDataBase.close();
    }
      }

layout main.xml

<Button
     android:id="@+id/btnID"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

答案 1 :(得分:1)

在onClick()函数中

你应该对该textview使用seText()方法并在setText()方法中传递参数。在执行onclick

时,必须使用变量来更新问题

答案 2 :(得分:1)