来自Android中的sqlite数据库的数据没有显示出来

时间:2014-05-15 15:27:29

标签: android sqlite

我尝试查看我创建的sqlite数据库中的数据,但数据未显示在视图页面中

这是我的数据库处理程序,用于存储和检索数据

 package com.careurbaby;

 import java.text.SimpleDateFormat;
 import java.util.Date;
 import java.util.Locale;
 import android.content.ContentValues;
 import android.content.Context;
 import android.database.Cursor;
 import android.database.sqlite.SQLiteDatabase;
 import android.database.sqlite.SQLiteOpenHelper;

 public class babyDB {
       public static final String KEY_ROWID = "_id";
       public static final String KEY_NAME = "baby_name";
       public static final String KEY_BERAT = "baby_berat";
       public static final String KEY_TINGGI = "baby_tinggi";
       public static final String KEY_SEX = "baby_sex";
       public static final String KEY_TGLLAHIR = "baby_tgllahir";
       public static final String KEY_TPTLAHIR = "baby_tptlahir";
       public static final String KEY_TGLAKSES = "baby_tglakses";

       private static final String DATABASE_NAME = "Babydb";
       private static final String DATABASE_TABLE2 = "babyDataTable";
       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, DATABASE_VERSION);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub


        db.execSQL("CREATE TABLE " + DATABASE_TABLE2 + " (" +
                KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                KEY_BERAT + " DOUBLE NOT NULL, " +
                KEY_TINGGI + " DOUBLE NOT NULL, " +
                KEY_TGLAKSES + " TEXT NOT NULL);"
        );
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXIST " + DATABASE_NAME);
    }

}
public babyDB (Context c){
    ourContext = c;
}

private String getDateTime() {
    SimpleDateFormat dateFormat = new SimpleDateFormat(
            "yyyy-MM-dd HH:mm:ss", Locale.getDefault());
    Date date = new Date();
    return dateFormat.format(date);
    }

public babyDB open(){
    ourHelper = new DbHelper(ourContext);
    ourDatabase = ourHelper.getWritableDatabase();
    return null;

}

public void close(){
    ourHelper.close();
}

public long createEntry(double b, double t) {
    // TODO Auto-generated method stub
    ContentValues cv = new ContentValues();
    cv.put(KEY_BERAT, b);
    cv.put(KEY_TINGGI, t);
    cv.put(KEY_TGLAKSES, getDateTime());
    return ourDatabase.insert(DATABASE_TABLE2, null, cv);
}

public String getEntry(){
    String[] columns = new String[]{KEY_ROWID,KEY_BERAT,KEY_TINGGI,KEY_TGLAKSES};
    Cursor c = ourDatabase.query(DATABASE_TABLE2, columns, null, null, null, null, null);
    String result = "";

    int iRow = c.getColumnIndex(KEY_ROWID);
    int iBerat = c.getColumnIndex(KEY_BERAT);
    int iTinggi = c.getColumnIndex(KEY_TINGGI);
    int iTglAkses = c.getColumnIndex(KEY_TGLAKSES);

    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
        result = result + c.getString(iRow) + " " + c.getString(iBerat) + " " + c.getString(iTinggi) + " " + c.getString(iTglAkses);
    }

    return result;
   }



 }

这是我的view.java

public class dataBTView extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.databtview);
    TextView hasil = (TextView) findViewById(R.id.tvIdShow);
    babyDB info = new babyDB(this);

    info.open();
    String result = info.getEntry();

    info.close();
    hasil.setText(result);
   }
}

这是我的数据输入类

public class BabyDataInput extends Activity implements OnClickListener {

Button sqlInput,sqlView;
EditText sqlTinggi, sqlBerat;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.babydata_input);

    sqlInput = (Button) findViewById(R.id.bInputData);
    sqlView = (Button) findViewById(R.id.btView);
    sqlTinggi= (EditText) findViewById(R.id.etTinggi);
    sqlBerat = (EditText) findViewById(R.id.etBerat);

    sqlInput.setOnClickListener(this);
    sqlView.setOnClickListener(this);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.baby_data_input, menu);
    return true;
}

@Override
public void onClick(View v) {
    switch(v.getId()){
    case R.id.bInputData:
        boolean didItWork = true;
        try{
        String berat = sqlBerat.getText().toString();
        String tinggi = sqlTinggi.getText().toString();

        double b = Double.parseDouble(berat);
        double t = Double.parseDouble(tinggi);

        babyDB entry = new babyDB(this);
        entry.open();
        entry.createEntry(b,t);
        entry.close();

    }catch (Exception e){
        didItWork  = false;
        String error = e.toString();
        Dialog d = new Dialog(this);
        d.setTitle("Error!");
        TextView tv = new TextView(this);
        tv.setText(error);
        d.setContentView(tv);
        d.show();
    }finally{
        if(didItWork){
            Dialog d = new Dialog(this);
            d.setTitle("Success");
            TextView tv = new TextView(this);
            tv.setText("Your data has been input Successfully!");
            d.setContentView(tv);
            d.show();
        }
    }
        break;

    case R.id.btView:
        Intent i = new Intent("com.careurbaby.DATABTVIEW");
        startActivity(i);
        break;
    }
}

}

我不知道错误的地方,列表视图没有显示出来。

0 个答案:

没有答案