-2

`

custom_layout

<ImageView
    android:id="@+id/addition"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher"
    android:layout_alignParentRight="true"
    android:paddingRight="15dp"
    android:gravity="center"
   />

<TextView
    android:id="@+id/qun"
    android:layout_width="33dp"
    android:layout_height="33dp"
    android:layout_alignParentRight="true"
    android:gravity="center"
    android:layout_marginRight="60dp"
    android:layout_marginTop="9dp"
    android:background="@drawable/quantity_box"
    />
<ImageView
    android:id="@+id/subtraction"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/sub"
    android:layout_alignParentRight="true"
    android:gravity="center"
    android:layout_marginRight="100dp"/>

` here is the image what i'm getting
here is the code we have +button increase and -button decrease i want to get but i'm unable to get the the increase and decrease of all item except 1 items ... please help me as i have tried all thing .. thank you for your suggestion and help

public class MainActivity extends AppCompatActivity {
    ///Description
    ImageView add, edit;
    Button ok;
    Button next2;
    ImageView addition, subtraction;
    TextView qun;
    int count = 0;
    final Context context = this;
    ListView listView;
    private ArrayAdapter<String> adapter;
    private ArrayList<String> arrayList;
    String strCounter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main_activity);

        //initialization
        add = (ImageView) findViewById(R.id.add);
        edit = (ImageView) findViewById(R.id.edit);
        listView = (ListView) findViewById(R.id.list);
        next2 = (Button) findViewById(R.id.next2);


        final String items[] = {"toned milk"};
        arrayList = new ArrayList<>(Arrays.asList(items));
        adapter = new ArrayAdapter<String>(this, R.layout.custom_layout, R.id.text, arrayList);
        listView.setAdapter(adapter);
        registerForContextMenu(listView);//to register the object of list view for context menu


        add.setOnClickListener(new View.OnClickListener()

        {
            @Override
            public void onClick(View v) {
                final Dialog dialog = new Dialog(MainActivity.this);
                dialog.setTitle("Enter new Milk");
                dialog.setContentView(R.layout.dialog);
                dialog.setCancelable(false);// to prevent the user when he click  any where in the screen
                dialog.setCanceledOnTouchOutside(false);
                dialog.show();


                //button initilazation
                Button ok = (Button) dialog.findViewById(R.id.ok);

                ok.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //initialization for edit text
                        final EditText editText = (EditText) dialog.findViewById(R.id.pro);
                        String data = editText.getText().toString();

                        arrayList.add(data);

                        Toast.makeText(getApplicationContext(), "product name is :" + data, Toast.LENGTH_LONG).show();

                        adapter.notifyDataSetChanged();// to   refresh with update one

                        dialog.cancel();


                    }
                });


                addition = (ImageView) findViewById(R.id.addition);
                subtraction = (ImageView) findViewById(R.id.subtraction);
                qun = (TextView) findViewById(R.id.qun);

                addition.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (v == addition) {
                            count++;
                            strCounter = Integer.toString(count);
                            qun.setText(strCounter);

                        }
                    }
                });


                subtraction.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (v==subtraction){
                            if (count>0)
                            count--;
                            strCounter =Integer.toString(count);
                            qun.setText(strCounter);
                        }

                    }
                });

            }
        });
sebenalern
  • 2,515
  • 3
  • 26
  • 36

1 Answers1

0

You should set OnClickListener to your +buttons and -buttons of every ListView item. You can do this by Override the getView() method of ArrayAdapter and write you own implementation. eg:

@Override
public View getView(int position, View convertView, ViewGroup parent){
    if(convertView == null){
        // inflate your view
    }else{
        addition = (ImageView) convertView.findViewById(R.id.addition);
        subtraction = (ImageView) convertView.findViewById(R.id.subtraction);
        qun = (TextView) convertView.findViewById(R.id.qun);
    }
}

Please refer to an example here: Custom Adapter for List View

Community
  • 1
  • 1
Lei Guo
  • 2,550
  • 1
  • 17
  • 22