-3

my project contains the grid view in which i created the custom view. Custom view had minus button, text view and plus button. My intention is like that, when i press on minus or plus button, the value of text view get changes according to some point. Please tell me how to implement that. Thanks in advance.

sid
  • 157
  • 1
  • 1
  • 10

3 Answers3

0
String number = mTextView.getText()
int numberInt = Integer.parseInt(number);

IN plus button onClickListener

numberInt++

IN minus button onClickListener

numberInt--

and final

mTextView.setText(""+numberInt);
rajahsekar
  • 916
  • 1
  • 11
  • 25
0
TextView mCount = (TextView) findViewById(R.id.count);
Button plus = (Button) findViewById(R.id.plus);
Button minus = (Button) findViewById(R.id.minus);
plus.setOnClickListener(onClickListener);
minus.setOnClickListener(onClickListener);

private final View.OnClickListener onClickListener = new View.OnClickListener() {
  @Override
  public void onClick(View v) {
    switch (v.getId()) {
     case R.id.minus:
      mCount.setText(String.valueOf(Double.valueOf(mCount.getText().toString()) - 1));
      return;
     case R.id.plus:
      mCount.setText(String.valueOf(Double.valueOf(mCount.getText().toString()) + 1));
      return;
     default:
    }
  }
};
Kia
  • 124
  • 1
  • 1
  • 10
0

You can set OnClickListener on button inside your gridView adapter, like this:

 button.setOnClickListener( new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String data = mTextView.getText().toString();
            double number = Double.parseDouble( data );

            number++;    //if you want to increase it by 1
            //or
            number += someValue;   //if you want to increase it by some value
            mTextView.settext(String.valueOf( number ));
        }
    } );

similarly you can set click listener on another button to subtract the value, just replce

number++;   
//or
number += someValue;   

with

number--;    
//or
number -= someValue;  
Shivam995
  • 64
  • 1
  • 8
  • i added that custom view in grid View. so every view cell contains that custom view. My problem is, i want to fetch the value of particular view cell on which plus or minus get press – sid Apr 18 '17 at 07:07
  • set click listener inside your getView method of adapter `public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { convertView = inflater.inflate(R.layout.your_layout, null); } Button button = (Button) convertView.findViewById(R.id.grid_item); //set click OnClickListener here /*here you don't need to manually handle gridView item positions here.*/ return convertView; }` – Shivam995 Apr 18 '17 at 08:30
  • its works.. now i want to calculate the sum of all grid view cells values collectively. – sid Apr 18 '17 at 12:48
  • you can do it in your activity class like this: `for(int i=0; i – Shivam995 Apr 19 '17 at 04:04