首页 > 代码库 > Android开始之 数据存储CursorAdapter游标适配器&Loader和AsyncLoader的使用

Android开始之 数据存储CursorAdapter游标适配器&Loader和AsyncLoader的使用

技术分享

-------------PersonService-----------------------

1 public interface PersonService {
2     public Cursor listPersonCursor();
3     
4     public boolean addPerson(ContentValues values);
5 
6 }

-----------------------DBHelper-------------------------------------------------------

 1 public class DBHelper extends SQLiteOpenHelper {
 2 
 3     private static final int VERSION = 1;// 数据库版本号
 4     private static final String NAME = "MYDB.DB";// 数据库名称
 5 
 6     public DBHelper(Context context) {
 7         super(context, NAME, null, VERSION);
 8         // TODO Auto-generated constructor stub
 9     }
10 
11     @Override
12     public void onCreate(SQLiteDatabase db) {
13         // TODO Auto-generated method stub
14         //!!用SQLite数据库完成UI界面的显示,表中必须包含:_id
15 
16         String sql = "create table person(_id integer primary key autoincrement,name varchar(64),address varchar(64))";
17         db.execSQL(sql);
18     }
19 
20     @Override
21     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
22         // TODO Auto-generated method stub
23 
24     }
25 
26 }

---------------------------DBManger----------------------------------------------

 1 public class DBManger {
 2     private DBHelper helper;
 3     private SQLiteDatabase database;
 4     public DBManger(Context context) {
 5         // TODO Auto-generated constructor stub
 6         helper=new DBHelper(context);
 7         database=helper.getWritableDatabase();
 8     }
 9     
10     //插入
11     public boolean insert(String tableName,String nullColumnHack,ContentValues values){
12         boolean flag=false;
13         long count=database.insert(tableName, nullColumnHack, values);
14         flag=count>0?true:false;
15         return flag;
16     }
17     //查询
18     public Cursor queryByCursor(String table, String[] columns, String selection,
19             String[] selectionArgs, String groupBy, String having,
20             String orderBy){
21         Cursor cursor=null;
22         cursor=database.query(table, columns, selection, selectionArgs, groupBy, having, orderBy);
23         
24         return cursor;
25         
26         
27     }
28     //关闭数据库
29     public void closeConn(){
30         if(database!=null){database.close();}
31         
32     }
33     
34     
35 
36 }

----------------------PersonServiceImp----------------------------------------------

 1 public class PersonServiceImp implements PersonService {
 2 private DBManger manger;
 3     public PersonServiceImp(Context context) {
 4         // TODO Auto-generated constructor stub
 5         manger=new DBManger(context);
 6     }
 7 
 8     @Override
 9     public Cursor listPersonCursor() {
10         // TODO Auto-generated method stub
11         return manger.queryByCursor("person", null,  null,  null,  null,  null,  null);
12     }
13 
14     @Override
15     public boolean addPerson(ContentValues values) {
16         // TODO Auto-generated method stub
17         boolean flag=false;
18         flag=manger.insert("person", null, values);
19         return flag;
20     }
21 
22 }

--------------------------MyTest()---------------------------------------------

 1 public class MyTest extends AndroidTestCase {
 2 
 3     public MyTest() {
 4         // TODO Auto-generated constructor stub
 5     }
 6 
 7     public void insert(){
 8     
 9         
10         PersonService service=new PersonServiceImp(getContext());
11         ContentValues values=new ContentValues();
12         values.put("name", "zy");
13         values.put("address", "重庆");
14         
15         service.addPerson(values);
16         
17     }
18 
19 
20 
21 }

