Search code examples
androidlistviewandroid-buttonbaseadapter

Button's click in listiview not working properly


In my listview i set two buttons and one edittext but it is not working properly,the view of my buttons and edittext in listview is look like this

Button1 Edittext Button2

in my edittext i set 1,and Button1 is for decrement or minus and Button2 for increment or plus,but the issue is in every listitem it is not working properly,if i have three items in my listview,and if i increment in first item the operation is performing in third item,,

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        final View rootView = inflater.inflate(R.layout.list_view_addtocart, container, false);

        totvalues=(TextView)rootView.findViewById(R.id.totaldisp);

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        edtcopupon=(EditText)rootView.findViewById(R.id.edt_coupon);
        btn_applycoupon=(Button)rootView.findViewById(R.id.aplycoupoon);

        btn_applycoupon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });

        list=(ListView)rootView.findViewById(R.id.list_addtocart);
        plcordr=(Button)rootView.findViewById(R.id.plcordr);
        plcordr.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Placeorder tf = new Placeorder();
                Bundle bundle = new Bundle();
                tf.setArguments(bundle);
                android.app.FragmentManager fm = getActivity().getFragmentManager();
                android.app.FragmentTransaction ft = fm.beginTransaction();
                ft.replace(R.id.container_body, tf);
                ft.setTransition(android.support.v4.app.FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                ft.addToBackStack(null);
                ft.commit();
            }
        });

        ArrayList<ContactListItems> contactList = new ArrayList<ContactListItems>();
        contactList.clear();
        String query = "SELECT * FROM PHONE_CONTACTS ";
        Cursor c1 = SqlHandler.selectQuery(query);
        if (c1 != null & c1.getCount() != 0) {
            if (c1.moveToNext()) {
                do {
                    contactListItems = new ContactListItems();
                   contactListItems.setSlno(c1.getString(c1
                            .getColumnIndex("slno")));
                    contactListItems.setName(c1.getString(c1
                            .getColumnIndex("name")));
                    contactListItems.setPhone(c1.getString(c1
                            .getColumnIndex("phone")));
                    contactListItems.setSizes(c1.getString(c1
                            .getColumnIndex("sizes")));
                    contactListItems.setQtys(c1.getString(c1
                            .getColumnIndex("qtys")));
                    contactList.add(contactListItems);
                } while (c1.moveToNext());
            }
        }
        c1.close();

        contactListAdapter = new ContactListAdapter(getActivity(), contactList);
        list.setAdapter(contactListAdapter);


        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                // TODO Auto-generated method stub

            }
        });

        return rootView;
    }



    public class ContactListAdapter extends BaseAdapter {

        Context context;
        ViewHolder holder;
        ArrayList<ContactListItems> contactList;
        String dollars = "\u0024";

        public ContactListAdapter(Context context, ArrayList<ContactListItems> list) {

            this.context = context;
            contactList = list;
        }

        @Override
        public int getCount() {

            return contactList.size();
        }

        @Override
        public Object getItem(int position) {

            return contactList.get(position);
        }

        @Override
        public long getItemId(int position) {

            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {

            if (convertView == null) {
                holder = new ViewHolder();
                convertView = LayoutInflater.from(getActivity()).inflate(R.layout.list_item_addtocart, null);
                holder.propic = (ImageView) convertView.findViewById(R.id.propicaccept);

                holder.tvSlNo = (TextView) convertView.findViewById(R.id.txt_size);
                holder.tvName = (TextView) convertView.findViewById(R.id.proprice_addtocart);
                holder.tvPhone = (TextView) convertView.findViewById(R.id.proname_addtocart);
                holder.tvsizes = (TextView) convertView.findViewById(R.id.txt_sizes);
                holder._decrease = (Button)convertView.findViewById(R.id.minuss_addtocart);
                holder.mBTIncrement = (Button)convertView.findViewById(R.id.plus_addtocart);
                holder._value = (EditText)convertView.findViewById(R.id.edt_procount_addtocart);

                holder.mBTIncrement.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        //increment();
                        int mValue = Integer.parseInt(holder._value.getText().toString());
                        mValue++;
                        quntity++;
                        tots = pric * quntity;
                        System.out.println("INCRESE VALUES" + tots);
                        totvalues.setText(String.valueOf(tots));
                        holder._value.setText("" + mValue);
                    }
                });

                holder._decrease.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        //decrement();

                        int mValue = Integer.parseInt(holder._value.getText().toString());
                        mValue--;
                        quntity--;
                        tots = pric * quntity;
                        System.out.println("INCRESE VALUES" + tots);
                        totvalues.setText(String.valueOf(tots));
                        if (mValue < 0) {
                            System.out.println("not valid");
                            holder._decrease.setEnabled(false);
                        } else {
                            holder._value.setText("" + mValue);
                        }
                    }
                });

                convertView.setTag(holder);
            }else{
                holder = (ViewHolder) convertView.getTag();
            }

            holder.tvName.setText(dollars+contactListItems.getName());
            holder.tvPhone.setText(contactListItems.getPhone());
            holder.tvsizes.setText(contactListItems.getSizes());
            holder._value.setText(contactListItems.getQtys());

            quntity=Float.parseFloat(contactListItems.getQtys());

            pric=Float.parseFloat(contactListItems.getName());

            tots=pric*quntity;

            totvalues.setText(String.valueOf(tots));

            System.out.println("QTYSSS>>" + quntity);
            System.out.println("PRICCC>>" + pric);
            // float aditya=+tots;
            System.out.println("TOTTTT>>" + tots);




            //aQuery.id(holder.propic).image(listData.get(position).get(TAG_IMAGE),true,true,0,R.drawable.ic_launcher);

            return convertView;
        }
        class ViewHolder{
            ImageView propic;
            TextView tvSlNo;
            TextView tvName;
            TextView tvPhone;
            Button mBTIncrement;
            TextView tvsizes;
            Button _decrease;
            public EditText _value;
        }

    }

XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/rectback"

    >
    <ImageView
        android:layout_width="70dp"
        android:layout_height="100dp"
        android:id="@+id/img_addtocart"
        android:src="@drawable/productfirst"
        />


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/relfirsrt"
        android:layout_toRightOf="@+id/img_addtocart"

        android:layout_above="@+id/relativeLayout"
        android:layout_alignParentTop="true">


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/proname_addtocart"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:maxLength="13"
            android:textColor="@android:color/black"


            />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/txt_size"

            android:layout_centerVertical="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/txt_sizes"

            android:layout_centerVertical="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />


    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:layout_alignBottom="@+id/img_addtocart"
        android:layout_toRightOf="@+id/img_addtocart"
        android:layout_toEndOf="@+id/img_addtocart"
        android:id="@+id/relativeLayout">
        <Button
            android:layout_width="40dp"
            android:layout_height="29dp"
            android:id="@+id/minuss_addtocart"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:background="@drawable/minusbox"
            android:layout_alignParentTop="true"
            android:layout_toLeftOf="@+id/edt_procount_addtocart"
            android:layout_toStartOf="@+id/edt_procount_addtocart" />
        <EditText
            android:layout_width="40dp"
            android:layout_height="29dp"
            android:id="@+id/edt_procount_addtocart"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:background="@drawable/inputbox"
            android:inputType="number"
            android:gravity="center"
            android:text="0"
            android:layout_alignParentTop="true"
            android:layout_toLeftOf="@+id/plus_addtocart"
            android:layout_toStartOf="@+id/plus_addtocart" />
        <Button
            android:layout_width="40dp"
            android:layout_height="29dp"
            android:id="@+id/plus_addtocart"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:background="@drawable/addbox"
            android:layout_alignParentTop="true"
            android:focusable="true"

            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/proprice_addtocart"
            android:gravity="center_vertical"
            android:layout_centerVertical="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />


    </RelativeLayout>


</RelativeLayout>

Solution

  • Write your onClick listener and tag assigning code outside the if-else block.

     if (convertView == null) {
    
    }
    else {
    }
    
    holder.position = position;
    
      holder.mBTIncrement.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                //increment();
                                int mValue = Integer.parseInt(holder._value.getText().toString());
                                mValue++;
                                quntity++;
                                tots = pric * quntity;
                                System.out.println("INCRESE VALUES" + tots);
                                totvalues.setText(String.valueOf(tots));
                                holder._value.setText("" + mValue);
                            }
                        });
    
                        holder._decrease.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
    
                                //decrement();
    
                                int mValue = Integer.parseInt(holder._value.getText().toString());
                                mValue--;
                                quntity--;
                                tots = pric * quntity;
                                System.out.println("INCRESE VALUES" + tots);
                                totvalues.setText(String.valueOf(tots));
                                if (mValue < 0) {
                                    System.out.println("not valid");
                                    holder._decrease.setEnabled(false);
                                } else {
                                    holder._value.setText("" + mValue);
                                }
                            }
                        });