Binding Cursor data to a non-list layout - android

I'm writing a screen that displays a row's worth of information from a DB. Basically it's a Detail Fragment that represents information pertaining to one 'row' in a table. I want to understand the best practice for binding data from a cursor (one unique row from a table) to a layout of textviews, checkboxes, etc.
Is AdapterView the ticket?
#JoeMalin suggested:
Then write an adapter between a cursor and an array of text views.
Which boils down my question. What's the right way to hook a series of text views to a cursor?

If you want to do processing on some of the cursor data before you move it to the text views, then you're going beyond the adapter pattern, which assumes that "recasting" the form of a data structure to another data structure without any intermediate processing. The virtue of an adapter is that, for two data structures A and B linked by an adapter, it's assumed that B automatically changes whenever A changes.
Of course, you can redefine the idea of adapter to insert your own intermediate operation, such as converting dates, or you could make the conversion an aspect of the view that's displaying the data. I am guessing that the "processing" is really formatting, which you do for display purposes. That's an attribute of the text view, not the data; write something that extends text view and converts dates as needed. Then write an adapter between a cursor and an array of text views.

I recently implemented my own data adapter class that may be in the ball park.
public class NoteImageDataAdapter {
private final View mMainView;
private Cursor mCursor;
private ViewHolder holder;
private ContentObserver mContentObserver;
public static class ViewHolder {
public TextView title;
public TextView text;
public ImageView image;
}
public NoteImageDataAdapter(View mainView, Cursor c) {
if (mainView == null) {
throw new IllegalArgumentException("View mainView cannot be null");
}
if (c == null) {
throw new IllegalArgumentException("Cursor c cannot be null");
}
mMainView = mainView;
mCursor = c;
holder = new ViewHolder();
holder.title = (TextView) mMainView.findViewById(R.id.title);
holder.text = (TextView) mMainView.findViewById(R.id.text);
holder.image = (ImageView) mMainView.findViewById(R.id.myImageView);
mContentObserver = new ImageNoteContentObserver(new Handler());
mCursor.registerContentObserver(mContentObserver);
bindView();
}
class ImageNoteContentObserver extends ContentObserver {
public ImageNoteContentObserver(Handler handler) {
super(handler);
}
#Override
public boolean deliverSelfNotifications() {
return true;
}
#Override
public void onChange(boolean selfChange) {
Log.d("NoteImageDataAdapter", "ImageNoteContentObserver.onChange( "
+ selfChange + ")");
super.onChange(selfChange);
mCursor.requery();
bindView();
}
}
public void bindView() {
Log.d("NoteImageDataAdapter", "bindView");
mCursor.moveToFirst();
holder.text.setText(Note.getText(mCursor));
holder.title.setText(Note.getTitle(mCursor));
Uri imageUri = Note.getImageUri(mCursor);
if (imageUri != null) {
assignImage(holder.image, imageUri);
} else {
Drawable d = Note.getImageThumbnail(mCursor);
holder.image.setImageDrawable(d);
holder.image.setVisibility(View.VISIBLE);
}
}
private static final int MAX_IMAGE_PIXELS = 1024*512;
private void assignImage(ImageView imageView, Uri imageUri){
if (imageView != null && imageUri != null){
ContentResolver cr = imageView.getContext().getContentResolver();
Display display = ((WindowManager) imageView.getContext()
.getSystemService(Context.WINDOW_SERVICE))
.getDefaultDisplay();
int width = (int) (display.getWidth() * 0.9);
int height = (int) (display.getHeight() * 0.9);
int minSideLength = Math.min(height, width);
Bitmap b = Util.makeBitmap(minSideLength, MAX_IMAGE_PIXELS, imageUri, cr, false);
if (b == null){
b = Util.makeBitmap(minSideLength, MAX_IMAGE_PIXELS/2, imageUri, cr, false);
}
if (b != null){
imageView.setImageBitmap(b);
imageView.setAdjustViewBounds(true);
imageView.setVisibility(View.VISIBLE);
}
}
}
}
and in your activity
private NoteImageDataAdapter mAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.note_image_view_layout);
wireDataAdapter();
}
private void wireDataAdapter() {
final String[] COLUMNS = new String[] {
Note.Columns.TITLE,
Note.Columns.TEXT,
Note.Columns.IMAGE_URI,
Note.Columns.IMAGE_THUMBNAIL,
Note.Columns._ID };
// the uri for the note row
Uri contentUri = getIntent().getData();
Cursor cur = managedQuery(contentUri, COLUMNS, null, null, null);
View mainLayout = this.findViewById(R.id.noteImageViewLayout);
mAdapter = new NoteImageDataAdapter(mainLayout, cur);
}

