0

我有一个用标签制作的应用程序。一个选项卡使用 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;
}

有人能帮我吗?

4

2 回答 2

1

you should create a method like given below, When you delete or update info in database. call this method within the same activity it will fetch the data and reload list.

   setData() {

    info.open();
    String data=info.getData();
    final String[] data_array = data.split(",");
    info.close();
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, data_array);
    listView1.setAdapter(adapter);

        }

P.S: above code is to give you just an hint that you can basically reload list on deletion or updation of db. it is not a exact solution.

于 2012-07-09T14:45:36.040 回答
0

请问您为什么要进行所有这些额外的处理?您正在查询游标,连接结果,返回连接的结果,然后再次拆分它们。这对处理器来说有点密集。

为什么不直接将光标与 SimpleCursorAdapter 一起使用?然后您只需使用requery()刷新列表。

您可以通过以下更改来做到这一点:

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(); 
    Cursor data=info.getData(); 
    final ListView listView1 = (ListView) findViewById(R.id.list_mia); 
    String[] from = new String[] { db.KEY_CONTENUTO };
    int[] to = new int[] { android.R.id.text1 };
    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,android.R.layout.simple_list_item_1, data, from, to); 
    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(view.getText().toString().startsWith("http://")){ 
                Uri uri=Uri.parse(view.getText().toString()); 
                Intent intent=new Intent(Intent.ACTION_VIEW, uri); 
                startActivity(intent); 
            }else{ 
                alertDialog.setTitle("Content"); 
                alertDialog.setMessage(view.getText().toString()); 
                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) {  
        long rowId = 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.deleteEntry(rowId); 
                    data.requery();
                    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; 
    } 
}); 

} }

public Cursor getData() { 
    String[] columns=new String[]{KEY_ROWID,KEY_CONTENUTO}; 
    Cursor c=ourDatabase.query(DATABASE_TABLE,columns,null,null,null,null,"id"); 
    return c; 
} 
于 2012-07-09T15:16:16.193 回答