本人是刚入android的菜鸟,如果下面有写的不对的地方,特别欢迎纠正。
先说说对loadermanager的理解,loadermanager是对loader进行管理,而loader的作用就是“异步”加载数据,也就是说loader相当于有两个功能(这种说法比较牵强),一个是查询功能,另一种是异步,这样做的好处是ui加载与数据加载分离,效率方面的到很大提升。而loadermanager的功能是监听(monitor)loader的数据,同时将数据的变化返回到ui,进行展现,这里用到了一种设计模式----观察者设计模式。
对于contentprovider,就类似于创建一个“服务器”,“服务器”需要地址访问,同时需要对地址进行解析,同时需要对数据(如:数据库等),进行CRUD操作。总之,contentprovider作用:1.对地址进行解析 2.CRUD 操作 3.添加访问地址 等
好了废话不多说上代码。
AndroidManifest.xml
DBHelper.java
package com.example.android_contentprovdier_loadermanager.db;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBHelper extends SQLiteOpenHelper {
private final static String name = "mydb1.db";
private final static int version = 1;
public DBHelper(Context context) {
super(context, name, null, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "create table person(_id integer primary key autoincrement, name varchar(64), address varchar(64))";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
MyProvider.java
package com.example.android_contentprovdier_loadermanager;
import com.example.android_contentprovdier_loadermanager.db.DBHelper;
import android.content.ContentProvider;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
public class MyProvider extends ContentProvider {
private DBHelper helper;
private ContentResolver resolver;
private static UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
private final static int PERSON = 1;
private final static int PERSONS = 2;
static {
matcher.addURI("com.example.android.MyProvider", "person", PERSON);
matcher.addURI("com.example.android.MyProvider", "person/*", PERSONS);
}
@Override
public boolean onCreate() {
helper = new DBHelper(getContext());
resolver = getContext().getContentResolver();
return false;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
SQLiteDatabase database = helper.getReadableDatabase();
Cursor cursor = database.query("person", projection, selection, selectionArgs, null, null, sortOrder);
resolver.notifyChange(uri, null);
return cursor;
}
@Override
public String getType(Uri uri) {
int match = matcher.match(uri);
switch(match) {
case PERSON:
return "vnd.android.cursor.item/person";
case PERSONS:
return "vnd.android.cursor.dir/person";
}
return null;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
SQLiteDatabase database = helper.getWritableDatabase();
int match = matcher.match(uri);
switch (match) {
case PERSON:
long key = database.insert("person", null, values);
Uri uri1 = ContentUris.withAppendedId(uri, key);
resolver.notifyChange(uri1, null);
return uri1;
case PERSONS:
break;
}
return null;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
return 0;
}
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
return 0;
}
}
package com.example.android_contentprovdier_loadermanager;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.LoaderManager;
import android.app.LoaderManager.LoaderCallbacks;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.CursorLoader;
import android.content.Loader;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity implements LoaderCallbacks {
private Button button1;
private ListView listView1;
private LoaderManager manager;
private MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) this.findViewById(R.id.button1);
listView1 = (ListView) this.findViewById(R.id.listView1);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ContentResolver resolver = MainActivity.this.getContentResolver();
ContentValues values = new ContentValues();
values.put("name", "你好");
values.put("address", "济南");
Uri uri = Uri.parse("content://com.example.android.MyProvider/person");
resolver.insert(uri, values);
//重启loadermanager,告诉它重新加载数据
manager.restartLoader(101, null, MainActivity.this);
}
});
manager = getLoaderManager();
//mainactivity实现loadCallback接口,所以这边的callback用this
manager.initLoader(101, null, this);
}
public class MyAdapter extends BaseAdapter {
private List list;
public MyAdapter(List list) {
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView textView1 = null;
if(convertView != null) {
textView1 = (TextView) convertView;
} else {
textView1 = new TextView(MainActivity.this);
}
textView1.setText(list.get(position));
return textView1;
}
}
//异步查询数据
@Override
public Loader onCreateLoader(int id, Bundle args) {
CursorLoader loader = new CursorLoader(this);
Uri uri = Uri.parse("content://com.example.android.MyProvider/person");
loader.setUri(uri);
loader.setProjection(new String[]{"address"});
return loader;
}
//查询过后对数据进行遍历
@Override
public void onLoadFinished(Loader loader, Cursor data) {
List list = new ArrayList();
int count = data.getColumnCount();
while(data.moveToNext()) {
for(int i=0; i loader) {
}
}