codepath / codepath/android_guides

I want to use the previous cursor for indexing purpose( to give index label)

Open
#350 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
No language data
Stars
28.3k
Forks
6.2k
PR merge metrics
No merged PRs in 30d

Description

I want to use the previous cursor for indexing purpose. To give index label i.e: A(All name starting with A's will be in Label A), B, C, D...
I have tried swapCursor but it returns null or sometimes get the previous cursor, then it will not be able to fetch the previous data to compare the current index and/or next data.

Below is the setupCursorAdapter() in Activity

`private void setupCursorAdapter() {
// Column data from cursor to bind views from
String[] uiBindFrom = {Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ?
ContactsContract.Contacts.DISPLAY_NAME_PRIMARY :
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.PHOTO_URI};
// View IDs which will have the respective column data inserted
int[] uiBindTo = {R.id.contactEntryText, R.id.contact_image};
// Create the simple cursor adapter to use for our list
// specifying the template to inflate (item_contact),
mAdapter = new SimpleCursorRecyclerAdapter(R.layout.contact_new_listitem,
null, uiBindFrom, uiBindTo, ctx, ContactsNew.this);
}`

and Below is the Adapter's Bind method
`@Override
public void onBindViewHolder(SimpleViewHolder holder, Cursor cursor) {
String contactName = cursor.getString(cursor.getColumnIndex(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ?
ContactsContract.Contacts.DISPLAY_NAME_PRIMARY :
ContactsContract.Contacts.DISPLAY_NAME));

if(alphaIndexer == null) {
alphaIndexer = new AlphabetIndexer(cursor, ContactsWrapper.getInstance().getContactIndexableColumnIndex(cursor),
" ABCDEFGHIJKLMNOPQRSTUVWXYZ");
}else {
alphaIndexer.setCursor(cursor);
}

int position = cursor.getPosition();

String label = contactName;

if(label!=null) {
if(label.length()>0) {
holder.nameviews.setText(contactName);
}else{
label="No Name";
holder.nameviews.setText(label);
}
}else{
label="No Name";
holder.nameviews.setText(label);
}
char firstChar = label.toUpperCase().charAt(0);
System.out.println("Character: "+firstChar);

String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String photoUri = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.PHOTO_URI));

Cursor phoneCursor = mContext.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[]{id},
null);
String phoneNumber = null;

if (phoneCursor.moveToNext()) {

phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}

if (photoUri == null) {
String letter = gc.split_word(contactName);
TextDrawable drawable = gc.name_image(letter);

holder.textDr.setImageDrawable(drawable);
}else{
try {
Picasso.with(mContext)
.load(photoUri)
.placeholder(R.drawable.contact_active)
.error(R.drawable.contact_active)
.fit().centerCrop()
.into(holder.textDr);
}catch (Exception e){
e.printStackTrace();
}
}

holder.numberView.setText(phoneNumber);

if (gc.getFavContactList(mContext) != null) {
favContactGetSets = gc.getFavContactList(mContext);
}

if (favContactGetSets.contains(id)) {
holder.ivAddToFav1.setImageResource(R.drawable.imgfav);
} else {
holder.ivAddToFav1.setImageResource(R.drawable.imgunfav);
}

applyClickEvents(holder, contactName,phoneNumber);
}`

and Below is the SwapCursor Method:
`public Cursor swapCursor(Cursor newCursor) {
if (newCursor == mCursor) {
return null;
}
Cursor oldCursor = mCursor;
mCursor = newCursor;
if (newCursor != null) {
mRowIDColumn = newCursor.getColumnIndexOrThrow("_id");
mDataValid = true;
// notify the observers about the new cursor
notifyDataSetChanged();
} else {
mRowIDColumn = -1;
mDataValid = false;
// notify the observers about the lack of a data set
notifyItemRangeRemoved(0, getItemCount());
}
return oldCursor;
}`

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.