----------------------MainActivity------------------------------------

  1 public class MainActivity extends Activity implements LoaderCallbacks<Cursor> {
  2     private ListView listView;
  3     private android.widget.SimpleCursorAdapter adapter;
  4     private static PersonService service;
  5     private LoaderManager manager;
  6 
  7     @Override
  8     protected void onCreate(Bundle savedInstanceState) {
  9         super.onCreate(savedInstanceState);
 10         setContentView(R.layout.fragment_main);
 11         service = new PersonServiceImp(this);
 12         Cursor cursor = service.listPersonCursor();
 13         listView = (ListView) this.findViewById(R.id.listView1);
 14         // String[]from={"name","address"};
 15         // int[]to={R.id.listView1,R.id.textView2};
 16         // adapter = new android.widget.SimpleCursorAdapter(this ,
 17         // R.layout.fragment_main, cursor,
 18         // from, to);
 19         // listView.setAdapter(adapter);
 20 
 21         manager = getLoaderManager();// 获得Loader的管理类
 22         manager.initLoader(1001, null, this);
 23         // 主线程调用loader
 24         manager.getLoader(1001).onContentChanged();
 25 
 26     }
 27 
 28     @Override
 29     public boolean onCreateOptionsMenu(Menu menu) {
 30 
 31         // Inflate the menu; this adds items to the action bar if it is present.
 32         getMenuInflater().inflate(R.menu.main, menu);
 33         return true;
 34     }
 35 
 36     @Override
 37     public boolean onOptionsItemSelected(MenuItem item) {
 38         // Handle action bar item clicks here. The action bar will
 39         // automatically handle clicks on the Home/Up button, so long
 40         // as you specify a parent activity in AndroidManifest.xml.
 41         int id = item.getItemId();
 42         if (id == R.id.action_settings) {
 43             return true;
 44         }
 45         return super.onOptionsItemSelected(item);
 46     }
 47 
 48     /**
 49      * A placeholder fragment containing a simple view.
 50      */
 51     public static class PlaceholderFragment extends Fragment {
 52 
 53         public PlaceholderFragment() {
 54         }
 55 
 56         @Override
 57         public View onCreateView(LayoutInflater inflater, ViewGroup container,
 58                 Bundle savedInstanceState) {
 59             View rootView = inflater.inflate(R.layout.fragment_main, container,
 60                     false);
 61             return rootView;
 62         }
 63     }
 64 
 65     @Override
 66     public Loader<Cursor> onCreateLoader(int id, Bundle args) {
 67         // TODO Auto-generated method stub
 68         // 完成查询操作,把结果返回给调用者
 69 
 70         return new MyAsyncLoader(this);
 71     }
 72 
 73     // 更新UI操作
 74     @Override
 75     public void onl oadFinished(Loader<Cursor> loader, Cursor data) {
 76         // TODO Auto-generated method stub
 77         adapter = new android.widget.SimpleCursorAdapter(this,
 78                 R.layout.fragment_main, data,
 79                 new String[] { "name", "address" }, new int[] { R.id.textView1,
 80                         R.id.textView2 });
 81         listView.setAdapter(adapter);
 82         adapter.notifyDataSetChanged();
 83     }
 84 
 85     @Override
 86     public void onl oaderReset(Loader<Cursor> loader) {
 87         // TODO Auto-generated method stub
 88 
 89     }
 90 
 91     // 异步加载数据
 92     public static class MyAsyncLoader extends AsyncTaskLoader<Cursor> {
 93 
 94         public MyAsyncLoader(Context context) {
 95             super(context);
 96             // TODO Auto-generated constructor stub
 97         }
 98 
 99         @Override
100         protected void onStartLoading() {
101             // TODO Auto-generated method stub
102             super.onStartLoading();
103             // 如果内容改变,通知调用者
104             if (takeContentChanged()) {
105                 forceLoad();
106 
107             }
108 
109         }
110 
111         // 完成查询操作
112         @Override
113         public Cursor loadInBackground() {
114             // TODO Auto-generated method stub
115             Cursor cursor = service.listPersonCursor();
116             return cursor;
117         }
118 
119     }
120 
121 }

技术分享

 

Android开始之 数据存储CursorAdapter游标适配器&Loader和AsyncLoader的使用