我有一个用标签制作的应用程序。一个选项卡使用 get_data() 方法(从另一个类调用)从数据库中获取数据。
它工作正常,但无法看到从另一个选项卡或在被指控的选项卡中所做的更改(更改为 DELETE 或 UPDATE),例如:
我使用从 db 获取的数据进入选项卡(带有列表视图),然后删除一条记录,然后转到另一个选项卡,然后返回带有列表的选项卡。我看到删除的行。如果我关闭应用程序并重新打开它,我会看到更改。
有没有办法“重新加载数据库”?
这是代码(view.class),来自选项卡的阅读器:
public class Bookmarks extends Activity{
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.bookmarks);
final Database info=new Database(this);
final Timer t = new Timer();
info.open();
String data=info.getData();
final String[] data_array = data.split(",");
info.close();
final ListView listView1 = (ListView) findViewById(R.id.list_mia);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, data_array);
listView1.setAdapter(adapter);
final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
listView1.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id){
if(data_array[position].startsWith("http://")){
Uri uri=Uri.parse((String) data_array[position]);
Intent intent=new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}else{
alertDialog.setTitle("Content");
alertDialog.setMessage(data_array[position]);
alertDialog.setIcon(R.drawable.ic_launcher);
alertDialog.show();
}
}
});
listView1.setOnItemLongClickListener(new OnItemLongClickListener(){
@Override
public boolean onItemLongClick(AdapterView<?> parent, View v, final int position, long id) {
builder.setTitle("Delete")
.setMessage("Are you sure to remove this entry?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
info.open();
info.deleteEntry(position+1);
info.close();
alertDialog.setTitle("Delete");
alertDialog.setMessage("Item deleted successfully!");
alertDialog.setIcon(R.drawable.ic_launcher);
alertDialog.show();
t.schedule(new TimerTask() {
public void run() {
alertDialog.dismiss();
t.cancel();
}
},2000);
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {}
})
.show();
return false;
}
});
}
}
和调用的方法(在 db.class 中):
public String getData() {
String[] columns=new String[]{KEY_ROWID,KEY_CONTENUTO};
Cursor c=ourDatabase.query(DATABASE_TABLE,columns,null,null,null,null,"id");
String result="";
int iRow=c.getColumnIndex(KEY_ROWID);
int iName=c.getColumnIndex(KEY_CONTENUTO);
for(c.moveToFirst();!c.isAfterLast();c.moveToNext()){
result=result+c.getString(iName)+',';
}
result=result.substring(0, result.length()-1);
return result;
}
有人能帮我吗?