Como usar dois Cursores e CursorJoiner no LoaderManager no Android
eu tenho umContentProvider
, está tendo duas tabelas 1.OnlineContacts
2)AllContacts
. Então, eu tenho um método no qual estou consultando as duas tabelas e obtendo suas resultantescursors
separadamente. E depoisjuntar eles usandoCursorJoiner
e fazendo uma lista deContacts
. Passando esta lista para o meuCustomAdapter extending BaseAdapter
estou preenchendo meulistview
. Gostar :
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;
}
E aqui está o meuCustomAdapter :
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;
}
}
Agora, eu preciso fazer isso usandoLoaderManager
. Eu sei, até certo ponto, a implementação dele. Eu sei, oonCreateLoader
age como:
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);
}
E emOnCreate
se eu usarMyCursorAdapter extending CursorAdapter
, fazemos algo como:
mAdapter = new MyCursorAdapter(getActivity(), null, 0);
setListAdapter(mAdapter);
getLoaderManager().initLoader(0, null, this);
Agora, preciso fazer é como conseguir a implementação acima usandoLoaderManager
. Eu não sabia como perguntar por que é muito explicativo.