Textview滚动Listview后计数器值的变化?

时间:2016-06-04 06:02:53

标签: android listview android-adapter listadapter

问题是

我在listview中有产品列表,在增量/减量按钮后面有一个textview。如果我在列表视图的第一项上执行递增和递减,则在执行滚动时也会更改为其他列表视图位置。

到目前为止我所做的一切:

我无法解决我的问题。 我在最近三天遇到了这个问题。任何人请帮助我,我将非常感谢你。感谢

这是我的适配器类代码:

class SubProductsListAdapter extends BaseAdapter{

    private Context context;
  //  private ArrayList<String> list;
    DBHelper dbHelper;
    ArrayList<AddedProducts> arrayList;
    boolean exists;
    String addedQuant;
    String quant;
    String discounted;
    String finalPrice;
    URI uri;
    private List<SubProductData> subProductDataList = null;


    public SubProductsListAdapter(Context context, ArrayList<SubProductData> list){
        this.context = context;
        this.subProductDataList = list;
    }

    @Override
    public int getCount() {
        return subProductDataList.size();
    }

    @Override
    public Object getItem(int position) {
        return subProductDataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }





    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if(convertView == null){
            holder = new ViewHolder();
            dbHelper = new DBHelper(context);
            convertView = LayoutInflater.from(context).inflate(R.layout.sub_fragment_list_items,null,false);
            holder.ItemImage = (ImageView) convertView.findViewById(R.id.item_image);
            holder.txtItemName = (TextView) convertView.findViewById(R.id.item_name);
            holder.disCountedPrice = (TextView) convertView.findViewById(R.id.discounted_price);
            holder.finalPrice = (TextView) convertView.findViewById(R.id.final_price);
            holder.quantity = (TextView) convertView.findViewById(R.id.quantity);
            holder.addedQuantity = (TextView) convertView.findViewById(R.id.item_quantity);
            holder.addItem = (Button) convertView.findViewById(R.id.add);
            holder.removeItem = (Button) convertView.findViewById(R.id.remove);
             holder.v = (Vibrator) this.context.getSystemService(Context.VIBRATOR_SERVICE);
                           convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }



        /**  set item name from the arrayList   */

        holder.txtItemName.setText(subProductDataList.get(position).getProductName());
        final String url = subProductDataList.get(position).getProductImage();
        String parentUrl = "http://test//";
        String finalUrl = parentUrl+url;



        try {
             uri  = new URI(finalUrl.replaceAll(" ", "%20"));
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }


        Picasso.with(context)
                    .load(String.valueOf(uri)) // loaded image
                .placeholder(R.drawable.categorydefault)     // thumbnail image
                .error(R.drawable.categorydefault)   // if unable to load image or fetch image from server
                    .into(holder.ItemImage);


        String productPrice = subProductDataList.get(position).getProductPrice();
        double myProductFianlPrice = Double.parseDouble(productPrice);
        myProductFianlPrice =Double.parseDouble(new DecimalFormat("##.####").format(myProductFianlPrice));

        holder.finalPrice.setText(String.valueOf(myProductFianlPrice));


        /**  Strike discounted price   */
        holder.disCountedPrice.setPaintFlags(holder.disCountedPrice.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);

         addedQuant = holder.addedQuantity.getText().toString();
         discounted = holder.disCountedPrice.getText().toString();
         finalPrice =  holder.finalPrice.getText().toString();



        /** Setting quantitiy of products from databse to textview  */

        final String itemName =subProductDataList.get(position).getProductName();
        arrayList = dbHelper.getAllProducts();
        exists = false;

        for (AddedProducts hmap : arrayList)
        {
            if (hmap.getTitle().equals(itemName))
            {
                exists = true;
                break;
            }}

        if (exists){

            Cursor id = dbHelper.getQuantityOfProduct(itemName);
            quant = null;
            id.moveToFirst();
            if (id.moveToFirst()) {
                quant   = id.getString(id.getColumnIndex("qty"));
                Log.e(quant,"this is my quantity");
            }
            holder.addedQuantity.setText(quant);
        }




        /**  Add items to the cart   */
        holder.addItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String present_value_string = holder.addedQuantity.getText().toString();
                int present_value_int = Integer.parseInt(present_value_string);
                present_value_int++;


