在查看修改后的And​​r​​oid SQLite的刷新数据 [英] Android SQLite Refreshing data on view after modification

查看:171
本文介绍了在查看修改后的And​​r​​oid SQLite的刷新数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个活动,的GridView ,显示从我的数据库中的数据。当用户选择一个帐户将加载另一个活动的AccountManager 这将包含从<$所选项目的所有细节C $ C>的GridView 键,允许用户从那里进行管理(修改,删除和诸如此类的东西)。

问题:

当用户修改帐户发不显示修改的的AccountManager ,而是仍然显示的初始数据从的GridView 。我的猜测是,那是因为从 GridView控件连接到的AccountManager 数据不直接连接到数据库,而我用中间人对于它,以便数据的方式静态和分开的数据库中。

问:

有没有一种方法来更新的AccountManager 中的数据,而无需改变我的code。如果是的话我怎么做到这一点,如果没有什么方法可以让我得到的AccountManager 的数据链接到我的数据库中的数据,因此任何的修改将反映在的AccountManager 修改后即可。

我如何通过从的GridView 数据的AccountManager

  grid.setOnItemClickListener(新OnItemClickListener(){

 @覆盖
     公共无效onItemClick(适配器视图&LT;&GT;母公司,视图V,INT位置,
                                                                长ID){
     尝试 {
    SQLiteCursor CR =(SQLiteCursor)parent.getItemAtPosition(位置);
    字符串名称= cr.getString(cr.getColumnIndex(DatabaseHelper.colName));
    INT金额= cr.getInt(cr.getColumnIndex(DatabaseHelper.colAmount));
    串目的= cr.getString(cr.getColumnIndex(DatabaseHelper.colPurpose));
    串条款= cr.getString(cr.getColumnIndex(DatabaseHelper.colTermsClass));
    字符串状态= cr.getString(cr.getColumnIndex(DatabaseHelper.colStatClass));
    字符串日期= cr.getString(cr.getColumnIndex(DatabaseHelper.colDate));
    串editdate = cr.getString(cr.getColumnIndex(DatabaseHelper.colEditDate));
    账户ACC =新帐户(名称,数量,用途,db.GetTermsID(条款),日期,editdate,db.GetStatID(​​状态));
    acc.SetID((int)的内径);

意图myIntent =新的意图(AccountManager.this,AccountDetails.class);
myIntent.putExtra(AccountObject,ACC);
startActivityForResult(myIntent,0);

  }
 }
}
 

我如何检索的AccountManager

的GridView 数据

 决算命中率=(账户)getIntent()getSerializableExtra(AccountObject)。
 

解决方案

您可以使用CursorLoader的 http://developer.android.com/reference/android/content/CursorLoader.html

或ContentObserver http://developer.android.com/reference/android /database/ContentObserver.html

通报时的数据在ContentProvider(的http被改变该URI:// developer.android.com/reference/android/content/ContentProvider.html ),将火onLoadFinished或CursorLoader分别的onChange和ContentObserver

I have an activity, GridView, that displays the data from my database. When user selects an Account it will load up another activity AccountManager which will contain all details of the selected item from GridView and allow the user to manage it from there (Modify, Delete and whatnot).

PROBLEM:

When user modifies the Account the modifications made do not show up in AccountManager and instead still shows the initial data from GridView. My guess is it's because the data linked to AccountManager from GridView isn't directly linked to the database, rather I use a middle-man for it so the data is in a way static and separate from the database.

QUESTION:

Is there a way to update the data on AccountManager without having to change my code. If so how do I do this, if not what method would allow me to get the data on AccountManager to be linked to the data in my database so any modifications will reflect on AccountManager immediately after modification.

How I pass data from GridView to AccountManager

grid.setOnItemClickListener(new OnItemClickListener() {

 @Override
     public void onItemClick(AdapterView<?> parent, View v, int position,
                                                                long id) {
     try {
    SQLiteCursor cr = (SQLiteCursor) parent.getItemAtPosition(position);
    String name = cr.getString(cr.getColumnIndex(DatabaseHelper.colName));
    int amount = cr.getInt(cr.getColumnIndex(DatabaseHelper.colAmount));
    String purpose = cr.getString(cr.getColumnIndex(DatabaseHelper.colPurpose));
    String Terms = cr.getString(cr.getColumnIndex(DatabaseHelper.colTermsClass));
    String Status = cr.getString(cr.getColumnIndex(DatabaseHelper.colStatClass));
    String date = cr.getString(cr.getColumnIndex(DatabaseHelper.colDate));
    String editdate = cr.getString(cr.getColumnIndex(DatabaseHelper.colEditDate));
    Account acc = new Account(name, amount, purpose,     db.GetTermsID(Terms),date,editdate,db.GetStatID(Status));
    acc.SetID((int) id);

Intent myIntent = new Intent(AccountManager.this, AccountDetails.class);
myIntent.putExtra("AccountObject", acc);
startActivityForResult(myIntent, 0);

  }
 }
}

How I retrieve the data from GridView in AccountManager

final Account Acc = (Account) getIntent().getSerializableExtra("AccountObject");

解决方案

You can use a CursorLoader http://developer.android.com/reference/android/content/CursorLoader.html

or a ContentObserver http://developer.android.com/reference/android/database/ContentObserver.html

Notifying the URI when the data are changed in the ContentProvider ( http://developer.android.com/reference/android/content/ContentProvider.html ), will fire onLoadFinished or onChange of the CursorLoader and ContentObserver respectively

这篇关于在查看修改后的And​​r​​oid SQLite的刷新数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