3

I have a very simple Activity that extends ListActivity. I am overriding the method onListItemClick do perform some custom operations.

What I have seen in the logs is that the adapter methode getView is called after I click a list item (which I am overriding too in order to get my list made up by custom views).

Now, I would like to know if this is the correct behavior or not. If it is I might have a problem.

The problem is that my list items have images, those are fetched from web and when I click on a list item, the call to the adapter causes calls to the web refreshing the images in the list and messing them up fro some reason.

Can anyone shade some light?

this is my getView:

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ContentListItemView cv = null;
        if (convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(this.context);
            convertView = (RelativeLayout) inflater.inflate(this.layout, null);
            cv = new ContentListItemView(convertView);
        } else {
            cv = (ContentListItemView) convertView.getTag();
        }
        Log.d(this.getClass().getSimpleName(), "position: " + position);
        cv.init(getItem(position));
        convertView.setTag(cv);
        return convertView;
    }

and this is my OnListItemClick

protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        //Log.d(this.getClass().getSimpleName(), position + " " + id);
        Intent contentDetailsIntent = new Intent(this, ContentDetailsActivity.class);
        contentDetailsIntent.putExtra("com.tamtamy.jatta.content_list_item_selected", position);
        contentDetailsIntent.putExtra("com.tamtamy.jatta.datasource", ContentDetailsActivity.CONTENT_LIST);
        contentDetailsIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(contentDetailsIntent);
    }
0m4r
  • 1,036
  • 1
  • 14
  • 25
  • 2
    An adapter is an object. Objects are not called. Methods are called. I suggest that you amend your question to fix your "adapter is called" statements to be absolutely positively precise about what method is being called on your adapter. – CommonsWare Feb 16 '11 at 23:36
  • I did, is it more clear now? can you help me? – 0m4r Feb 17 '11 at 08:13

1 Answers1

1

If it's happening then it's not really relevant if it's correct or not, it's something you have to deal with.

My suggestion would be to locally cache the images in your apps cache directory so you don't need to fetch them over the web each time.

The other thing to note is that list item views do get recycled, so your application should not assume that a ContentListItemView passed before the image is fetched is still the same row after the image has been downloaded. Making this incorrect assumption is usually the cause of incorrect images in ListView rows.

Have a look at the multi-threading blog post on Googles Android developers blog which discusses image downloading and has a link to an example on Google code which should give you a good example of how to fix your image loading problems.

Al Sutton
  • 3,904
  • 1
  • 22
  • 17