From the activity use:
Adpater adapter = new Adapter(Activity.this or context , Cursor);
setListAdapter(adapter) in case of List Activity;
Otherwise
listViewObj.setAdpater(adapter)
public class CustomCursorAdapter extends CursorAdapter {
private LayoutInflater mInflater;
private Context activityContext;
private ViewHolder holder;
public ContactsAdapter(Context aContext,Cursor cursor) {
super(mContext, cursor);
mInflater = LayoutInflater.from(mContext);
activityContext = aContext;
}
public static class ViewHolder{
public TextView textView1;
// View Group on Row inflate lyaout that need to be used
public ImageView imageView;
}
#Override
public void bindView(View v, Context context, Cursor c) {
holder=(ViewHolder)v.getTag();
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
final LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.item_inflate_layout, parent, false);
holder = new ViewHolder();
holder.textView1 = (TextView) v.findViewById(R.id.TEXTVIEW1);
// Other Id that need to be used and are available on item_inflate_layout
holder.imageView = (ImageView) v.findViewById(R.id.IMAGEVIEW);
v.setTag(holder);
bindView(v, context, cursor);
return v;
}
}

Related

Android - CursorAdapter doesn't work

I have a ChatActivity, which loads its data via a CursorLoader. The CursorLoader return a cursor with two registers, but the newView and bindView methods in adapter is never called.
My activity
public class ChatActivity extends BaseActivity implements LoaderManager.LoaderCallbacks<Cursor> {
public static final String EXTRA_AMANTEID = "amanteId";
private EditText messageET;
private ListView messagesContainer;
private Button sendBtn;
private ChatAdapter adapter;
private Long amanteId;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
amanteId = getIntent().getLongExtra(ChatActivity.EXTRA_AMANTEID, 0L);
messagesContainer = (ListView) findViewById(R.id.messagesContainer);
messageET = (EditText) findViewById(R.id.messageEdit);
sendBtn = (Button) findViewById(R.id.chatSendButton);
RelativeLayout container = (RelativeLayout) findViewById(R.id.container);
adapter = new ChatAdapter(this);
getLoaderManager().initLoader(0, null, this);
messagesContainer.setAdapter(adapter);
}
private void scroll() {
messagesContainer.setSelection(messagesContainer.getCount() - 1);
}
#Override
public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
return new CursorLoader(ChatActivity.this, MensagemProvider.CONTENT_URI_CONVERSA, null, null, new String[]{Long.toString(amanteId), Long.toString(amanteId)}, null);
}
#Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
adapter.swapCursor(cursor);
}
#Override
public void onLoaderReset(Loader<Cursor> loader) {
adapter.swapCursor(null);
}
}
My adapter
public class ChatAdapter extends CursorAdapter {
private Cursor cursor;
private int dataEnvioColumnIndex;
private int idMensagemColumnIndex;
private int idRemetenteColumnIndex;
private int idDestinatarioColumnIndex;
private int apelidoRemetenteColumnIndex;
private int apelidoDestinatarioColumnIndex;
private int textoMensagemColumnIndex;
private long idColaboradorLogado;
public ChatAdapter(Context context) {
super(context, null, false);
}
public ChatMessage getItem() {
ChatMessage message = new ChatMessage();
SimpleDateFormat dt = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
Date dataEnvio = new Date(cursor.getLong(dataEnvioColumnIndex));
message.setDate(dt.format(dataEnvio));
message.setId(cursor.getLong(idMensagemColumnIndex));
Long de = cursor.getLong(idRemetenteColumnIndex);
Long logado = BaseApp.getCredentials().getId();
message.setMe(de.equals(logado));
message.setMessage(cursor.getString(textoMensagemColumnIndex));
return message;
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View retView = vi.inflate(R.layout.list_item_chat_message, null);
return retView;
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
ViewHolder holder = createViewHolder(view);;
view.setTag(holder);
ChatMessage chatMessage = getItem();
boolean myMsg = chatMessage.getIsme() ;//Just a dummy check
holder.txtMessage.setText(chatMessage.getMessage());
holder.txtInfo.setText(chatMessage.getDate());
}
private ViewHolder createViewHolder(View v) {
ViewHolder holder = new ViewHolder();
holder.txtMessage = (TextView) v.findViewById(R.id.txtMessage);
holder.content = (LinearLayout) v.findViewById(R.id.content);
holder.contentWithBG = (LinearLayout) v.findViewById(R.id.contentWithBackground);
holder.txtInfo = (TextView) v.findViewById(R.id.txtInfo);
return holder;
}
private static class ViewHolder {
public TextView txtMessage;
public TextView txtInfo;
public LinearLayout content;
public LinearLayout contentWithBG;
}
#Override
public Cursor swapCursor(Cursor cursor) {
if(cursor!=null) {
cursor.moveToFirst();
idMensagemColumnIndex = cursor.getColumnIndex(MensagemProvider.COLUMN_MENSAGEMID);
idRemetenteColumnIndex = cursor.getColumnIndex(MensagemProvider.COLUMN_DE);
idDestinatarioColumnIndex = cursor.getColumnIndex(MensagemProvider.COLUMN_PARA);
apelidoRemetenteColumnIndex = cursor.getColumnIndex(MensagemProvider.COLUMN_APELIDO_REMETENTE);
apelidoDestinatarioColumnIndex = cursor.getColumnIndex(MensagemProvider.COLUMN_APELIDO_DESTINATARIO);
textoMensagemColumnIndex = cursor.getColumnIndex(MensagemProvider.COLUMN_MENSAGEM);
}
notifyDataSetChanged();
return cursor;
}
}
what I'm doing wrong ? Can anybody help me ?
Thanks!
Overriding swapCursor() is asking for trouble. The cursor won't be positioned where the adapter expects it to be positioned (before first). And you don't call super.swapCursor() so the adapter never really hears about the new cursor.
I bet you're trying to "optimize" by getting the column indexes only once each time a new cursor is swapped.
First just try getting rid of the swapCursor() override and making the getColumnIndex() calls in your getItem() method. If that works and you still really want to have getColumnIndex() called only once per cursor, you could try something like setting all your cursor indexes to -1 when you swap the cursor, then calling getColumnIndex() inside getItem() only when the index is -1.
But don't mess with swapCursor(), especially without calling super.swapCursor() and returning its result.

