Android的ListActivity onListItemClick电话适配器getView [英] Android ListActivity onListItemClick calls adapter getView

查看:180
本文介绍了Android的ListActivity onListItemClick电话适配器getView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的活动,扩展了ListActivity。 我重写onListItemClick做执行一些自定义操作的方法。

我所看到的日志中的适配器梅索德 getView 被称为后,我点击列表项(其中我重写也为了让我的名单发由自定义视图)。

现在,我想知道这是否是正确的行为或不。如果是我可能有问题。

现在的问题是,我的列表项的图像,这些都是从网上获取,当我点击一个列表项,调用适配器使调用网页刷新图像列表,搞乱起来来回某种原因

任何人都可以树荫一些轻?

这是我的 getView

  @覆盖
    公共查看getView(INT位置,查看convertView,ViewGroup中父){
        ContentListItemView CV = NULL;
        如果(convertView == NULL){
            LayoutInflater充气= LayoutInflater.from(this.context);
            convertView =(RelativeLayout的)inflater.inflate(this.layout,NULL);
            CV =新ContentListItemView(convertView);
        } 其他 {
            CV =(ContentListItemView)convertView.getTag();
        }
        Log.d(this.getClass()getSimpleName(),的位置:+位置);
        cv.init(的getItem(位置));
        convertView.setTag(CV);
        返回convertView;
    }
 

这是我的 OnListItemClick

 保护无效onListItemClick(ListView的L,视图V,INT位置,长的id){
        super.onListItemClick(L,V,位置ID);
        。//Log.d(this.getClass()getSimpleName(),位​​置++ ID);
        意图contentDetailsIntent =新的意图(这一点,ContentDetailsActivity.class);
        contentDetailsIntent.putExtra(com.tamtamy.jatta.c​​ontent_list_item_selected,位置);
        contentDetailsIntent.putExtra(com.tamtamy.jatta.datasource,ContentDetailsActivity.CONTENT_LIST);
        contentDetailsIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(contentDetailsIntent);
    }
 

解决方案

如果它发生,那么它不是真正相关的,如果它的正确与否,它的东西,你必须处理。

我的建议是在本地缓存的图片在您的应用程序缓存目录所以你不需要每次接他们在网上。

另外要注意的是,列表项的看法也得到回收,因此应用程序不应该假定ContentListItemView传递前的图像被取出仍是同一行的形象已被下载之后。制作这种不正确的假设通常是不正确的图像中的ListView行的原因。

有一个看多线程对谷歌的博客文章Android开发者博客,其中讨论了图像下载和有一个链接到谷歌上的code的例子应该给你如何解决你的图像加载问题的一个很好的例子。

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

解决方案

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.

这篇关于Android的ListActivity onListItemClick电话适配器getView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