本文介绍了滚动列表视图后,列表视图项的Android反复得到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的项目,而点击网格视图项目,我需要补充一点,在列表视图中,所以它是一个动态的one.But将显示在滚动重复项8至9个项目列表视图之后。
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
if (Product.mDiscountPercentageString != null
&& !Product.mDiscountPercentageString.isEmpty()) {
float floatValue1 = Float
.parseFloat(ProductCartCustomAdapter.mSubTotalamount);
float floatValue2 = Float
.parseFloat(Product.mDiscountPercentageString);
float mProductDiscountAmount = floatValue1
* (floatValue2 / 100);
ProductCartCustomAdapter.mDiscountAmountString = new DecimalFormat(
"##.##").format(mProductDiscountAmount);
Product.mDiscountAmount_tv.setText(mCurrencySymbol
+ mDiscountAmountString);
float mTotalAmount1 = floatValue1
- mProductDiscountAmount;
float mVatpercentage = Float
.parseFloat(Product.mVatPercentage);
// Calculate the Tax
Double mTaxamount = (double) (mTotalAmount1 * (mVatpercentage / 100));
mTaxAmount = new DecimalFormat("##.##")
.format(mTaxamount);
Double mTotal = mTaxamount + mTotalAmount1;
mTotalAmounttext = new DecimalFormat("##.##")
.format(mTotal);
}
// Else part of the Discount for all product cart
else {
float mVatpercentage = Float
.parseFloat(Product.mVatPercentage);
Double mTaxamount = (double) (mTotalAmount * (mVatpercentage / 100));
mTaxAmount = new DecimalFormat("##.##")
.format(mTaxamount);
// System.out.println("Amount Tax : " +
// mTaxAmount);
Double mTotal = mTaxamount + mTotalAmount;
mTotalAmounttext = new DecimalFormat("##.##")
.format(mTotal);
}
Product.mSubTotal_tv.setText("");
Product.mSubTotal_tv
.setText(ProductCartCustomAdapter.mSubTotalamount);
Product.mVatPercent_textview.setText("("
+ Product.mVatPercentage + ")" + "%");
Product.mTaxAmount_tv.setText(mCurrencySymbol
+ ProductCartCustomAdapter.mTaxAmount);
Product.mTotalAmount_tv.setText(mCurrencySymbol
+ ProductCartCustomAdapter.mTotalAmounttext);
int CartQuantity1 = mSelectedProductCustom.size();
int CartQuantity2 = mSelectedProductId.size();
int CartQuantity3 = CartQuantity1 + CartQuantity2;
Product.mCartNumber_tv.setText("Cart(" + CartQuantity3
+ ")");
}
}
cursor.close();
count++;
}
// Custom Cart Sale Method to display
else if (count >= mSelectedProductId.size()) {
// && countCustom < mSelectedProductCustom.size()
if (mSelectedProductCustom != null
&& mSelectedProductCustom.size() > 0) {
List<String> lstProductId = new ArrayList<String>(
mSelectedProductCustom.keySet());
List<String> lstProductId1 = lstProductId;
List<String> lstQuantity = new ArrayList<String>(
mSelectedProductCustom.values());
List<String> lstQuantity2 = lstQuantity;
String ss = lstQuantity.get(countCustom);
// System.out.println("ss custom :" + ss);
String[] separated = ss.split("\\|");
String amount = separated[0];
String qty = separated[1];
// System.out.println("a custom: " + amount);
// System.out.println("b custom: " + qty);
Float amt = Float.parseFloat(amount);
int mQty = Integer.parseInt(qty);
float mAmount = mQty * amt;
// String Amount1 = String.valueOf(mAmount);
String amount1 = new DecimalFormat("##.##").format(mAmount);
mViewHolder.tvTitle.setText(lstProductId.get(countCustom));
mViewHolder.tvAmount.setText(mCurrencySymbol + amount1);
mViewHolder.ivQtyIcon.setText(qty);
// System.out.println("mDiscountPercentageString : "+
// Product.mDiscountPercentageString);
// Method for Special Discount for All Items
if (Product.mDiscountPercentageString != null
&& !Product.mDiscountPercentageString.isEmpty()
&& Float.parseFloat(Product.mDiscountPercentageString) > 0) {
// System.out.println("mSubTotalamount 1: "
// + ProductCartCustomAdapter.mSubTotalamount);
float subtotal = Float
.parseFloat(ProductCartCustomAdapter.mSubTotalamount);
if (amount1 != null && !amount1.isEmpty()) {
subtotal = Float
.parseFloat(ProductCartCustomAdapter.mSubTotalamount)
+ Float.parseFloat(amount1);
ProductCartCustomAdapter.mSubTotalamount = String
.valueOf(subtotal);
}
// System.out.println("mSubTotalamount 2 - Discount: "
// + ProductCartCustomAdapter.mSubTotalamount);
ProductCartCustomAdapter.mSubTotalamount = new DecimalFormat(
"##.##").format(subtotal);
Product.mSubTotal_tv
.setText(ProductCartCustomAdapter.mSubTotalamount);
float floatValue1 = Float
.parseFloat(ProductCartCustomAdapter.mSubTotalamount);
float floatValue2 = Float
.parseFloat(Product.mDiscountPercentageString);
float mProductDiscountAmount = floatValue1
* (floatValue2 / 100);
mDiscountAmountString = new DecimalFormat("##.##")
.format(mProductDiscountAmount);
Product.mDiscountAmount_tv.setText("");
Product.mDiscountAmount_tv
.setText(mDiscountAmountString);
mTotalAmount = floatValue1 - mProductDiscountAmount;
}
float m = Float.parseFloat(amount1);
// Adding Total amount and particular amount on Item
mTotalAmount = mTotalAmount + m;
ProductCartCustomAdapter.mSubTotalamount = new DecimalFormat(
"##.##").format(mTotalAmount);
Product.mSubTotal_tv
.setText(ProductCartCustomAdapter.mSubTotalamount);
// Calculate Tax
float mVatpercentage = Float
.parseFloat(Product.mVatPercentage);
Double mTaxamount = (double) (mTotalAmount * (mVatpercentage / 100));
mTaxAmount = new DecimalFormat("##.##").format(mTaxamount);
// Adding subtotal and tax
Double mTotal = mTaxamount + mTotalAmount;
mTotalAmounttext = new DecimalFormat("##.##")
.format(mTotal);
// System.out.println("Amount tot : " +
// mTotalAmounttext);
Product.mVatPercent_textview.setText("("
+ Product.mVatPercentage + ")" + "%");
Product.mTaxAmount_tv.setText(mCurrencySymbol
+ ProductCartCustomAdapter.mTaxAmount);
Product.mTotalAmount_tv.setText(mCurrencySymbol
+ ProductCartCustomAdapter.mTotalAmounttext);
int CartQuantity1 = mSelectedProductCustom.size();
int CartQuantity2 = mSelectedProductId.size();
int CartQuantity3 = CartQuantity1 + CartQuantity2;
Product.mCartNumber_tv.setText("Cart(" + CartQuantity3
+ ")");
countCustom++;
}
}
}
// loginDataBaseAdapter.close();
return convertView;
}
// Holder Class
private class ViewHolder {
public RelativeLayout Layout;
TextView tvTitle, tvAmount, ivQtyIcon, tvTotalAmount, mTaxAmount_tv,
mSubTotal_tv, mTotalAmount_tv, tvProductId, tvCurrencySymbol,
tvCartItemDiscount, tvCartItemTax;
ImageView ivIcon;
}
}
先谢谢了。
推荐答案
超载尝试以下两个函数在ProductCartCustomAdapter类:
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.
这篇关于滚动列表视图后,列表视图项的Android反复得到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!