Android AdapterView cannot display database records in some device

I would like to ask some question about AdapterView.
In my application, there is an activity which retrieve data from database and display them in AdapterView.
However, when i install the application in different devices, I found that the part I have just mentioned could only function on some devices. The others cannot show the database results.
Here is my code:
private void showResults(String query) {
Cursor cursor = searchCustByInputText(query);
if (cursor == null) {
//
} else {
// Specify the columns we want to display in the result
String[] from = new String[] {
"cust_code",
"chinese_name"};
// Specify the Corresponding layout elements where we want the columns to go
int[] to = new int[] {
R.id.scust_code,
R.id.schinese_name};
// Create a simple cursor adapter for the definitions and apply them to the ListView
SimpleCursorAdapter customers = new SimpleCursorAdapter(this,R.layout.cust_list_item, cursor, from, to);
mListView.setAdapter(customers);
// Define the on-click listener for the list items
mListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Cursor c = (Cursor) mListView.getItemAtPosition(position);
String cust_code = c.getString(c.getColumnIndex("cust_code"));
if (callFromAct.equals("Main")) {
String pay_term = c.getString(c.getColumnIndex("pay_term"));
String chinese_name = c.getString(c.getColumnIndex("chinese_name"));
String english_name = c.getString(c.getColumnIndex("english_name"));
String address_1 = c.getString(c.getColumnIndex("address_1"));
String address_2 = c.getString(c.getColumnIndex("address_2"));
String address_3 = c.getString(c.getColumnIndex("address_3"));
String address_4 = c.getString(c.getColumnIndex("address_4"));
String contact = c.getString(c.getColumnIndex("contact"));
String telephone = c.getString(c.getColumnIndex("telephone"));
String last_order_date = c.getString(c.getColumnIndex("last_order_date"));
//Pass data to another Activity
Intent it = new Intent(CustEnqActivity.this, CustEnqDetailsActivity.class);
Bundle bundle = new Bundle();
bundle.putString("cust_code", cust_code);
bundle.putString("pay_term", pay_term);
bundle.putString("chinese_name", chinese_name);
bundle.putString("english_name", english_name);
bundle.putString("address_1", address_1);
bundle.putString("address_2", address_2);
bundle.putString("address_3", address_3);
bundle.putString("address_4", address_4);
bundle.putString("contact", contact);
bundle.putString("telephone", telephone);
bundle.putString("last_order_date", last_order_date);
it.putExtras(bundle);
startActivity(it);
}
else {
returnToCallingAct(cust_code);
}
//searchView.setQuery("",true);
}
});
}
}
Besides, I discovered there were two warnings in my logcat.
The constructor SimpleCursorAdapter(Context, int, Cursor, String[], int[]) is deprecated
AdapterView is a raw type. References to generic type AdapterView should be parameterized
Are they related to the problem?
Try to create a class that extends BaseAdapter and use ViewHolders for performance
eg:
public class MyBaseAdapter extends BaseAdapter {
ArrayList<ListData> myList = new ArrayList<ListData>();
LayoutInflater inflater;
Context context;
public MyBaseAdapter(Context context, ArrayList<ListData> myList) {
this.myList = myList;
this.context = context;
inflater = LayoutInflater.from(this.context); // only context can also be used
}
#Override
public int getCount() {
return myList.size();
}
#Override
public ListData getItem(int position) {
return myList.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
MyViewHolder mViewHolder;
if(convertView == null) {
convertView = inflater.inflate(R.layout.layout_list_item, null);
mViewHolder = new MyViewHolder();
convertView.setTag(mViewHolder);
} else {
mViewHolder = (MyViewHolder) convertView.getTag();
}
mViewHolder.tvTitle = detail(convertView, R.id.tvTitle, myList.get(position).getTitle());
mViewHolder.tvDesc = detail(convertView, R.id.tvDesc, myList.get(position).getDescription());
mViewHolder.ivIcon = detail(convertView, R.id.ivIcon, myList.get(position).getImgResId());
return convertView;
}
// or you can try better way
private TextView detail(View v, int resId, String text) {
TextView tv = (TextView) v.findViewById(resId);
tv.setText(text);
return tv;
}
private ImageView detail(View v, int resId, int icon) {
ImageView iv = (ImageView) v.findViewById(resId);
iv.setImageResource(icon); //
return iv;
}
private class MyViewHolder {
TextView tvTitle, tvDesc;
ImageView ivIcon;
}
}
More info/example:
http://www.pcsalt.com/android/listview-using-baseadapter-android/#sthash.lNGSCiyB.dpbs

