简体   繁体   English

Android ListActivity onListItemClick调用适配器getView

[英]Android ListActivity onListItemClick calls adapter getView

I have a very simple Activity that extends ListActivity. 我有一个非常简单的Activity,它扩展了ListActivity。 I am overriding the method onListItemClick do perform some custom operations. 我重写方法onListItemClick执行一些自定义操作。

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). 我在日志中看到的是,在我单击列表项之后调用了适配器方法getView (为了使我的列表由自定义视图组成,我也要覆盖它)。

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. 问题是我的列表项有图像,这些图像是从网上获取的,当我点击列表项时,对适配器的调用会导致调用Web刷新列表中的图像并因某些原因弄乱它们。

Can anyone shade some light? 有人可以遮挡一些光吗?

this is my getView : 这是我的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 这是我的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);
    }

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. 我的建议是在您的应用程序缓存目录中本地缓存图像这样您就不需要每次都通过Web获取它们。

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. 另外需要注意的是列表项视图确实被回收,因此您的应用程序不应该假定在获取图像之前传递的ContentListItemView在下载图像后仍然是同一行。 Making this incorrect assumption is usually the cause of incorrect images in ListView rows. 进行这种不正确的假设通常是ListView行中图像不正确的原因。

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. 看看Googles Android开发者博客上讨论图像下载的多线程博客文章,并提供了一个关于Google代码示例的链接,它可以为您提供如何修复图像加载问题的良好示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM