【发布时间】:2014-09-04 04:29:32
【问题描述】:
我有一个 ContentProvider,它有两个表 1. OnlineContacts 2. AllContacts。然后我有一个方法,我在其中查询两个表并分别获取它们的结果cursors。然后使用 CursorJoiner加入他们并列出 Contacts。将此列表传递给我的 CustomAdapter extending BaseAdapter,我正在填充我的 listview。喜欢:
public static List<Contact> getContacts(Context context){
List<Contact> contactList = new ArrayList<Contact>();
// Getting First Cursor
String URL = xyz;
Uri baseUri1 = Uri.parse(URL);
String[] select = xyz;
String where =xyz;
Cursor cursor = context.getContentResolver().query(baseUri1, select, where, null, "pid");
// Getting 2nd Cursor
Uri baseUri = xyz;
String[] projection =xyz;
String selection =xyz;
String[] selectionArgs = null;
String sortOrder = xyz;
Cursor mCursor= context.getContentResolver().query(baseUri, projection, selection, selectionArgs, sortOrder);
// Joinging Both Cursors
CursorJoiner joiner = new CursorJoiner(cursor, new String[] {MyContentProvider.PHONE_ID} , mCursor, new String[] {MyContentProvider.Phone._ID});
for (CursorJoiner.Result joinerResult : joiner) {
Contact cont = new Contact();
switch (joinerResult) {
case LEFT:
// handle case where a row in cursorA is unique
break;
case RIGHT:
// handle case where a row in cursorB is unique
case BOTH:
// handle case where a row with the same key is in both cursors
cont.setID(xyz);
cont.setName(xyz);
cont.setPhoneNumber(xyz);
cont.setStatus("0");
contactList.add(cont);
break;
}
}
mCursor.close();
cursor.close();
return contactList;
}
这是我的CustomAdapter:
private class CustomAdapter extends BaseAdapter {
List<Contact> contactsList ;
public CustomAdapter(List<Contact> contactsList){
this.contactsList = contactsList;
}
public List<Contact> contacts() {
return this.contactsList;
}
@Override
public int getCount() {
return contactsList.size();
}
@Override
public Contact getItem(int arg0) {
return contactsList.get(arg0);
}
@Override
public long getItemId(int arg0) {
return arg0;
}
@Override
public View getView(int position, View view, ViewGroup viewGroup) {
SimpleViewHolder viewHolder;
if(view==null)
{
LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.list_item, viewGroup,false);
viewHolder = new SimpleViewHolder(view);
view.setTag(viewHolder);
}
viewHolder = (SimpleViewHolder) view.getTag();
TextView contName = (TextView) viewHolder.get(R.id.nameText);
ImageView image = (ImageView) viewHolder.get(R.id.contact_image);
Contact contact = contactsList.get(position);
image.setBackgroundResource(R.drawable.person_empty_offline);
contName.setText(contact.getName());
return view;
}
}
现在,我需要使用 LoaderManager 来完成。我知道,在某种程度上,它的执行。我知道,onCreateLoader 的行为类似于:
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
Uri baseUri = xyz;
String[] projection = xyz;
String selection = xyz;
String[] selectionArgs = null;
String sortOrder = xyz;
return new CursorLoader(getActivity(), baseUri, projection, selection, selectionArgs, sortOrder);
}
在 OnCreate 中,如果我使用 MyCursorAdapter extending CursorAdapter,我们会执行以下操作:
mAdapter = new MyCursorAdapter(getActivity(), null, 0);
setListAdapter(mAdapter);
getLoaderManager().initLoader(0, null, this);
现在,我需要做的是如何使用 LoaderManager 实现上述实现。我不知道怎么问这就是为什么它太解释了。
【问题讨论】:
-
您可以使用两个加载器进行两个不同的查询,然后将结果连接起来(检查此gist.github.com/luksprog/7ec8cd3fcdea97b5839c),或者您在
ContentProvider级别实现连接并返回MatrixCursorfrom连接的结果。
标签: android android-cursorloader android-loadermanager