Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

In my project,While clicking on grid view items, i need to add that in list view, So it to be a dynamic one.But After adding 8 to 9 items list view showing items repeatedly on scroll.

public class ProductCartCustomAdapter extends BaseAdapter {

private LayoutInflater inflater;
private Context context;
private int mPosition;
private ArrayList<String> arrayList;
public LoginDataBaseAdapter loginDataBaseAdapter;
private HashMap<Integer, Integer> mSelectedProductId = new HashMap<Integer, Integer>();
private HashMap<String, String> mSelectedProductCustom = new HashMap<String, String>();
private Bitmap scaledBitmap;
private int count = 0;
private int countCustom = 0;
public static Integer mQuantity;
public static String mSubTotalamount, mTaxAmount, mTotalAmounttext,
        mDiscountAmountString, mCurrencySymbol;
public static float mTotalAmount = 0;
public static HashMap<Integer, List<String>> mSelectedProductId1 = new HashMap<Integer, List<String>>();
private List<String> value = new ArrayList<String>();
private String mProductId;
// inside your custom Adapter

public static HashMap<String, Integer> mPidarrayList = new HashMap<String, Integer>();
public static HashMap<String, Integer> mQtyarrayList = new HashMap<String, Integer>();
public static HashMap<String, Integer> mDiscountarrayList = new HashMap<String, Integer>();
public static HashMap<String, Integer> mTaxarrayList = new HashMap<String, Integer>();

private static final int REGULAR = 0;
private static final int HEADER = 1;


// Constructor
public ProductCartCustomAdapter(Context applicationContext, int pos,
        ArrayList<String> arrayList2,
        HashMap<Integer, Integer> mSelectedProductId,
        HashMap<String, String> mSelectedProductCustom,
        HashMap<Integer, List<String>> mSelectedProductId1,
        HashMap<String, Integer> mPidarrayList,
        HashMap<String, Integer> mQtyarrayList,
        HashMap<String, Integer> mDiscountarrayList,
        HashMap<String, Integer> mTaxarrayList) {
    // TODO Auto-generated constructor stub

    this.context = applicationContext;
    inflater = LayoutInflater.from(this.context);
    this.mPosition = pos;
    this.arrayList = arrayList;

    this.mSelectedProductId = mSelectedProductId;
    this.mSelectedProductCustom = mSelectedProductCustom;
    this.mSelectedProductId1 = mSelectedProductId1;
    this.mPidarrayList = mPidarrayList;
    this.mQtyarrayList = mQtyarrayList;
    this.mDiscountarrayList = mDiscountarrayList;
    this.mTaxarrayList = mTaxarrayList;

    mTotalAmount = 0;

    // create a instance of SQLite Database
    loginDataBaseAdapter = new LoginDataBaseAdapter(context);
    loginDataBaseAdapter = loginDataBaseAdapter.open();

    Cursor cursor10 = loginDataBaseAdapter.RetrieveStoreInfo();
    if (cursor10 != null && cursor10.moveToFirst()) {

        mCurrencySymbol = cursor10.getString(cursor10
                .getColumnIndexOrThrow("CURRENCY_SYMBOL"));

    }

}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return mSelectedProductId.size() + mSelectedProductCustom.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;
}

