android-smart-image-view icon indicating copy to clipboard operation
android-smart-image-view copied to clipboard

Display wrong image

Open tai1135 opened this issue 13 years ago • 9 comments

I have a list of contacts. In listView's Adapter, i check if contact have image not null or empty.

  • If yes -> display default image with : contactImage.setImageResource(R.drawable.nophoto);
  • If no -> display image with contact's image : contactImage.setImageUrl(image);

But for the case when Image is empty or null, it display another image in the list. (Not the default one).

Here is the code :

if(image == null || image.isEmpty()) { contactImage.setImageResource(R.drawable.nophoto); } else { contactImage.setImageUrl(image); }

Can you help me this ? Thanks a lots.
Untitled

tai1135 avatar Jan 10 '13 10:01 tai1135

Hi tai1135,

You should post whole adapter code because snippet you provided is not enough.

Regards, Dragan

dmarjanovic avatar Jan 11 '13 14:01 dmarjanovic

This is code for Adapter :

package toxter.com.component;

import java.util.ArrayList; import com.loopj.android.image.SmartImageView;

import toxter.com.R; import toxter.com.common.AppConfig; import toxter.com.model.Contact; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.Filter; import android.widget.TextView;

public class ListContactAdapter extends ArrayAdapter<Contact>{ ArrayList<Contact> currentList; ArrayList<Contact> origin_array; int resource; Context context;

public ListContactAdapter(Context context, int textViewResourceId, ArrayList<Contact> objects) {
    super(context, textViewResourceId, objects);
    this.context = context;
    resource = textViewResourceId;
    currentList = objects;
    origin_array = (ArrayList<Contact>) currentList.clone();
}

@Override
public Filter getFilter() {
    return arrayFilter;
}

Filter arrayFilter = new Filter() {

    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        FilterResults filterResults = new FilterResults();

        ArrayList<Contact> data;
        if(constraint != null && constraint.length() != 0) {
            data = new ArrayList<Contact>();
            for (Contact contact : origin_array) {
                if((contact.first_name != null && contact.first_name.toLowerCase().contains(constraint)) || (contact.last_name != null && contact.last_name.toLowerCase().contains(constraint)) || (contact.email != null && contact.email.toLowerCase().contains(constraint))) {
                    data.add(contact);
                }
            }
        } else {
            data = origin_array;
        }

        filterResults.values = data;
        filterResults.count = data.size();

        return filterResults;
    }

    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {
        ArrayList<Contact> filteredList = (ArrayList<Contact>) results.values;
        if(results != null && results.count > 0) {
            clear();
            for (Contact c : filteredList) {
                add(c);
            }
            notifyDataSetChanged();
        }
    }

};

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View contactView = convertView;

    if (contactView == null) {
        contactView = new ContactView(getContext());
    }

    final Contact contact = currentList.get(position);

    //Display infor in view
    if (contact != null) {
        TextView lblContactName = ((ContactView) contactView).lblContactName;
        TextView lblContactEmail = ((ContactView) contactView).lblContactEmail;
        SmartImageView imgContactImage = ((ContactView) contactView).imgContactImage;

        lblContactName.setText(AppConfig.GetContactFullName(contact.first_name ,contact.last_name));
        lblContactEmail.setText(contact.email);

        String image = "";
        if(contact.image == null || contact.image.isEmpty()) {
            try {
                Integer.parseInt(contact.fb_image);
            } catch (Exception e) {
                image = contact.fb_image ;
            }
        } else {
            image = contact.image;
        } 

        if(image == null || image.isEmpty()) {
            imgContactImage.setImageResource(R.drawable.nophoto);
        } else {
            imgContactImage.setImageUrl(image);
        }


        //set onClick for item
        contactView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Activity activity = (Activity)context;
                Intent returnIntent = new Intent();
                returnIntent.putExtra("contact", AppConfig.gson.toJson(contact));
                activity.setResult(Activity.RESULT_OK,returnIntent);
                activity.finish();
            }
        });
    }

    return contactView;
}    

}

tai1135 avatar Jan 14 '13 16:01 tai1135

I guess you have valid nophoto.png in your /drawable directory.

Have you checked with debugger or by logging if this line is ever executed imgContactImage.setImageResource(R.drawable.nophoto) ?

You should always handle all cases in getView() even when contact is null - in that case you should set TextViews to empty string and hide ImageView. In case you don't there will be shown items with some random name/email/image even if contact is null.

dmarjanovic avatar Jan 14 '13 23:01 dmarjanovic

  • First it executed line : imgContactImage.setImageResource(R.drawable.nophoto).
  • Second is i saw this line executed 4 or more times just for 1 view (1 contact).
  • And the picture still wrong.
  • An additional is first time it good, but when i reload the list (PullToRefreshList) it take the wrong image.

Do you have any ideas ? Thanks a lots.

tai1135 avatar Jan 15 '13 09:01 tai1135

Was this issue ever resolved?

TeknoloGenie avatar Aug 20 '13 00:08 TeknoloGenie

@TeknoloGenie : Not yet, long time not work on it.

tai1135 avatar Aug 20 '13 02:08 tai1135

I resolved the issue we were having. Same outcome as you, not sure if the same issue, but i'll post on this tomorrow with a pull request.

TeknoloGenie avatar Aug 20 '13 10:08 TeknoloGenie

We solved this by assigning a default drawable at view, it is kinda hack but never had problem ever since.

BuddhaSource avatar Aug 20 '13 11:08 BuddhaSource

@TeknoloGenie : Have you posted the Solution for this? can you please share it on this thread?

two solutions I found for it were,

  1. adding ".refreshDrawableState();" on the view. this makes the image appear and disappear. which makes it not a good solution.
  2. Always creating a new ContactView() (as in above example). But this solution makes the Listview very Slow.

Please share any better robust solution. Thanks!

azhar-bandri avatar Jan 25 '14 08:01 azhar-bandri