Textview中的自定义列表视图通过滚动

时间:2016-04-18 05:30:44

标签: android

这是我的适配器问题是滚动listview来改变textview值,每个位置如何解决它?加上单击事件以将一个和减号事件递增到decrment并在textview中设置值(加上点击数量+ 1,减去点击数量 - 1)。

   public class CustomListViewDrycleaning extends BaseAdapter {
    ArrayList<ProductModel> myList = new ArrayList<ProductModel>();
    LayoutInflater inflater;
    Context context;
    int loader = R.drawable.loader;
    int minteger = 0;
    private ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener();
    String rem, b;
    private DisplayImageOptions options;
    ProductModel currentListData;
    String cid, qcount;
    public CustomListViewDrycleaning(Context context, ArrayList<ProductModel> list) {
        this.myList = list;
        this.context = context;
        inflater = LayoutInflater.from(context);
        options = new DisplayImageOptions.Builder().showImageOnLoading(R.drawable.ic_launcher)              .showImageForEmptyUri(R.drawable.ic_launcher).showImageOnFail(R.drawable.ic_launcher)               .cacheInMemory(true).cacheOnDisk(true).considerExifParams(true).build();
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return myList.size();
    }
    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return myList.get(position);
    }
    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }
    @Override
    public int getViewTypeCount() {
        return 1;
    }
    @Override
    public int getItemViewType(int position) {
        return position;
    }
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        final MyViewHolder mViewHolder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.customproductlistdrycleaning, parent, false);
            mViewHolder = new MyViewHolder(convertView);
            convertView.setTag(mViewHolder);
        } else {
            mViewHolder = (MyViewHolder) convertView.getTag();
        }
        currentListData = myList.get(position);
        mViewHolder.btndropdown.setTag(currentListData.getCategoryId());
        mViewHolder.name.setText(currentListData.getName());
        mViewHolder.prize.setText("$" + currentListData.getCharge());
        mViewHolder.name.setTag(currentListData.getCategoryId());
        mViewHolder.plus.setTag(currentListData.getCategoryId());
        mViewHolder.minus.setTag(currentListData.getCategoryId());
        String img_path = currentListData.getImage();
        ImageLoader.getInstance().displayImage(img_path, mViewHolder.imgbucket, options, animateFirstListener);
        String servicecheck1 = currentListData.getServiceId1();
        String servicecheck2 = currentListData.getServiceId2();
        String servicecheck3 = currentListData.getServiceId3();
        if (servicecheck1 == null) {
            mViewHolder.btndropdown.setVisibility(View.GONE);
        } else {
            mViewHolder.btndropdown.setVisibility(View.VISIBLE);
        }
        mViewHolder.btndropdown.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                mViewHolder.lnrbelowdry.setVisibility(View.VISIBLE);
                mViewHolder.btndropdown.setVisibility(View.GONE);
                mViewHolder.btndropdown1.setVisibility(View.VISIBLE);
            }
        });
        mViewHolder.btndropdown1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                mViewHolder.lnrbelowdry.setVisibility(View.GONE);
                mViewHolder.btndropdown.setVisibility(View.VISIBLE);
                mViewHolder.btndropdown1.setVisibility(View.GONE);
            }
        });
        if (servicecheck1 == null) {
            mViewHolder.btndropdown.setVisibility(View.GONE);
            mViewHolder.lnrproduct1.setVisibility(View.GONE);
        } else {
            mViewHolder.btndropdown.setVisibility(View.VISIBLE);
            mViewHolder.lnrproduct1.setVisibility(View.VISIBLE);
            mViewHolder.checkBox1.setText(currentListData.getServiceName1());
            mViewHolder.txtproductprize1.setText("$" + currentListData.getServiceCharge1());
        }
        if (servicecheck2 == null) {
            mViewHolder.lnrproduct2.setVisibility(View.GONE);
        } else {
            mViewHolder.lnrproduct2.setVisibility(View.VISIBLE);
        mViewHolder.checkBox2.setText(currentListData.getServiceName2());
            mViewHolder.txtproductprize2.setText("$" + currentListData.getServiceCharge2());
        }
        if (servicecheck3 == null) {
            mViewHolder.lnrproduct3.setVisibility(View.GONE);
        } else {
            mViewHolder.lnrproduct3.setVisibility(View.VISIBLE);
        mViewHolder.checkBox3.setText(currentListData.getServiceName3());
            mViewHolder.txtproductprize3.setText("$" + currentListData.getServiceCharge3());
        }
        qcount = currentListData.getQuantity();
        mViewHolder.plus.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Utils.COUNTCARTALIST.add(mViewHolder.name.getTag() + "");
                int quantityyp = 0;
                for (int m = 0; m < Utils.qtylist.size(); m++) {
                    String ii = mViewHolder.plus.getTag() + "";
                    Log.e("", "@ii" + ii);
                    if (mViewHolder.plus.getTag().equals(Utils.qtylist.get(m).get("categoryId"))) {
                        Toast.makeText(context, "Match", Toast.LENGTH_SHORT).show();
                        rem = Utils.qtylist.get(m).get("categoryId");
                        b = Utils.qtylist.get(m).get("quantity");
                        quantityyp = Integer.parseInt(b) + 1;
                        String c = Integer.toString(quantityyp);
                        mViewHolder.strcount.setText(c);
                        Utils.qtylist.remove(m);
                        HashMap<String, String> hashmaplus = new HashMap<String, String>();
                        hashmaplus.put("categoryId", rem);
                        hashmaplus.put("quantity", c);
                        Utils.qtylist.add(hashmaplus);
                        Log.e("", "@Utils.qtylistadd" + Utils.qtylist);
                        break;
                    }
                }
            }
        });
        mViewHolder.minus.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Utils.COUNTCARTALIST.remove(mViewHolder.name.getTag() + "");
                int quantityym = 0;
                for (int m = 0; m < Utils.qtylist.size(); m++) {
                    String ii = mViewHolder.minus.getTag() + "";
                    Log.e("", "@iiminus" + ii);
                    if (mViewHolder.minus.getTag().equals(Utils.qtylist.get(m).get("categoryId"))) {
                        rem = Utils.qtylist.get(m).get("categoryId");
                        b = Utils.qtylist.get(m).get("quantity");
                        Log.e("", "@QQQ" + b);
                        Log.e("", "@rem" + rem);
                        if (b.equals("0")) {
                        } else {
                            quantityym = Integer.parseInt(b) - 1;
                            String c = Integer.toString(quantityym);
                            mViewHolder.strcount.setText(c);
                            Utils.qtylist.remove(m);
                            HashMap<String, String> hashmapminus = new HashMap<String, String>();
                            hashmapminus.put("categoryId", rem);
                            hashmapminus.put("quantity", c);
                            Utils.qtylist.add(hashmapminus);
                            break;
                        }
                    }
                }
            }
        });
        return convertView;
    }
    private class MyViewHolder {
        TextView name, prize, strcount, txtproductprize1, txtproductprize2, txtproductprize3;
        Button cart, plus, minus, btndropdown, btndropdown1;
        ImageView imgbucket;
        LinearLayout lnrbelowdry, lnrproduct1, lnrproduct2, lnrproduct3;
        CheckBox checkBox1, checkBox2, checkBox3;
        public MyViewHolder(View item) {
            name = (TextView) item.findViewById(R.id.txtproductname);
            prize = (TextView) item.findViewById(R.id.txtprize);
            strcount = (TextView) item.findViewById(R.id.txtcount);
            imgbucket = (ImageView) item.findViewById(R.id.imgbucket);
            plus = (Button) item.findViewById(R.id.btnplus);
            minus = (Button) item.findViewById(R.id.btnminus);
            btndropdown = (Button) item.findViewById(R.id.btndropdown);
            btndropdown1 = (Button) item.findViewById(R.id.btndropdown1);
            lnrbelowdry = (LinearLayout) item.findViewById(R.id.lnrbelowdry);
            lnrproduct1 = (LinearLayout) item.findViewById(R.id.lnrproduct1);
            lnrproduct2 = (LinearLayout) item.findViewById(R.id.lnrproduct2);
            lnrproduct3 = (LinearLayout) item.findViewById(R.id.lnrproduct3);
            checkBox1 = (CheckBox) item.findViewById(R.id.checkBox1);
            checkBox2 = (CheckBox) item.findViewById(R.id.checkBox2);
            checkBox3 = (CheckBox) item.findViewById(R.id.checkBox3);
            txtproductprize1 = (TextView) item.findViewById(R.id.txtproductprize1);
            txtproductprize2 = (TextView) item.findViewById(R.id.txtproductprize2);
            txtproductprize3 = (TextView) item.findViewById(R.id.txtproductprize3);
        }
    }
    private static class AnimateFirstDisplayListener extends SimpleImageLoadingListener {
        static final List<String> displayedImages = Collections.synchronizedList(new LinkedList<String>());
        @Override
        public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
            if (loadedImage != null) {
                ImageView imageView = (ImageView) view;
                boolean firstDisplay = !displayedImages.contains(imageUri);
                if (firstDisplay) {
                    FadeInBitmapDisplayer.animate(imageView, 500);
                    displayedImages.add(imageUri);
                }
            }
        }
    }

}

1 个答案:

答案 0 :(得分:0)

您为每一行返回相同的ID。试试这个:

public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}