// Binding the values fetch from arraylist
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub

    ViewHolder mViewHolder = null;
    String mProductTitle_Json = null, mProductPrice_Json = null;
    byte[] mProductImage_Json = null;


    if (convertView == null) {
        // convertView = inflater.inflate(R.layout.activity_products, null);
        convertView = inflater.inflate(R.layout.cartlistviewitems, null);

        mViewHolder = new ViewHolder();

        mViewHolder.tvAmount = (TextView) convertView
                .findViewById(R.id.CartItemAmount);
        mViewHolder.tvTitle = (TextView) convertView
                .findViewById(R.id.CartItemName);
        mViewHolder.tvCartItemDiscount = (TextView) convertView
                .findViewById(R.id.CartItemDiscount);
        mViewHolder.tvCartItemTax = (TextView) convertView
                .findViewById(R.id.CartItemTax);
        mViewHolder.tvProductId = (TextView) convertView
                .findViewById(R.id.ProductId);
        mViewHolder.ivIcon = (ImageView) convertView
                .findViewById(R.id.CartItemImage);
        mViewHolder.ivQtyIcon = (TextView) convertView
                .findViewById(R.id.CartQuantityImage);
        mViewHolder.Layout = (RelativeLayout) convertView
                .findViewById(R.id.productcart_listview_row_layout);

        mViewHolder.tvCurrencySymbol = (TextView) convertView
                .findViewById(R.id.currencysymbol);

        mViewHolder.tvCartItemDiscount.setVisibility(View.GONE);

        convertView.setTag(mViewHolder);
    } else {

        mViewHolder = (ViewHolder) convertView.getTag();
    }

    // List out the Product name , Quantity, Amount, Discount and Tax in
    // list view
    if ((count + countCustom) < mSelectedProductId.size()
            + mSelectedProductCustom.size()) {

        if (mSelectedProductId != null && mSelectedProductId.size() > 0
                && count < mSelectedProductId.size()) {

            System.out.println("*************************************");

            // System.out.println("mPos adapter: " + mPosition);

            // Copy the Products keyset and values in List
            List<Integer> lstProductId = new ArrayList<Integer>(
                    mSelectedProductId.keySet());
            List<Integer> lstQuantity = new ArrayList<Integer>(
                    mSelectedProductId.values());

            List<Integer> lstPId = new ArrayList<Integer>(
                    mPidarrayList.values());
            List<Integer> lstQty = new ArrayList<Integer>(
                    mQtyarrayList.values());

            List<Integer> lstDiscount = new ArrayList<Integer>(
                    mDiscountarrayList.values());
            List<Integer> lstTax = new ArrayList<Integer>(
                    mTaxarrayList.values());

            mQuantity = lstQty.get(count);



            Cursor cursor = loginDataBaseAdapter
                    .RetrieveProductInfoEntry(String.valueOf(lstPId
                            .get(count)));

            if (cursor != null && cursor.moveToFirst()) {

                mProductImage_Json = cursor.getBlob(cursor
                        .getColumnIndexOrThrow("PRODUCT_IMAGE"));
                mProductTitle_Json = cursor.getString(cursor
                        .getColumnIndexOrThrow("PRODUCT_TITLE"));
                mProductPrice_Json = cursor.getString(cursor
                        .getColumnIndexOrThrow("PRICE"));
                mProductId = cursor.getString(cursor
                        .getColumnIndexOrThrow("PRODUCT_ID"));

            // Scaling the image
                if (mProductImage_Json != null
                        && mProductImage_Json.length != 0) {
                    BitmapFactory.Options options = new BitmapFactory.Options();
                    options.inSampleSize = 3;
                    Bitmap bm = BitmapFactory.decodeByteArray(
                            mProductImage_Json, 0,
                            mProductImage_Json.length);
                    scaledBitmap = Bitmap.createScaledBitmap(bm, 25, 25,
                            true);

                    String Qty = String.valueOf(mQuantity);
                    try {
                        mViewHolder.ivIcon.setImageBitmap(scaledBitmap);
                        mViewHolder.tvTitle.setText(mProductTitle_Json);
                        mViewHolder.tvProductId.setText(mProductId);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    Float amt = Float.parseFloat(mProductPrice_Json);

                    float mAmount = mQuantity * amt;
                    // String Amount1 = String.valueOf(mAmount);
                    String amount = new DecimalFormat("##.##")
                            .format(mAmount);

                    int k = lstProductId.size();
                    int l = count;
                    int p = k - l;

                    // Method for Special Discount for particular Item
                    try {
                        if (!(lstDiscount.get(count) == 0)
                                && lstDiscount.get(count) != null) {

                            String Discount = lstDiscount.get(count)
                                    .toString();

                            mViewHolder.tvCartItemDiscount
                                    .setVisibility(View.VISIBLE);
                            mViewHolder.tvCartItemDiscount.setText(Discount
                                    + "%");

                        loat floatValue1 = Float.parseFloat(Discount);

                            float mProductDiscountAmount = mAmount
                                    * (floatValue1 / 100);

                        mAmount = mAmount - mProductDiscountAmount;

                            amount = new DecimalFormat("##.##")
                                    .format(mAmount);

                        }
                    } catch (Exception e) {
                        System.out
                                .println("Product Custom Adapter Spl Dis: "
                                        + e);
                    }

                    // Method for Special Tax for particular Item
                    try {

                        // if (lstTax.get(p - 1) == 1) {
                        if (lstTax.get(count) == 1) {

                            float mVatpercentage = Float
                                    .parseFloat(Product.mVatPercentage);

                            Double mTaxamount = (double) (mAmount * (mVatpercentage / 100));
                            mTaxAmount = new DecimalFormat("##.##")
                                    .format(mTaxamount);

                            mAmount = (float) (mAmount - Float
                                    .parseFloat(mTaxAmount));

                            amount = new DecimalFormat("##.##")
                                    .format(mAmount);

                            mViewHolder.tvCartItemTax
                                    .setText("Tax not applicable");

                        }
                    } catch (Exception e) {
                        System.out
                                .println("Product Custom Adapter Spl tax error: "
                                        + e);
                    }

                    try {
                        mViewHolder.tvAmount.setText(amount);
                        mViewHolder.ivQtyIcon.setText(Qty);
                        mViewHolder.tvCurrencySymbol
                                .setText(mCurrencySymbol);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    mTotalAmount = mTotalAmount + Float.parseFloat(amount);
                    mSubTotalamount = new DecimalFormat("##.##")
                            .format(mTotalAmount);

                // Method for Special Discount for All Items

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
112 views
Welcome To Ask or Share your Answers For Others

1 Answer

try overloading the following two functions in your ProductCartCustomAdapter class:

getItemViewType()

getViewTypeCount()

after overloading they may look like :

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

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

hope this would solve your problem.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...