Picasso loading of image spawned inside AsyncTask

So I am trying to use the Picasso Library for image downloading and caching. In order to get the contactUri to pass to Picasso I need to make a query to the Contacts Content Provider. Since I don't want to block the main UI thread to get the contactId, I have put this in an AsyncTask. And once I get that contactId, I make the call to Picasso in the onPostExecute() method of the AsyncTask.
However, I am noticing a flickering that shows up when I scroll through my ListView quickly. It seems to me that there is an issue with the ViewHolder since the recycled views are displaying the previous image before setting the appropriate image. Is there anyway to avoid this?
public class ConversationThreadsCursorAdapter extends SimpleCursorAdapter {
// region Constants
private static final int RECIPIENT_IDS_COLUMN_INDEX = 3;
private static final int ID2_COLUMN_INDEX = 0;
private static final int ADDRESS_COLUMN_INDEX = 1;
// endregion
// region Variables
private final String DEBUG_TAG = getClass().getSimpleName().toString();
private Context mContext;
protected Drawable mDefaultPicDrawable;
protected ContentResolver mContentResolver;
protected LinearLayout.LayoutParams mContactPicLayoutParams;
// endregion
// region Constructors
public ConversationThreadsCursorAdapter(Context context, int layout,
Cursor c, String[] from, int[] to, int flags) {
super(context, layout, c, from, to, flags);
mContext = context;
mDefaultPicDrawable = mContext.getResources().getDrawable(
R.drawable.ic_contact_picture);
mContactPicLayoutParams = new LinearLayout.LayoutParams(
mDefaultPicDrawable.getIntrinsicWidth(),
mDefaultPicDrawable.getIntrinsicHeight());
}
// endregion
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView == null) {
convertView = mLayoutInflater.inflate(R.layout.simple_message, null);
// Creates a ViewHolder and store references to the children
// views we want to bind data to.
viewHolder = setupViewHolder(convertView);
convertView.setTag(viewHolder);
} else {
// Get the ViewHolder back to get fast access to the TextView
// and the ImageView.
viewHolder = (ViewHolder) convertView.getTag();
viewHolder.task.cancel(true);
}
mCursor = getCursor();
mCursor.moveToPosition(position);
viewHolder.position = position;
String recipient_ids = mCursor.getString(RECIPIENT_IDS_COLUMN_INDEX);
String[] recipients = recipient_ids.split(" ");
viewHolder.task = new AddressFetcherTask(viewHolder, position);
viewHolder.task.execute(recipients);
return convertView;
}
// region Helper Methods
private ViewHolder bindUIElements(View convertView) {
ViewHolder viewHolder = new ViewHolder();
viewHolder.contactBadge = (QuickContactBadge) convertView.findViewById(R.id.contact_pic);
return viewHolder;
}
private ViewHolder setupViewHolder(View convertView) {
ViewHolder viewHolder = bindUIElements(convertView);
viewHolder.contactBadge.setLayoutParams(mContactPicLayoutParams);
return viewHolder;
}
// endregion
// region Inner Classes
private class ViewHolder {
QuickContactBadge contactBadge;
int position;
}
private class AddressFetcherTask extends AsyncTask < String[], Void, Integer > {
private ViewHolder mViewHolder;
private int mPosition;
public AddressFetcherTask(ViewHolder viewHolder, int position) {
mViewHolder = viewHolder;
mPosition = position;
}
#Override
protected Integer doInBackground(String[]...recipients) {
String recipient = recipients[0][0];
Log.d(DEBUG_TAG, "recipient is " + recipient);
Cursor c = mContentResolver.query(
Uri.parse("content://mms-sms/canonical-addresses"), null, "_id = " + recipient, null, null);
String _id = "";
String address = "";
while (c.moveToNext()) {
_id = c.getString(ID2_COLUMN_INDEX);
address = c.getString(ADDRESS_COLUMN_INDEX);
}
c.close();
int contactId;
if (address != null) {
contactId = ContactsUtils.getContactId(mContext, address, "address");
} else {
contactId = Integer.valueOf(address);
}
return contactId;
}
#Override
protected void onPostExecute(Integer contactId) {
if (mViewHolder.position == mPosition) {
Picasso.with(mContext)
.load(getContactUri(contactId))
.placeholder(R.drawable.ic_contact_picture)
.into(mViewHolder.contactBadge);
}
}
}
// endregion
}
Just set the imageview to null in within getView and it should remove what you are experiencing for the most part you'll be right.
The other tiny tiny corner case aspect is that when your asynctask arrives at postExecute, the view might still exist, but it might have already been assigned a different contact to load up (it's been recycled).
You need to put some kind of tag in the viewholder, and then check that it is still the same when you go to set it in postexecute.
To remove the fade in, you need to remove the asynctask from the getview. You need to be able to call picasso within getview, which means having your data ready before arriving at getview.
The below, not quite sure if it will compile, I've done it in a text editor.
But bassically I'm caching results in mCachedContactIds and just reloading the whole table if I need a new one. I've typically found this to be robust. But you can also call the picasso code which I've commented out
public class ConversationThreadsCursorAdapter extends SimpleCursorAdapter {
// region Constants
private static final int RECIPIENT_IDS_COLUMN_INDEX = 3;
private static final int ID2_COLUMN_INDEX = 0;
private static final int ADDRESS_COLUMN_INDEX = 1;
private HashMap<String, Integer> mCachedContactIds = new HashMap<String, Integer>();
// endregion
// region Variables
private final String DEBUG_TAG = getClass().getSimpleName().toString();
private Context mContext;
protected Drawable mDefaultPicDrawable;
protected ContentResolver mContentResolver;
protected LinearLayout.LayoutParams mContactPicLayoutParams;
// endregion
// region Constructors
public ConversationThreadsCursorAdapter(Context context, int layout,
Cursor c, String[] from, int[] to, int flags) {
super(context, layout, c, from, to, flags);
mContext = context;
mDefaultPicDrawable = mContext.getResources().getDrawable(
R.drawable.ic_contact_picture);
mContactPicLayoutParams = new LinearLayout.LayoutParams(
mDefaultPicDrawable.getIntrinsicWidth(),
mDefaultPicDrawable.getIntrinsicHeight());
}
// endregion
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView == null) {
convertView = mLayoutInflater.inflate(R.layout.simple_message, null);
// Creates a ViewHolder and store references to the children
// views we want to bind data to.
viewHolder = setupViewHolder(convertView);
convertView.setTag(viewHolder);
} else {
// Get the ViewHolder back to get fast access to the TextView
// and the ImageView.
viewHolder = (ViewHolder) convertView.getTag();
viewHolder.task.cancel(true);
viewHolder.contactBadge.setImageDrawable(mDefaultPicDrawable);
}
mCursor = getCursor();
mCursor.moveToPosition(position);
viewHolder.position = position;
String recipient_ids = mCursor.getString(RECIPIENT_IDS_COLUMN_INDEX);
String[] recipients = recipient_ids.split(" ");
String recipient = recipients[0];
if(mCachedContactIds.get(recipient) != null){
Picasso.with(mContext)
.load(getContactUri(mCachedContactIds.get(recipient)))
.placeholder(R.drawable.ic_contact_picture)
.into(mViewHolder.contactBadge);
} else {
viewHolder.task = new AddressFetcherTask(viewHolder, position);
viewHolder.task.execute(recipients);
}
return convertView;
}
// region Helper Methods
private ViewHolder bindUIElements(View convertView) {
ViewHolder viewHolder = new ViewHolder();
viewHolder.contactBadge = (QuickContactBadge) convertView.findViewById(R.id.contact_pic);
return viewHolder;
}
private ViewHolder setupViewHolder(View convertView) {
ViewHolder viewHolder = bindUIElements(convertView);
viewHolder.contactBadge.setLayoutParams(mContactPicLayoutParams);
return viewHolder;
}
// endregion
// region Inner Classes
private class ViewHolder {
QuickContactBadge contactBadge;
int position;
AddressFetcherTask task;
}
private class AddressFetcherTask extends AsyncTask < String[], Void, Integer > {
private ViewHolder mViewHolder;
private int mPosition;
private String mRecipient;
public AddressFetcherTask(ViewHolder viewHolder, int position) {
mViewHolder = viewHolder;
mPosition = position;
}
#Override
protected Integer doInBackground(String[]...recipients) {
mRecipient = recipients[0][0];
Log.d(DEBUG_TAG, "recipient is " + recipient);
Cursor c = mContentResolver.query(
Uri.parse("content://mms-sms/canonical-addresses"), null, "_id = " + mRecipient, null, null);
String _id = "";
String address = "";
while (c.moveToNext()) {
_id = c.getString(ID2_COLUMN_INDEX);
address = c.getString(ADDRESS_COLUMN_INDEX);
}
c.close();
int contactId;
if (address != null) {
contactId = ContactsUtils.getContactId(mContext, address, "address");
} else {
contactId = Integer.valueOf(address);
}
return contactId;
}
#Override
protected void onPostExecute(Integer contactId) {
if (mViewHolder.position == mPosition) {
mCachedContactIds.put(mRecipient, contactId);
Picasso.with(mContext)
.load(getContactUri(mCachedContactIds.get(recipient)))
.placeholder(R.drawable.ic_contact_picture)
.into(mViewHolder.contactBadge);
}
}
}
// endregion
}
Or if all that's bugging you left is the fade from picasso, then add noFade() to the request.

