嗨,我正在创建一个存储计数器金额的数据库。我有一个保存按钮来保存计数器中的值,还有一个视图按钮来查看这些值的表格。但是当我按下查看时屏幕是空白的
代码如下 主要活动类
public class MainActivity extends Activity implements OnClickListener {
int counter;
Button add;
Button sub;
Button save;
Button view;
TextView display;
private SharedPreferences prefs;
private String prefName = "MyPref";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
counter = 0;
add = (Button) findViewById(R.id.button1);
sub = (Button) findViewById(R.id.button2);
save = (Button) findViewById(R.id.button3);
view = (Button) findViewById(R.id.button4);
display = (TextView) findViewById(R.id.tvDisplay);
save.setOnClickListener(this);
view.setOnClickListener(this);
add.setOnClickListener(this);
sub.setOnClickListener(this);
}
public void onClick(View arg0){
switch(arg0.getId()){
case R.id.button1:
counter++;
display.setText("Counter " + counter );
break;
case R.id.button2:
counter--;
display.setText("Counter " + counter );
break;
case R.id.button3:
boolean diditwork = true;
try{
StringBuilder sb = new StringBuilder();
sb.append("");
sb.append(counter);
String count = sb.toString();
counterdata entry = new counterdata(this);
entry.open();
entry.createEntry(count);
entry.close();
}catch(Exception e){
diditwork = false;
String error = e.toString();
Dialog d = new Dialog(this);
d.setTitle("Nope");
TextView tv = new TextView(this);
tv.setText(error);
d.setContentView(tv);
d.show();
}finally {
if(diditwork){
Dialog d = new Dialog(this);
d.setTitle("Hell YEa");
TextView tv = new TextView(this);
tv.setText("Sucess");
d.setContentView(tv);
d.show();
}
}
break;
case R.id.button4:
Intent i = new Intent(MainActivity.this, SqlView.class);
startActivity(i);
break;
}
}
SQL视图类
public class SqlView extends Activity {
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.sqlview);
TextView tv = (TextView) findViewById(R.id.databaseview);
counterdata info = new counterdata(this);
info.open();
String data = info.getdata();
info.close();
tv.setText(data);
}
}
数据库表的XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableRow>
</TableRow>
</TableLayout>
<TextView
android:id="@+id/databaseview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="get database here" />
</LinearLayout>
处理数据库的类
public class counterdata {
public static final String KEY_ROWID = "_id";
public static final String KEY_Amount = "_amount";
private static final String DATABASE_NAME = "CounterDb";
private static final String DATABASE_TABLE = "CounterTable";
private static final int DATABASE_VERSION = 1;
private DbHelper ourhelper;
private final Context Ourcontext;
private SQLiteDatabase ourDatabase;
private static class DbHelper extends SQLiteOpenHelper {
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ROWID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_Amount
+ " INT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXITS " + DATABASE_NAME);
onCreate(db);
}
}
public counterdata(Context c) {
Ourcontext = c;
}
public counterdata open() throws SQLException {
ourhelper = new DbHelper(Ourcontext);
ourDatabase = ourhelper.getWritableDatabase();
return this;
}
public void close() {
ourhelper.close();
}
public void createEntry(String count) {
// TODO Auto-generated method stub
ContentValues cv = new ContentValues();
cv.put(KEY_Amount, count);
ourDatabase.insert(DATABASE_TABLE, null, cv);
}
public String getdata() {
// TODO Auto-generated method stub
String[] columns = new String[]{KEY_ROWID, KEY_Amount};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
String result = " ";
int irow = c.getColumnIndex(KEY_ROWID);
int iamount = c.getColumnIndex(KEY_Amount);
for(c.moveToFirst();!c.moveToLast();c.moveToNext())
{
result = result + c.getString(irow) + " " + c.getString(iamount) + "\n";
}
return result;
}
}
任何想法?
答案 0 :(得分:0)
我不相信你有一张表开头,因为表的语法不正确你忘了“;”在末尾。它应该如下:
db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ROWID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_Amount
+ " INT);"); // here you forget the first ';'
你确定你插的是正确的吗?!