loopj / android-smart-image-view

Android ImageView replacement which allows image loading from URLs or contact address book, with caching
http://loopj.com/android-smart-image-view/
1.3k stars 510 forks source link

Display wrong image #11

Open tai1135 opened 11 years ago

tai1135 commented 11 years ago

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

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

dmarjanovic commented 11 years ago

Hi tai1135,

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

Regards, Dragan

tai1135 commented 11 years ago

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{ ArrayList currentList; ArrayList 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;
}    

}

dmarjanovic commented 11 years ago

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.

tai1135 commented 11 years ago

Do you have any ideas ? Thanks a lots.

TeknoloGenie commented 11 years ago

Was this issue ever resolved?

tai1135 commented 11 years ago

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

TeknoloGenie commented 11 years ago

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.

BuddhaSource commented 11 years ago

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

azhar-bandri commented 10 years ago

@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!