滚动列表视图时,列表视图中项目的位置是否会发生变化?

时间:2015-05-23 05:25:29

标签: android android-layout android-listview

我的应用程序有一个列表视图,列表视图的行包含imageView和Textview。当我在listview上下来时,我遇到了关于图像重新下载和其他数据的问题。

Listview中有一些行。当我滚动列表视图时,我的列表项的所有数据都会更改。因此它不会显示需要显示的值或行。

我的适配器代码就是这个......

public class CartAdapter extends BaseAdapter {

Context context;
public ArrayList<CartList> arr_cart;

private static LayoutInflater inflater = null;

public CartAdapter(Context context, ArrayList<CartList> arr_cart) {
    // TODO Auto-generated constructor stub
    this.context = context;
    this.arr_cart = arr_cart;
    inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

private static class ViewHolder {
    private static TextView tv_Title, tv_Price, tv_SellingPrice,
            tv_ShippingCharge, tv_TotalPrice;
    private static ImageView iv_Cancel, iv_ProductImage;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return arr_cart.size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

@SuppressWarnings("static-access")
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    ViewHolder holder;
    double dNationalShippingCost, dInternationalShippingCost, dSellingPrice, TotalCost;
    String totalString = null, sShippingCost = null, sNationalShippingCost, sInternationalShippingCost, sSellingPrice;

    if (convertView == null) {
        holder = new ViewHolder();
        convertView = inflater.inflate(R.layout.cart_item, null);

        holder.iv_ProductImage = (ImageView) convertView
                .findViewById(R.id.iv_ProductImage);
        holder.tv_Title = (TextView) convertView
                .findViewById(R.id.tv_Title);
        holder.tv_Price = (TextView) convertView
                .findViewById(R.id.tv_Price);
        holder.tv_SellingPrice = (TextView) convertView
                .findViewById(R.id.tv_SellingPrice);
        holder.tv_ShippingCharge = (TextView) convertView
                .findViewById(R.id.tv_ShippingCharge);
        holder.tv_TotalPrice = (TextView) convertView
                .findViewById(R.id.tv_TotalPrice);
        holder.iv_Cancel = (ImageView) convertView
                .findViewById(R.id.iv_Cancel);
        holder.iv_Cancel.setTag(position);
        holder.iv_Cancel.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(context, "Delete button Clicked",
                        Toast.LENGTH_LONG).show();
                // Integer index = (Integer) convertView.getTag();
                arr_cart.remove(arr_cart.get(position));
                notifyDataSetChanged();
            }
        });

        convertView.setTag(holder);
    } else {
        // the getTag returns the viewHolder object set as a tag to the view
        holder = (ViewHolder) convertView.getTag();
    }

    String[] image = arr_cart.get(position).getImage().split(",");
    String firstImage = image[0];
    Log.e("first Image", "" + firstImage);
    if (firstImage.equals("")) {

    } else {
        Picasso.with(this.context).load(firstImage)
                .into(holder.iv_ProductImage);
    }
    holder.tv_Title.setText(arr_cart.get(position).getAddTitle());
    holder.tv_Price.setText(arr_cart.get(position).getAddPrice());
    holder.tv_SellingPrice.setText(arr_cart.get(position)
            .getAddSellingPrice());

    try {
        // get cost in String
        sSellingPrice = arr_cart.get(position).getAddSellingPrice();
        sNationalShippingCost = arr_cart.get(position)
                .getAddNationalShippingCharge();
        sInternationalShippingCost = arr_cart.get(position)
                .getAddWorldShippingCharge();

        // convert in to double
        dSellingPrice = Double.parseDouble(sSellingPrice);
        dNationalShippingCost = Double.parseDouble(sNationalShippingCost);
        dInternationalShippingCost = Double
                .parseDouble(sInternationalShippingCost);

        // total Shipping Cost
        // Log.e("national chage in double", "" + dNationalShippingCost);
        // Log.e("international chage in double", ""
        // + dInternationalShippingCost);

        TotalCost = dInternationalShippingCost + dSellingPrice;
        // Log.e("total in double", "" + TotalCost);

        // convert in to string
        sShippingCost = Double.toString(dInternationalShippingCost);
        totalString = Double.toString(TotalCost);

    } catch (NumberFormatException e) {
    }

    holder.tv_ShippingCharge.setText(sShippingCost);
    // Log.e("Total shhiping chager str", totalString);
    holder.tv_TotalPrice.setText(totalString);

    return convertView;
}

1 个答案:

答案 0 :(得分:3)

删除static的{​​{1}}说明符 尝试替换此

ViewHolder

private static class ViewHolder {
    private static TextView tv_Title, tv_Price, tv_SellingPrice,
            tv_ShippingCharge, tv_TotalPrice;
    private static ImageView iv_Cancel, iv_ProductImage;
}