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

【Android开始之 数据存储CursorAdapter游标适配器& Loader和AsyncLoader的使用】幼敏悟过人,读书辄成诵。这篇文章主要讲述Android开始之 数据存储CursorAdapter游标适配器& Loader和AsyncLoader的使用相关的知识,希望能为你提供帮助。

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

文章图片

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

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

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

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

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

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

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

文章图片

 

    推荐阅读