vendredi 22 mai 2015

Position of items in listview is changing when I scroll the listview?

My application has an listview and listview's rows consist of imageView and Textview. I had a problem about image redownloading and other data when I scoll up and down the listview.

There are some rows in Listview. All data of my list item is change when i scroll the listview.So it doesn't display the values or rows that needed to display.

My adapter code is liek this...

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

Aucun commentaire:

Enregistrer un commentaire