0

When I click "present" button the "percentage" does not change while the entered values changes. After the first click, the value of the "percentage" changes. The same problem occur when I press the "missed" button. After the first click, when I pressed "present" button increment function works and for "missed" button decrement function works.

I want that when "button b" is pressed it calculates the "percentage". when a user press "present" it increases the values of both "e1 (attended) and e2 (total)" from the previous entered values and then calculates the "percentage". For "missed" button it increases the value of e2 only and then calculates the percentage.

EditText e1,e2;
Button b,save;
TextView t;
String attended,total,mysum;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    e1=(EditText) findViewById(R.id.editTextN1);
    e2=(EditText) findViewById(R.id.editTextN2);
    b=(Button) findViewById(R.id.buttoncalcsum);
    save= (Button) findViewById(R.id.save);
    t=(TextView) findViewById(R.id.textViewresult);
    SharedPreferences pre=getSharedPreferences("data",Context.MODE_PRIVATE);
    e1.setText(pre.getString("attended", ""));
    e2.setText(pre.getString("total", ""));
    t.setText(pre.getString("mysum", ""));

    b.setOnClickListener(new OnClick());
}

public class OnClick implements OnClickListener 
{

    @Override
    public void onClick(View v) 
    {     

            double n1 = Integer.parseInt(e1.getText().toString());
            e1.setBackgroundColor(Color.CYAN);
            double n2 = Integer.parseInt(e2.getText().toString());
            e2.setBackgroundColor(Color.BLUE);
            double mysum;
            mysum=(n1/n2)*100;
            if(mysum>65)
            {   t.setText(String.format("%.2f", mysum));
            t.setBackgroundColor(Color.GREEN);}
            else 
            { t.setText(String.format("%.2f", mysum));
            t.setBackgroundColor(Color.RED);}

        }           
    }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

public void execute(View v) {
    SharedPreferences prefre=getSharedPreferences("data",Context.MODE_PRIVATE);
    Editor editor=prefre.edit();
    editor.putString("attended", e1.getText().toString());
    editor.putString("total", e2.getText().toString());
    editor.putString("mysum", t.getText().toString());
    editor.commit();
    finish();   }

public void increment(View v) {
    int t1 = Integer.parseInt(e1.getText().toString());
    e1.setText(String.valueOf(t1+1));
    int t2 = Integer.parseInt(e2.getText().toString());
    e2.setText(String.valueOf(t2+1));
    double mysum;
    mysum=((double)t1/t2)*100;
    if(mysum>65)
    {   t.setText(String.format("%.2f", mysum));
        t.setBackgroundColor(Color.GREEN);}
    else 
    { t.setText(String.format("%.2f", mysum));
        t.setBackgroundColor(Color.RED);}

    }

public void decrement(View v) {
     int p1 = Integer.parseInt(e1.getText().toString());
    int p2 = Integer.parseInt(e2.getText().toString());
      e2.setText(String.valueOf(p2+1));
      double mysum;
        mysum=((double)p1/p2)*100;
        if(mysum>65)
        {   t.setText(String.format("%.2f", mysum));
        t.setBackgroundColor(Color.GREEN);}
        else 
        { t.setText(String.format("%.2f", mysum));
        t.setBackgroundColor(Color.RED);}

}

and here is the activity_main.xml file

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
 android:background="@drawable/bg"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.counter.MainActivity" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="90dp"
    android:text="ATTENDED" />

<EditText
    android:id="@+id/editTextN1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/textView1"
    android:layout_alignBottom="@+id/textView1"
    android:layout_marginLeft="42dp"
    android:layout_toRightOf="@+id/textView1"
    android:ems="10"
    android:inputType="number" 
    android:hint="ENTER VALUE HERE"
    android:gravity="center"/>

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editTextN1"
    android:layout_marginTop="39dp"
    android:layout_toLeftOf="@+id/editTextN1"
    android:text="TOTAL" />