                holder.addedQuantity.setText(String.valueOf(present_value_int));
                addedQuant = holder.addedQuantity.getText().toString();


                float tot = Float.parseFloat(finalPrice) * present_value_int;
                int dis = Integer.parseInt(discounted) * present_value_int;
                String itemName =subProductDataList.get(position).getProductName();
                String proIds = subProductDataList.get(position).getProductId();
                String proModel = subProductDataList.get(position).getProductModel();

                Log.e(proModel,"productModel");

                final String url = subProductDataList.get(position).getProductImage();
                String parentUrl = "http://falconet.co.in/jubstore/image/";
                String finalUrl = parentUrl+url;



                try {
                    uri  = new URI(finalUrl.replaceAll(" ", "%20"));
                } catch (URISyntaxException e) {
                    e.printStackTrace();
                }

                boolean test =  dbHelper.CheckIsDataAlreadyInDBorNot(itemName);


                if (!test)
                {
                    dbHelper.insertProduct(context,itemName,proIds,finalPrice, discounted,present_value_int,proModel,String.valueOf(uri),tot,dis);

                }
                else
                {
                    //item already exists
                    Cursor id = dbHelper.getQuantityData(itemName);

                    String myId = null;
                    id.moveToFirst();
                    if (id.moveToFirst()) {
                        myId   = id.getString(id.getColumnIndex("id"));
                        Log.e(myId,"this is my id");

                    }

                    float tots = Float.parseFloat(finalPrice) * present_value_int;
                    dbHelper.updateProduct(Integer.parseInt(myId),present_value_int,tots);
                    Log.e(String.valueOf(tots),"updatedtotalcheck");


                }


            }
        });

       /**  remove items from the cart   */
        holder.removeItem.setOnClickListener(new View.OnClickListener() {
            @TargetApi(Build.VERSION_CODES.KITKAT)
            @Override
            public void onClick(View v) {
                String present_value_string = holder.addedQuantity.getText().toString();
                int present_value_int = Integer.parseInt(present_value_string);
                if (present_value_int > 0) {
                    present_value_int--;
                    holder.addedQuantity.setText(String.valueOf(present_value_int));
                    holder.v.vibrate(300);

                    String itemName =subProductDataList.get(position).getProductName();
                    boolean test =  dbHelper.CheckIsDataAlreadyInDBorNot(itemName);

                    if (test)
                    {

                        //item already exists
                        Cursor id = dbHelper.getQuantityData(itemName);

                        String myId = null;
                        id.moveToFirst();
                        if (id.moveToFirst()) {
                            myId   = id.getString(id.getColumnIndex("id"));
                            Log.e(myId,"this is my id");
                        }
                        float tots = Float.parseFloat(finalPrice) * Integer.parseInt(addedQuant);
                        dbHelper.updateProduct(Integer.parseInt(myId),present_value_int,tots);

                    }
                }


                /** if textview quantity is equal to zero remove product from cart    */

                if (holder.addedQuantity.getText().toString() == "0"){


                    Cursor id = dbHelper.getQuantityData(itemName);
                    String myId = null;
                    id.moveToFirst();
                    if (id.moveToFirst()) {
                        myId   = id.getString(id.getColumnIndex("id"));
                        SubProductsListAdapter.this.notifyDataSetChanged();
                    }
                    if (myId != null) {
                        dbHelper.deleteProduct(Integer.valueOf(myId));

                    }
                }


            }});

        return convertView;
    }

    class ViewHolder{
        Button removeItem;
        Button addItem;
        TextView disCountedPrice,finalPrice,quantity,addedQuantity;
        ImageView ItemImage;
        TextView txtItemName;
        Vibrator v;
    }
}

1 个答案:

答案 0 :(得分:0)

在持有人类中添加一个变量,用于存储数量值并在其中设置最后数量。

class ViewHolder{
      int  quanity
    }

存储该数量

 holder.quantity = your quantity

现在您可以从持有人那里获取最后一个数量值并将其放入您的文本视图。

  /**  set item name from the arrayList   */

        holder.txtItemName.setText(subProductDataList.get(position).getProductName());
holder.txtQuanity.setText(holder.quantity)

希望这会有所帮助。