求大家帮忙,本人在编写一个数据库App中,按照以前android的思路,结合harmony官方文档对sqlite数据库进行增删改查操作时遇到了数据库不同步的问题,具体先看图,在上源代码和项目结构:
项目有五个Ability页面,分别是:MainAbility(主页面)、ZengJiaAbility(添加页面)、XiuGaiAbility(修改页面)、ShanChuAbility(删除页面)、ChaXunAbility(查询页面),如下图:
存在问题:数据库初始化后,在添加页面中添加数据后,在ZengJiaAbility页面能够查询到数据库数据,但是在ChaXunAbility页面中却查不到数据,如下图:
下面上项目目录图和源代码,希望大家帮忙指正!谢谢大家了,都琢磨了快一周了。
以下开始上源代码:
一、MainAbility 和MainAbilitySlice
public class MainAbility extends Ability {
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setMainRoute(MainAbilitySlice.class.getName());
}
}
public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener {
private Button cx,xg,tj,sc;
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
cx = (Button)findComponentById(ResourceTable.Id_chuxun_button);
xg = (Button)findComponentById(ResourceTable.Id_xiugai_button);
tj = (Button)findComponentById(ResourceTable.Id_tianjia_button);
sc = (Button)findComponentById(ResourceTable.Id_shanchu_button);
cx.setClickedListener(this);
xg.setClickedListener(this);
tj.setClickedListener(this);
sc.setClickedListener(this);
}
@Override
public void onClick(Component component) {
switch(component.getId()){
case ResourceTable.Id_chuxun_button:
HiLog.info(DBHelper.label,"查询数据页面打开成功");
Intent intent = new Intent();
Operation operation = new Intent.OperationBuilder()
.withBundleName("com.riton.jackie.shifu")
.withAbilityName(ChaXunAbility.class)
.build();
intent.setOperation(operation);
startAbility(intent);
new ToastDialog(getContext())
.setText("查询数据成功")
.show();
break;
case ResourceTable.Id_xiugai_button:
HiLog.info(DBHelper.label,"修改数据页面打开成功");
Intent intent1 = new Intent();
Operation operation1 = new Intent.OperationBuilder()
.withBundleName("com.riton.jackie.shifu")
.withAbilityName(XiuGaiAbility.class)
.build();
intent1.setParam("id",1);
intent1.setOperation(operation1);
startAbility(intent1);
new ToastDialog(getContext())
.setText("修改数据成功")
.show();
break;
case ResourceTable.Id_tianjia_button:
HiLog.info(DBHelper.label,"添加数据页面打开成功");
Intent intent0 = new Intent();
Operation operation0 = new Intent.OperationBuilder()
.withBundleName("com.riton.jackie.shifu")
.withAbilityName(ZengJiaAbility.class)
.build();
intent0.setOperation(operation0);
startAbility(intent0);
new ToastDialog(getContext())
.setText("添加数据成功")
.show();
break;
case ResourceTable.Id_shanchu_button:
HiLog.info(DBHelper.label,"删除数据页面打开成功");
Intent intent2 = new Intent();
Operation operation2 = new Intent.OperationBuilder()
.withBundleName("com.riton.jackie.shifu")
.withAbilityName(ShanChuAbility.class)
.build();
intent2.setParam("id",1);
intent2.setOperation(operation2);
startAbility(intent2);
new ToastDialog(getContext())
.setText("删除数据成功")
.show();
break;
}
}
@Override
public void onActive() {
super.onActive();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
}
二、DingDan实体对象和DingDanServices平台业务逻辑层
public class DingDan {
public String ddbh,khxm;
public int id,khdh,ptid;
public int getDdid(){
return id;
}
public void setDdid(int id){
this.id = id;
}
public int getPtid(){
return ptid;
}
public void setPtid(int ptid){
this.ptid = ptid;
}
public String getDdbh(){
return ddbh;
}
public void setDdbh(String ddbh){
this.ddbh = ddbh;
}
public String getKhxm(){return khxm;}
public void setKhxm(String khxm){
this.khxm = khxm;
}
public DingDan(){
super();
}
}
复制
public class DingDanServices {
private RdbStore store;
private DatabaseHelper helper ;
public DingDanServices(Context context) {
helper = new DatabaseHelper(context);
StoreConfig config = StoreConfig.newDefaultConfig("andd.db");
RdbOpenCallback callback = new RdbOpenCallback() {
@Override
public void onCreate(RdbStore rdbStore) {
String sql = "CREATE TABLE IF NOT EXISTS DingDan (" +
"id INTEGER PRIMARY KEY AUTOINCREMENT," +
"ptid INTEGER," +
"ddbh VARCHAR (20)," +
"khxm VARCHAR (20)" +
")";
rdbStore.executeSql(sql);
//store.executeSql("create table if not exists PingTai (id INTEGER PRIMARY KEY AUTOINCREMENT,ptmc text,ptys text)");
}
@Override
public void onUpgrade(RdbStore rdbStore, int i, int i1) {
}
};
store = helper.getRdbStore(config,1,callback,null);
}
public boolean insert(DingDan dd){
ValuesBucket vb = new ValuesBucket();
vb.putInteger("ptid",dd.getPtid());
vb.putString("ddbh",dd.getDdbh());
vb.putString("khxm",dd.getKhxm());
boolean isinsert = false;
if(store.insert("DingDan",vb)>0){
isinsert = true;
}
return isinsert;
}
public boolean update(DingDan dd){
ValuesBucket vb = new ValuesBucket();
vb.putInteger("ptid",dd.getPtid());
vb.putString("ddbh",dd.getDdbh());
vb.putString("khxm",dd.getKhxm());
boolean isupdate = false;
RdbPredicates predicates = new RdbPredicates("DingDan");
predicates.equalTo("id",dd.getDdid());
if(store.update(vb,predicates)>0){
isupdate = true;
}
return isupdate;
}
public List<DingDan> getDingDans(){
List<DingDan> dds = new ArrayList<>();
ResultSet rs = store.querySql("select id,ddbh,khxm from DingDan",null);
while (rs.goToNextRow()){
DingDan dd = new DingDan();
dd.setDdid(rs.getInt(rs.getColumnIndexForName("id")));
dd.setDdbh(rs.getString(rs.getColumnIndexForName("ddbh")));
dd.setKhxm(rs.getString(rs.getColumnIndexForName("khxm")));
dds.add(dd);
}
return dds;
}
public boolean delete(int id){
boolean isdelete = false;
RdbPredicates predicates = new RdbPredicates("DingDan");
predicates.equalTo("id",id);
if(store.delete(predicates)>0){
isdelete = true;
}
return isdelete;
}
}
三、ZengJiaAbility和 ZengJiaAbilitySlice
public class ZengJiaAbility extends Ability {
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setMainRoute(ZengJiaAbilitySlice.class.getName());
}
}
public class ZengJiaAbilitySlice extends AbilitySlice {
private Button zjbutton;
private DingDanServices ddservies;
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_zengjia);
ddservies = new DingDanServices(getContext());
zjbutton = (Button) findComponentById(ResourceTable.Id_zengjia_zjbutton);
zjbutton.setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
DingDan dd = new DingDan();
dd.setDdbh("0001");
dd.setKhxm("申明");
if (ddservies.insert(dd)) {
HiLog.info(DBHelper.label, "插入数据成功");
List<DingDan> dds = new ArrayList<>();
dds = ddservies.getDingDans();
for(DingDan dd1 : dds){
HiLog.info(DBHelper.label,""+dd1.getDdid()+"、"+dd1.getDdbh()+"==="+dd1.getKhxm());
}
} else {
HiLog.info(DBHelper.label, "插入数据失败");
}
}
});
}
@Override
public void onActive() {
super.onActive();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
}
四、ChaXunAbility和ChaXunAbilitySlice
public class ChaXunAbility extends Ability {
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setMainRoute(ChaXunAbilitySlice.class.getName());
}
}
public class ChaXunAbilitySlice extends AbilitySlice {
private ListContainer list;
private DingDanServices ddservices;
private List<DingDan> ddlist;
private ListItemProvider listItemProvider;
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_chaxun);
list = (ListContainer)findComponentById(ResourceTable.Id_chaxun_list);
ddlist = new ArrayList<>();
ddservices = new DingDanServices(this);
ddlist = ddservices.getDingDans();
if(ddlist.size()>0){
listItemProvider = new ListItemProvider(ChaXunAbilitySlice.this,ddlist);
list.setItemProvider(listItemProvider);
}else{
HiLog.info(DBHelper.label,"查不到数据");
}
}
@Override
public void onActive() {
super.onActive();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
}
五、其他代码
public class DBHelper {
public static final HiLogLabel label = new HiLogLabel(HiLog.LOG_APP,0x00201,"MyTag");
}
public class ListItemProvider extends BaseItemProvider {
private List<DingDan> ddlist;
private AbilitySlice slice;
public ListItemProvider(AbilitySlice slice,List<DingDan> list) {
this.ddlist = list;
this.slice = slice;
}
@Override
public int getCount() {
return ddlist.size();
}
@Override
public Object getItem(int i) {
return ddlist.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
Component cpt = component;
SettingHolder holder;
DingDan pt = ddlist.get(i);
if (component == null) {
cpt = LayoutScatter.getInstance(slice).parse(ResourceTable.Layout_dingdanitem1, null, false);
holder = new SettingHolder(cpt);
cpt.setTag(holder);
} else {
cpt = component;
holder = (SettingHolder) cpt.getTag();
}
holder.ddid.setText(pt.getDdid()+"");
holder.ddbh.setText(pt.getDdbh());
holder.khxm.setText(pt.getKhxm());
return cpt;
}
// 用于保存列表项中的子组件信息
public class SettingHolder {
Text ddbh,khxm;
Text ddid;
SettingHolder(Component component) {
ddid = (Text) component.findComponentById(ResourceTable.Id_dingdanitem_ddid);
ddbh = (Text) component.findComponentById(ResourceTable.Id_dingdanitem_ddbh);
khxm = (Text) component.findComponentById(ResourceTable.Id_dingdanitem_khxm);
}
}
}
大家有没有找出问题所在,希望大家帮帮忙,帮我快点解决这个问题,现在项目就卡数据存储在这里进行不下去了。其他的所有功能都实现了。
再次感谢大家!