列表视图命令行随机变化上的ListView android的滚动 [英] Listview rows order changes randomly on scroll of listview in android

查看:111
本文介绍了列表视图命令行随机变化上的ListView android的滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ListView控件和一个BaseAdapter.I正在下载的互联网服务和显示器上listview.It产品正确显示在列表视图。

I have one ListView and one BaseAdapter.I am downloading Products from webservice and display on listview.It displays properly on listview.

但是,当我滚动ListView控件在随机位置显示的行意味着有时第三行显示在第一的位置,中间位置排显示器上最后一个位置等等。

But when I scroll ListView displays rows in random position means sometimes third row display in first position, middle position row display on last position and so on.

这是我的适配器类

public class ProductListAdapter extends BaseAdapter implements OnClickListener{

    LayoutInflater inflater;
    ArrayList<Product> arrProducts;
    Context c;

    public ProductListAdapter(Context c, ArrayList<Product> arrProducts) {
        super();
        inflater = LayoutInflater.from(c);
        this.arrProducts = arrProducts;
        this.c = c;
    }

    @Override
    public int getCount() {
        return arrProducts.size();
    }

    @Override
    public Object getItem(int position) {
        return arrProducts.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View v, ViewGroup parent) {
        Product product = null;
        if(v == null) {
            v = inflater.inflate(R.layout.product_listview_row, null);
            product = (Product) getItem(position);
            v.setTag(product);
        }else {
            product = (Product)v.getTag();
        }

        v.setOnClickListener(this);

        TextView tvProductname = (TextView)v.findViewById(R.id.tvProductname);
        tvProductname.setText(product.getTitle());

        String strReviewCount = product.getReviews();
        TextView tvReviewsCounts = (TextView)v.findViewById(R.id.tvReviews);
        if(strReviewCount != null) tvReviewsCounts.setText(strReviewCount +" Reviews");

        Button btnPrice = (Button)v.findViewById(R.id.btnPrice);
        btnPrice.setText(product.getSellingPrice());

        return v;
    }

    @Override
    public void onClick(View v) {
        Product product = (Product) v.getTag();
        Intent i = new Intent(c, ProductDetails.class);
        i.putExtra(Product.PROD_ID, product.getId());
        startActivity(i);
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    @Override
    public int getViewTypeCount() {
        return super.getViewTypeCount();
    }
}

这是对的ListView XML布局

<ListView android:id="@+id/lstProducts" style="@style/fill_parent"
        android:dividerHeight="1dp" android:divider="#dbd3c5"
        android:scrollbars="vertical" android:layout_below="@id/layLine"></ListView>

BaseAdapter的同时,当我修改 getView()这样的方法这是工作的罚款

Also when I change getView() method of BaseAdapter like this it is working fine

@Override
    public View getView(int position, View v, ViewGroup parent) {
        Product product = null;
        v = inflater.inflate(R.layout.product_listview_row, null);
        product = (Product) getItem(position);
        v.setTag(product);
        v.setOnClickListener(this);

        TextView tvProductname = (TextView)v.findViewById(R.id.tvProductname);
        tvProductname.setText(product.getTitle());

        String strReviewCount = product.getReviews();
        TextView tvReviewsCounts = (TextView)v.findViewById(R.id.tvReviews);
        if(strReviewCount != null) tvReviewsCounts.setText(strReviewCount +" Reviews");

        Button btnPrice = (Button)v.findViewById(R.id.btnPrice);
        btnPrice.setText(product.getSellingPrice());

        return v;
    }

但在这种类型的问题是,我展示产品列表视图行,我here.By使用这种类型的code没有考虑图像的 getView()方法总是创造新的景观,当曾经的ListView 的滚动,使我不得不重新下载图像和again.I使用 ImageLoader的类,它是使用现金内存下载图像,但问题是,当我设置图像的ImageView很多时候它给我内存不足的错误

But problem in this type is I am displaying image of product on listview row which I had not consider here.By using this type of code getView() method always create new View when ever scroll of listView so that I have to download image again and again.I used ImageLoader class which is using cash memory for download images but problem is that when I set image to ImageView many times it giving me out of memory error.

请帮我出这个问题。

感谢

推荐答案

我降落,因为链接到这里的右键的解决方案。

I landed here because of a link to the right solution.

这里的问题是,绑定你的产品一旦到视图当您创建一个新的视图。你应该把这种对每一个电话 getView

The problem here is that you bind your product once to the view when you create a new view. You should call this on every call of getView:

product = (Product) getItem(position);
v.setTag(product);

但是你可以叫 v.setOnClickListener(本)一旦你创建视图(它无论如何也不会改变)。

But you could call v.setOnClickListener(this) once you create the view (it won't change anyway).

这应该做的伎俩。该视图类型的实现就在这里被滥用。我推荐阅读<一个href="http://stackoverflow.com/questions/7738527/getting-an-issue-while-checking-the-dynamically-generated-checkbox-through-list-v/7739006#7739006">my回答这里 ...

This should do the trick. The view type implementation is just misused here. I recommend to read my answer here...

这篇关于列表视图命令行随机变化上的ListView android的滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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