我正在尝试将数据发送到SQLite
并从中获取数据到ArrayList
。为此,我使用了helper类和getter / setter方法。请建议我更正此代码。
这是助手类
public class Bday_Detail {
String name,full_date,ch;
int age;
public Bday_Detail(String name, String full_date, String ch, int age) {
this.name = name;
this.full_date = full_date;
this.ch = ch;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getFull_date() {
return full_date;
}
public void setFull_date(String full_date) {
this.full_date = full_date;
}
public String getCh() {
return ch;
}
public void setCh(String ch) {
this.ch = ch;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
这是SQLiteConnection
类
public class SQLiteConnection {
public static final String DB_NAME = "birthday";
public static final String TBL_NAME = "birthday_details";
public static final String TBL_PK = "p_id";
public static final String TBL_COL1 = "p_name";
public static final String TBL_COL2 = "p_date";
public static final String TBL_COL3 = "p_age";
public static final String TBL_COL4 = "p_ch";
SQLiteDatabase sqLiteDatabase;
Context context;
public SQLiteConnection (Context context) {
this.context = context;
}
public class connect extends SQLiteOpenHelper {
public connect(Context context) {
super(context, DB_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
String Create_BIO = "CREATE TABLE " + TBL_NAME + " (" + TBL_PK + " integer primary key autoincrement, "
+ TBL_COL1 + " text, " + TBL_COL2 + " text, " + TBL_COL3 + " integer , " + TBL_COL4 + " text )";
db.execSQL(Create_BIO);
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL("DROP TABLE IF EXISTS birthday_details"+TBL_NAME);
onCreate(db);
}
}
public void openDB(){
sqLiteDatabase = new connect(context).getWritableDatabase();
}
public void closeDB(){
if (sqLiteDatabase.isOpen() && sqLiteDatabase != null){
sqLiteDatabase.close();
}
}
public long add_details(Bday_Detail student){
ContentValues contentValues = new ContentValues();
contentValues.put(TBL_COL1,student.getName());
contentValues.put(TBL_COL2,student.getFull_date());
contentValues.put(TBL_COL3,student.getAge());
contentValues.put(TBL_COL4,student.getCh());
return sqLiteDatabase.insert(TBL_NAME,null,contentValues);
}
public Cursor get_details() {
String[] columns = new String[]{TBL_PK,TBL_COL1,TBL_COL2,TBL_COL3,TBL_COL4};
Cursor cursor = sqLiteDatabase.query(TBL_NAME, null, null, null, null, null, null, null);
String result = "";
int iRow = cursor.getColumnIndex(TBL_PK);
int iName = cursor.getColumnIndex(TBL_COL1);
int iDate = cursor.getColumnIndex(TBL_COL2);
int iAge = cursor.getColumnIndex(TBL_COL3);
int iCh = cursor.getColumnIndex(TBL_COL4);
for (cursor.moveToFirst(); !cursor.isAfterLast();cursor.moveToNext()){
result = result + cursor.getString(iRow)+ " " + cursor.getString(iName)+ " " +cursor.getString(iDate)+ " " +cursor.getString(iAge)+ " " +cursor.getString(iCh)+ "\n";
}
while (cursor.moveToNext()){
Log.e(String.valueOf(cursor.getColumnCount()), "Cursor Log");
Bday_Detail student = new Bday_Detail(cursor.getString(cursor.getColumnIndex(TBL_COL1)),
cursor.getString(cursor.getColumnIndex(TBL_COL2)),
cursor.getString(cursor.getColumnIndex(TBL_COL4)));
cursor.getString(cursor.getColumnIndex(TBL_COL3)));
obj.add(student);
return cursor;
}
cursor.moveToFirst();
}
}
以下代码是我想要获取数据的activity
public class Add_activity extends AppCompatActivity {
TextView tv_date, tv_month, tv_submit;
DatePicker pickerDate;
EditText et_name;
SQLiteConnection sqLiteConnection;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_activity);
tv_date = (TextView) findViewById(R.id.view_date);
et_name = (EditText) findViewById(R.id.et_name);
pickerDate = (DatePicker) findViewById(R.id.pickerDate);
tv_submit = (TextView) findViewById(R.id.submit);
Calendar now = Calendar.getInstance();
pickerDate.init(
1998,
now.get(Calendar.MONTH),
now.get(Calendar.DAY_OF_MONTH),
null);
final int curr_year = now.get(Calendar.YEAR);
tv_submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Calendar cal = Calendar.getInstance();
cal.set(pickerDate.getYear(),
pickerDate.getMonth(),
pickerDate.getDayOfMonth()
);
String name = et_name.getText().toString();
final int month = pickerDate.getMonth()+1;
final int year = pickerDate.getYear();
final int date = pickerDate.getDayOfMonth();
final int age = curr_year-year;
String fchar=name.substring(0,1);
final String full_date ;
String finaldate =date+"/"+month+"/"+year;
tv_date.setText("\n\n***\n"
+ "Alarm is set@ " + finaldate +name+" age:--- "+age+" first char "+fchar+"***\n");
Bday_Detail bday_detail = new Bday_Detail(name,finaldate,fchar,age);
sqLiteConnection.add_details(bday_detail);
}
});
}
}
答案 0 :(得分:0)
这是因为您首先获取数据并在
之后设置其值使用以下惯例
public void setAge(int age)
{
this.age = age;
}
public int getAge() {
return age;
}
答案 1 :(得分:0)
您没有打开数据库来编写和阅读
public long add_details(Bday_Detail student){
openDB()
ContentValues contentValues = new ContentValues();
contentValues.put(TBL_COL1,student.getName());
contentValues.put(TBL_COL2,student.getFull_date());
contentValues.put(TBL_COL3,student.getAge());
contentValues.put(TBL_COL4,student.getCh());
return sqLiteDatabase.insert(TBL_NAME,null,contentValues);
}
同样适合阅读
public void openDb(){
sqLiteDatabase = new connect(context).getReadableDatabase();
}
然后在get_detail()方法
opneDb();
// rest of the code
closeDB();
}