我是 android 的新手,我想将 db 中的数据获取到 textview。
但它有错误我无法纠正这个问题。
请帮我。
这是数据库类
public class blockornot1 {
String TAG;
private dbHelper1 ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabese;
public final static String DATABASE_NAME1="databasename.db";
public final String DATABASE_TABLE="numbersTable";
public final String KEY_NAME="personname";
public final String KEY_ID="id";
public final int DATABASE_VERSION =2;
public class dbHelper1 extends SQLiteOpenHelper{
public dbHelper1 (Context context) {
super (context,DATABASE_NAME1,null,DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE "+ DATABASE_TABLE+ "("+KEY_ID+ " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ KEY_NAME + "TEXT NOT NULL);");
Log.v(TAG, "creat");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
}``
public blockornot1(Context c){
ourContext = c;
}
public blockornot1 open() throws SQLException{
ourHelper= new dbHelper1(ourContext);
ourDatabese= ourHelper.getWritableDatabase();
return this;
}
public void close(){
ourHelper.close();
}
public long creatEntry(String inputtext) {
// TODO Auto-generated method stub
ContentValues cv= new ContentValues();
cv.put(KEY_NAME, inputtext);
Log.v(inputtext, "add to database");
return ourDatabese.insert(DATABASE_TABLE, null, cv);
}
public String getData() {
// TODO Auto-generated method stub
String[] columns = new String[]{ KEY_ID, KEY_NAME };
Cursor c=ourDatabese.query(DATABASE_TABLE, columns, null, null, null, null, null);
String result = "";
int iRow = c.getColumnIndex(KEY_ID);
int iName = c.getColumnIndex(KEY_NAME);
for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
result= result + c.getString(iRow) + " " + c.getString(iName) + "/n";
}
return result;
}
}
这是我的主要课程
public class Mainclass extends Activity implements OnClickListener {
EditText et;
String TAG;
public void onCreate(Bundle savedInstanceState) {
Log.v(TAG,"onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
et=(EditText)findViewById(R.id.edittext1);
Button bt=(Button)findViewById(R.id.button1);
Button bt1=(Button)findViewById(R.id.button2view);
bt.setOnClickListener(this);
bt1.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
boolean dididwork =true;
try{
String inputtext=et.getText().toString();
blockornot1 entry= new blockornot1(Mainclass.this);
entry.open();
entry.creatEntry(inputtext);
entry.close();
Log.v(TAG," okkkkkkk hastaaaaaaa");
break;}
catch (Exception e){
String error=e.toString();
Dialog d= new Dialog(this);
d.setTitle("Heack ya!");
TextView t= new TextView(this);
t.setText(error);
d.setContentView(t);
d.show();
dididwork=false;
}finally {
if(dididwork){
Dialog d= new Dialog(this);
d.setTitle("Heack ya!");
TextView t= new TextView(this);
t.setText("success");
d.setContentView(t);
d.show();
}
}
case R.id.button2view:
Intent i = new Intent("android.intent.action.SQLVIEW");
startActivity(i);
break;
}
}
}
这个类用于在 textview 中显示 DB
public class ShowDb extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.sqlview);
TextView tv=(TextView)findViewById(R.id.textViewsqlview);
blockornot1 info= new blockornot1(this);
info.open();
String data= info.getData();
info.close();
tv.setText(data);
}
}