<EditText
    android:id="@+id/editTextN2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/textView2"
    android:layout_alignBottom="@+id/textView2"
    android:layout_alignLeft="@+id/editTextN1"
    android:layout_alignRight="@+id/editTextN1"
    android:ems="10"
    android:inputType="number"
    android:hint="ENTER VALUE HERE"
    android:gravity="center" />

<Button
    android:id="@+id/save"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/buttoncalcsum"
    android:layout_alignParentBottom="true"
    android:onClick="execute"
    android:text="SAVE" />

<Button
    android:id="@+id/btnatnd"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/save"
    android:layout_alignRight="@+id/editTextN2"
    android:onClick="increment"
    android:text="PRESENT" />

<Button
    android:id="@+id/btnmiss"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/save"
    android:layout_alignParentLeft="true"
    android:onClick="decrement"
    android:text="MISSED" />

<Button
    android:id="@+id/buttoncalcsum"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_toRightOf="@+id/btnmiss"
    android:text="CALCULATE" />

<TextView
    android:id="@+id/textViewresult"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/buttoncalcsum"
    android:layout_marginTop="46dp"
    android:layout_toLeftOf="@+id/btnatnd"
    android:text="PERCENTAGE"
    android:textAppearance="?android:attr/textAppearanceLarge" />

薛源少
  • 306
  • 1
  • 6
  • 18
  • `when i pressed "present"` where is this `present` in your code ??? At least describe your problem better – Sharp Edge Jan 09 '15 at 07:02
  • PRESENT uses increment funtion and missed uses decrement function in code i have used Onclick graphical layout to code – sumit bhatt Jan 09 '15 at 07:16
  • when you press present button, what should happen? what is the result you are expecting? Please explain briefly. – Karthikeyan Jan 09 '15 at 07:25
  • when button b is pressed it calculates the percentage. when a user press present it increases the values of both e1 (attended) and e2 (total)from the previous entered values and then calculates the percentage and for missed it increases the value of e2 only and then calculates the percentage. – sumit bhatt Jan 09 '15 at 07:38
  • its related to this http://stackoverflow.com/questions/1094872/is-there-a-difference-between-x-and-x-in-java – markas Jan 09 '15 at 08:39
  • ex: from your code e2.setText(String.valueOf(p2+1)); – markas Jan 09 '15 at 08:40

3 Answers3

0

As far as I am understanding, you have not imported Buttons btnatnd and btnmiss. Also in the functions of increment() and decrement(), you are setting the text of TextView such that t1, t2 are increasing and p2 is decreasing. However the values of t1, t2 and p2 remain unchanged.

You should also use t1+=1;, t2+=1; and p2-=1;

Hope this helps !

Pranit Bankar
  • 453
  • 1
  • 7
  • 21
  • my code runs well values of t1 ,t2 and p2 also changes but the percentage doesn't change when i presses btnatn and btnmiss for first time only for second time percentage also works – sumit bhatt Jan 09 '15 at 08:19
0

AS Far as I understood , I think you want to calculate percentage , and sum and other functions and you are not getting the updated answer as you are using the Shared preferences. So in this case I would suggest you two following methods:

1: When You calculate the percentage or any other thing (I am supposing that you are well in calculations and calculating it with any mistake) . You should make two different methods One for saving calculations in the shared preferences and the other to retrieve the last result. So when you calculate first call the save method and then call the retrieve method to retrieve values in text fields or where ever you want

2:This is more efficient way. You can make the receiver for any change in the values of of preferences and it will notify as when they are changed , so its up to you where you use those updates.

Black
  • 21
  • 6
0

In increment()/decrement() function you update the values of Attended/Total TextViews, but you are then calculating the sum from old values. Try to first update the values, e.g.:

public void increment(View v) {
    ...
    int t1 = Integer.parseInt(e1.getText().toString());
    e1.setText(String.valueOf(t1+1));
    int t2 = Integer.parseInt(e2.getText().toString());
    e2.setText(String.valueOf(t2+1));
    t1++; t2++; // updating values here
    ....
    mysum=((double)t1/t2)*100;
    ....
}
roman
  • 66
  • 1
  • 7