Simplecursoradapter with Listview just mixed up while scrolling

Folks.
I've a problem with my simplecursorAdapter. Everything works perfectly except when scrolling the listview it just mixes up the Favorite icon for the rows which included in the custom layout and it just appears randomly while scrolling. Can you guide me what's wrong in my code?
Thanks in advance!
public class AlternateRowCursorAdapter extends SimpleCursorAdapter {
int layoutn;
Cursor localCursor, test;
Bitmap bitImg;
Context localContext;
Bitmap Avatar;
ImageView one, two, three, four, five;
LayoutInflater mInflater;
SQLiteDatabase mDb;
MyDbHelper mHelper;
public static final String TABLE_NAME = "MSGS";
public static final String COL_MsgID = "msgIdc";
public static final String COL_MsgCat = "msgCatC";
public static final String COL_MsgTit = "msgtitleC";
public static final String COL_MsgFavor = "msgFavorC";
public AlternateRowCursorAdapter (Context context, int layout, Cursor c,
String[] from, int[] to) {
super(context, R.layout.listtype, c, from, to);
this.localContext = context;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = super.getView(position, convertView, parent);
final Cursor cursbbn = getCursor();
if (row == null)
{
row = ((LayoutInflater) localContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE))
.inflate(R.layout.listtype, null);
}
final String Title;
String SandID;
final String MsgID;
final String MsgFav;
Typeface tf = Typeface.createFromAsset(localContext.getAssets(),"fonts/khalaadsara.ttf");
Title = cursbbn.getString(2);
SandID = cursbbn.getString(1);
MsgID=cursbbn.getString(0);
MsgFav=cursbbn.getString(4);
TextView titler = (TextView) row.findViewById(R.id.Sandtit);
titler.setTypeface(tf);
titler.setText(Title);
one = (ImageView) row.findViewById(R.id.imageView5);
two = (ImageView) row.findViewById(R.id.imageView4);
three = (ImageView) row.findViewById(R.id.ImageView03);
four = (ImageView) row.findViewById(R.id.ImageView02);
five = (ImageView) row.findViewById(R.id.imageView1);
if(MsgFav.contentEquals("YES"))
{
one.setImageResource(R.drawable.favorpress);
}
return row;
}
}
Edit : Here is my code to refresh the values in Onresume event :
private void refreshvalues() {
mDb = mHelper.getWritableDatabase();
curs = mDb.query(MyDbHelper.TABLE_NAME, columns, null, null, null,
null, null,
null);
cursF = mDb.query(TABLE_NAME, columns, COL_MsgFavor + "=" + "?",
new String[] { "YES" }, null, null, COL_MsgTit + " ASC");
String[] headers = new String[] {MyDbHelper.COL_MsgTit ,MyDbHelper.COL_MsgID};
mAdapter = new AlternateRowCursorAdapter(this, R.layout.listtype, curs,
headers, new int[] { R.id.Sandtit});
fAdapter = new AlternateRowCursorAdapter(this, R.layout.listtype,
cursF, headers, new int[] { R.id.Sandtit });
mList.setAdapter(mAdapter);
fList.setAdapter(fAdapter);
curs.moveToFirst();
cursF.moveToFirst();
mAdapter.notifyDataSetChanged();
fAdapter.notifyDataSetChanged();
mList.invalidateViews();
fList.invalidateViews();
curs.requery();
cursF.requery();
}
First of all, you are customizing the ListView which needs to implement the custom adapter that extends any of the SimpleCustomAdapter, ArrayAdapter, and BaseAdapter.
Now, you get your listview messed up because, you are not holding the values in the views. You need to write a class ViewHolder that holds your views of the ListView in the same state, before it was scrolled. And Inside getView() method, you need to implement the else part of the if statement.
This is the efficient way to use ListView. Check below, how it can be implemented:
For example:
I made a class Task which contains the values from the database as:
static class Task {
int task_id;
String task_brief;
String task_priority;
String is_completed = "false";
Task(int tmp_task_id, String tmp_task_brief, String tmp_task_priority,
String tmp_task_is_completed) {
task_id = tmp_task_id;
task_brief = tmp_task_brief;
task_priority = tmp_task_priority;
is_completed = tmp_task_is_completed;
}
int get_task_id() {
return task_id;
}
String get_task_brief() {
return task_brief;
}
String get_task_priority() {
return task_priority;
}
String get_task_is_completed() {
return is_completed;
}
void set_task_is_completed(String tmp_task_is_completed) {
is_completed = tmp_task_is_completed;
}
}
Now, we create a class TaskViewHolder that holds the view:
static class TaskViewHolder {
TextView tv_task_brief;
ImageView iv_task_is_completed;
public TaskViewHolder(TextView tmp_tv_task_brief,
ImageView tmp_iv_task_is_completed) {
tv_task_brief = tmp_tv_task_brief;
iv_task_is_completed = tmp_iv_task_is_completed;
}
TextView get_tv_task_brief() {
return tv_task_brief;
}
ImageView get_iv_task_is_completed() {
return iv_task_is_completed;
}
}
And after that, implement custom adapter as below:
static class TaskAdapter extends ArrayAdapter<Task> {
LayoutInflater inflater;
public TaskAdapter(Context context, List<Task> tmp_al_task) {
super(context, R.layout.single_row_home,
R.id.textViewSingleRowHome, tmp_al_task);
inflater = LayoutInflater.from(context);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Task task = (Task) this.getItem(position);
final ImageView imageView;
final TextView textView;
if (convertView == null) {
convertView = inflater.inflate(R.layout.single_row_home, null);
imageView = (ImageView) convertView
.findViewById(R.id.imageViewSingleRowHome);
textView = (TextView) convertView
.findViewById(R.id.textViewSingleRowHome);
convertView.setTag(new TaskViewHolder(textView, imageView));
} else {
TaskViewHolder viewHolder = (TaskViewHolder) convertView
.getTag();
imageView = viewHolder.get_iv_task_is_completed();
textView = viewHolder.get_tv_task_brief();
}
imageView.setTag(task);
textView.setText(task.get_task_brief());
if(task.get_task_priority().equals("High"))
textView.setTextColor(Color.RED);
else if(task.get_task_priority().equals("Medium"))
textView.setTextColor(Color.GREEN);
else
textView.setTextColor(Color.BLUE);
if (task.get_task_is_completed().equals("true")) {
imageView.setImageResource(R.drawable.action_cancel_icon);
textView.setPaintFlags(textView.getPaintFlags()
| Paint.STRIKE_THRU_TEXT_FLAG);
} else {
imageView.setImageResource(R.drawable.action_cancel_icon_2);
textView.setPaintFlags( textView.getPaintFlags() & (~ Paint.STRIKE_THRU_TEXT_FLAG));
}
imageView.setFocusable(false);
imageView.setFocusableInTouchMode(false);
imageView.setClickable(true);
imageView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Task task = (Task) imageView.getTag();
if (task.get_task_is_completed().equals("false")) {
imageView.setImageResource(R.drawable.action_cancel_icon);
ContentValues values = new ContentValues();
values.put("is_completed", "true");
database.update("task_info", values, "task_id=?",
new String[] { task.get_task_id() + "" });
values.clear();
textView.setPaintFlags(textView.getPaintFlags()
| Paint.STRIKE_THRU_TEXT_FLAG);
task.set_task_is_completed("true");
} else {
imageView.setImageResource(R.drawable.action_cancel_icon_2);
ContentValues values = new ContentValues();
values.put("is_completed", "false");
database.update("task_info", values, "task_id=?",
new String[] { task.get_task_id() + "" });
values.clear();
textView.setPaintFlags( textView.getPaintFlags() & (~ Paint.STRIKE_THRU_TEXT_FLAG));
task.set_task_is_completed("false");
}
}
});
return convertView;
}
}
Note: One important thing, if your ListView contains items that has click events, then that items should be set to view.setFocusable(false); view.setFocusableInTouchMode(false); and view.setClickable(true);. We need this because ListView takes the click event of any of the view as its own click event. Also, the view's click event should be separated from ListView's Click event.
ListView recycles list item Views. So make sure to always call one.setImageResource().
if (MsgFav.contentEquals("YES")) {
one.setImageResource(R.drawable.favorpress);
} else {
one.setImageResource(R.drawable.default_drawable_as_specified_in_layout);
}

