Android ListActivity onListItemClick calls adapter getView

流过昼夜 提交于 2020-01-01 06:56:18

问题


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);
    }

回答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.



来源:https://stackoverflow.com/questions/5023074/android-listactivity-onlistitemclick-calls-adapter-getview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!