小弟在学习的时候,遇到了一些困难,求大神解答!
这是我遇到的困难:
我写的代码:
其中一个是AddDAO接口:
package com.AA.database;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class AddDAO {
SQLiteDatabase db;
public AddDAO(Context c){
db = new DBHelper(c).getWritableDatabase();
}
public void add(ContentValues cv) {
db.insert(DBConstants2.add.TABLE_NAME, null, cv);
}
public void update(long id, ContentValues cv) {
db.update(DBConstants2.add.TABLE_NAME, cv, "_id = ?", new String[] { String.valueOf(id) });
}
public void del(long id) {
db.delete(DBConstants2.add.TABLE_NAME, "_id = ?", new String[] { String.valueOf(id) });
}
public Cursor queryAll() {
return db.rawQuery("select * from add", null);
}
public Cursor queryAllByType(int type) {
return db.rawQuery("select * from add where type = ?", new String[] { String.valueOf(type) });
}
public Cursor queryAllDetail(String start, String end) {
return db.rawQuery("select * from add where [date] >= ? and [date] <= ?", new String[] { start, end });
}
public Cursor queryForTypeAll(String start, String end, int type) {
return db.rawQuery("select sum(time) from add where [date] >= ? and [date] <= ? and type =?",
new String[] { start, end, String.valueOf(type) });
}
public Cursor queryAllDay(String date) {
return db.rawQuery("select * from add where [date] = ? ", new String[] { date });
}
public Cursor queryAllDayByType(String date, int type) {
return db.rawQuery("select * from add where [date] = ? and type = ?",
new String[] { date, String.valueOf(type) });
}
public Cursor queryAllForKind(String start, String end, int type) {
return db.rawQuery("select * from add where [date] >= ? and [date] <= ? and type =? group by kind",
new String[] { start, end, String.valueOf(type) });
}
}
还有一个是OtherdataDAO2:
package com.AA.database;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class OtherdataDAO2 {
SQLiteDatabase db;
public OtherdataDAO2(Context c) {
db = new DBHelper(c).getWritableDatabase();
}
public void add(ContentValues cv) {
db.insert(DBConstants2.ActionList.TABLE_NAME, null, cv);
}
public void update(long id, ContentValues cv) {
db.update(DBConstants2.ActionList.TABLE_NAME, cv, "_id = ?", new String[]{String.valueOf(id)});
}
public void del(long id) {
db.delete(DBConstants2.ActionList.TABLE_NAME, "_id = ?", new String[]{String.valueOf(id)});
}
public Cursor queryAll(int type) {
return db.rawQuery("select * from ActionList where type = ?", new String[]{String.valueOf(type)});
}
public Cursor queryById(long id) {
return db.rawQuery("select * from ActionList where _id = ?", new String[]{String.valueOf(id)});
}
public String queryForFirst(int type) {
Cursor cursor = db.rawQuery("select * from ActionList where type = ? limit 0,1", new String[]{String.valueOf(type)});
if(cursor.moveToNext()) {
return cursor.getString(cursor.getColumnIndex(DBConstants2.ActionList.NAME));
}
return "其他";
}
}
然后RecordActivity2:
package com.AA.record;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import com.AA.database.ActionListDAO;
import com.AA.database.AddDAO;
import com.AA.database.DBConstants2;
import com.AA.database.OtherdataDAO2;
import com.AA.f1.R;
import com.AA.otherdata.OtherdataListActivity2;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.content.ContentValues;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class RecordActivity2 extends Activity {
TextView tv03,tv05,tv07,tv09,tv11,tv13;
EditText et01,et02;
ActionListDAO aldao;
AddDAO adao;
OtherdataDAO2 oddao2;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_record);
init();
initData();
}
//初始化函数
private void init() {
tv03 = (TextView) findViewById(R.id.tv03);//运动类型选项
tv05 = (TextView) findViewById(R.id.tv05);//锻炼部位选项
tv07 = (TextView) findViewById(R.id.tv07);//锻炼内容选项
tv09= (TextView) findViewById(R.id.tv09);//项目次数选项
tv11 = (TextView) findViewById(R.id.tv11);//力量级别选项
tv13 = (TextView) findViewById(R.id.tv13);//锻炼日期选项
et01 = (EditText) findViewById(R.id.et01);//锻炼时长
et02 = (EditText) findViewById(R.id.et02);//备注栏
adao=new AddDAO(this);//用于输入数据
aldao=new ActionListDAO(this);//用于选择动作
oddao2 = new OtherdataDAO2(this);//用于添加至明细
}
@SuppressLint("SimpleDateFormat")
private void initData() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
tv09.setText(sdf.format(new Date()));
tv03.setText(aldao.queryForFirst(DBConstants2.ActionList.TYPE_SportType));
tv05.setText(aldao.queryForFirst(DBConstants2.ActionList.TYPE_FitnessPart));
tv07.setText(aldao.queryForFirst(DBConstants2.ActionList.TYPE_FitnessContent));
tv11.setText(aldao.queryForFirst(DBConstants2.ActionList.TYPE_StrengthLevel));
tv13.setText(aldao.queryForFirst(DBConstants2.ActionList.TYPE_ProjectNumber));
}
public void toDate(View v) {
Calendar c = Calendar.getInstance();
DatePickerDialog dialog = new DatePickerDialog(this, new OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
tv09.setText(year + "-" + (monthOfYear + 1) + "-" + dayOfMonth);
}
}, c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH));
dialog.show();
}
public void back(View v) {
finish();
}
public void add(View v) {
ContentValues cv = new ContentValues();
cv.put(DBConstants2.add.TIME, et01.getText().toString());
cv.put(DBConstants2.add.SportType, tv03.getText().toString());
cv.put(DBConstants2.add.FitnessPart, tv05.getText().toString());
cv.put(DBConstants2.add.FitnessContent, tv07.getText().toString());
cv.put(DBConstants2.add.DATE, tv09.getText().toString());
cv.put(DBConstants2.add.StrengthLevel, tv11.getText().toString());
cv.put(DBConstants2.add.ProjectNumber, tv13.getText().toString());
cv.put(DBConstants2.add.MARK, et02.getText().toString());
//cv.put(DBConstants2.inout.MONEY, et01.getText().toString());
//cv.put(DBConstants2.inout.KIND, tv03.getText().toString());
//cv.put(DBConstants2.inout.ACCOUNT, tv05.getText().toString());
//cv.put(DBConstants2.inout.SHOP, tv07.getText().toString());
///cv.put(DBConstants2.inout.DATE, tv09.getText().toString());
//cv.put(DBConstants2.inout.MEMBER, tv11.getText().toString());
//cv.put(DBConstants2.inout.PROJECT, tv13.getText().toString());
//cv.put(DBConstants2.inout.MARK, et02.getText().toString());
//cv.put(DBConstants.inout.TYPE, DBConstants.inout.TYPE_IN);
adao.add(cv);
Toast.makeText(this, "记录成功!!!", Toast.LENGTH_SHORT).show();
finish();
}
//从Otherdatalist里获取动作列表
public void toOther(View v) {
Intent i = new Intent(this, OtherdataListActivity2.class);
i.putExtra("rs", true);
int requestCode = 0;
switch (v.getId()) {
case R.id.tv03:
requestCode = 3;
i.putExtra("type", DBConstants2.ActionList.TYPE_SportType);
break;
case R.id.tv05:
requestCode = 5;
i.putExtra("type", DBConstants2.ActionList.TYPE_FitnessPart);
break;
case R.id.tv07:
requestCode = 7;
i.putExtra("type", DBConstants2.ActionList.TYPE_FitnessContent);
break;
case R.id.tv11:
requestCode = 11;
i.putExtra("type", DBConstants2.ActionList.TYPE_StrengthLevel);
break;
case R.id.tv13:
requestCode = 13;
i.putExtra("type", DBConstants2.ActionList.TYPE_ProjectNumber);
break;
}
startActivityForResult(i, requestCode);
}
//进入OtherdataList里可以进行选择动作
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 3 && resultCode == RESULT_OK) {
tv03.setText(data.getStringExtra("value"));
}else if (requestCode == 5 &&resultCode == RESULT_OK){
tv05.setText(data.getStringExtra("value"));
}else if (requestCode == 7 && resultCode == RESULT_OK) {
tv07.setText(data.getStringExtra("value"));
} else if (requestCode == 11 && resultCode == RESULT_OK) {
tv11.setText(data.getStringExtra("value"));
} else if (requestCode == 13 && resultCode == RESULT_OK) {
tv13.setText(data.getStringExtra("value"));
}
}
}
求大神帮忙解答下!谢谢
db = new DBHelper(c).getWritableDatabase();
可以去DBHelper()类里看一下初始化数据库的代码,一般在onCreate()方法里,是不是没有建表?
或者建表语言写了,但是忘记调用db.execSQL(String 建表语句)方法。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。