Data in ListActivity not cached when going back to it

I've got a ListActivity filled by an adapter extending SimpleCursorAdapter. Some category buttons can be clicked, which fills the listview with a list of articles in the corresponding category. When clicking on an item in the list a new activity is launched which shows the detail of the article.
Now, when pressing the back button in that article detail page, no data is shown. Does it have to reload the data from the DB? Isn't the data supposed to be cached?
Moreover, when going back to the myListActivity, onCreate is not called. Only onResume is called. Shall I do everything in onResume? In that case what's the best way to keep the value of the current category and retrieve it when going back to the ListActivity from the article detail?
Thanks
public class Home extends ListActivity implements OnItemClickListener{
private DBAdapter dbAdapter;
private HomeListAdapter adapter; //extends SimpleCursorAdapter
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
// open database
dbAdapter = new DBAdapter(this);
dbAdapter.open();
/* more stuff */
getArticlesbyCat(category);
/* more stuff */
}
private void getArticles(int category){
Cursor c = dbAdapter.fetchArtByCat(category);
c.moveToFirst();
startManagingCursor(c);
String[] from = new String[] {};
int[] to = new int[] {};
setListAdapter(new HomeListAdapter(this, R.layout.article_row, c, from, to));
adapter.notifyDataSetChanged();
c.close();
}
#Override
public void onItemClick(AdapterView arg0, View v, int position, long arg3) {
/* Go to article page */
}
#Override
protected void onPause() {
super.onPause();
// close database
dbAdapter.close();
}
}
EDIT
public class HomeListAdapter extends SimpleCursorAdapter {
private int mLayout;
private Cursor mCursor;
private Resources mResources;
// Column index
private int mCatInd;
private int mURLPicInd;
private int mIntroInd;
private int mTitleInd;
private LayoutInflater mLayoutInflater;
private final ImageDownloader mImageLoader;
private final class ViewHolder {
public TextView category;
public ImageView image;
public TextView intro;
public TextView title;
}
public HomeListAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
super(context, layout, c, from, to);
this.mLayout = layout;
this.mCursor = c;
this.mCatInd = mCursor.getColumnIndex(DBAdapter.KEY_CATEGORY_NAME);
this.mURLPicInd = mCursor.getColumnIndex(DBAdapter.KEY_URL_PIC);
this.mIntroInd = mCursor.getColumnIndex(DBAdapter.KEY_INTRO);
this.mTitleInd = mCursor.getColumnIndex(DBAdapter.KEY_TITLE);
this.mLayoutInflater = LayoutInflater.from(context);
mResources = context.getResources();
mImageLoader = new ImageDownloader(mResources,
((BitmapDrawable)mResources.getDrawable(R.drawable.default_row_pic)).getBitmap(), 1);
}
public View getView(int position, View convertView, ViewGroup parent) {
if (mCursor.moveToPosition(position)) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = mLayoutInflater.inflate(mLayout, null);
viewHolder = new ViewHolder();
viewHolder.category = (TextView) convertView.findViewById(R.id.cat);
viewHolder.image = (ImageView) convertView.findViewById(R.id.picture);
viewHolder.title = (TextView) convertView.findViewById(R.id.title);
viewHolder.intro = (TextView) convertView.findViewById(R.id.intro);
viewHolder.intro.setMaxLines(3);
convertView.setTag(viewHolder);
}
else {
viewHolder = (ViewHolder) convertView.getTag();
}
String category = mCursor.getString(mCatInd);
String title = mCursor.getString(mTitleInd);
String intro = mCursor.getString(mIntroInd);
String url_pic = mCursor.getString(mURLPicInd);
viewHolder.category.setText(category);
viewHolder.title.setText(title);
viewHolder.intro.setText(intro);
if(!url_pic.equals("")){
mImageLoader.download(url_pic, (ImageView) viewHolder.image);
}
else {
viewHolder.image.setImageResource(R.drawable.default_row_pic);
}
}
return convertView;
}
}

Categories

Resources