在我的项目中,在点击网格视图项目时,我需要在列表视图中添加它,因此它是动态的。但是在添加8到9个项目后,列表视图会在滚动时重复显示项目。
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;
}
}
提前致谢。
答案 0 :(得分:1)
尝试在ProductCartCustomAdapter类中重载以下两个函数:
getItemViewType()
getViewTypeCount()
超载后,它们可能看起来像:
@Override
public int getItemViewType(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public int getViewTypeCount() {
// TODO Auto-generated method stub
return getCount();
}
希望这可以解决您的问题。
答案 1 :(得分:0)
您正在使用ViewHolder,但没有以正确的方式使用
执行convertView.setTag(mViewHolder);
时
您必须通过执行操作告诉视图持有者哪个标记对应于哪个ID
,convertView.setTag(R.id.yourtextviewID, viewHolder.textView);
用于convertView.setTag(mViewHolder);
行之后使用的每个小部件,
现在重要的部分是在你做完
之后else {
mViewHolder = (ViewHolder) convertView.getTag();
}
您还必须将标签设置为正确的位置,以便项目可以正确显示
viewHolder.textView.setTag(position);
您使用的每个小部件 您的ViewHolder不是静态类,通常视图是静态类
static class ViewHolder
其他一切都是正确的,不需要notifyDatasetChanged
答案 2 :(得分:-1)
在添加/删除后尝试调用youradapter.notifyDataSetChanged()