0

I have an app with a button and also a textview (Numeric ex . 100)

i am click button then increment Textview (value) +5.

I am trying to send the value to Php Table Row and Also Retrieve Table Row Data in Same Text View.

in app was close and Open then fetch PHP Data(server) as per Texview

my_table

  • id
  • user_Name
  • amount

MyActivity.java

public class MainActivity extends AppCompatActivity {
    int minteger = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void increaseInteger(View view) {
        minteger = minteger + 5 ;
        display(minteger);
    }

    private void display(int number) {

        TextView displayInteger = (TextView) findViewById(
                R.id.integer_number);
        displayInteger.setText("Integer: " + number);
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_margin="16dp"
    android:orientation="vertical"
    android:gravity="center_horizontal"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click the plus button to increase integer number" />

    <TextView
        android:id="@+id/integer_number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:layout_marginBottom="16dp"
        android:textSize="30sp"
        android:text="Integer: 0" />

    <Button
        android:id="@+id/increase"
        android:onClick="increaseInteger"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="INCREASE" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="bottom"
        android:textSize="20sp"
        android:text="viralandroid.com"/>
</LinearLayout>
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • if you want to communicate with server like sending the data from the app and retrieving the data from the server ,then u want set up retrofit or volley in your android project.Retrofit and volley are the external libraries for handling web services in android – swaroop Sep 02 '18 at 06:38

1 Answers1

0

Initialize the TextView & your Button inside onCreate first and remove the initialization from display method.

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

        displayInteger = (TextView) findViewById(
                    R.id.integer_number);
        Button myButton = (Button) findViewById(
                    R.id.yourButtonId);

        // Here you'll need onClickListener for the button to handle if button clicked, increase the number and etc or calling methods.

}

Follow Android Button Onclick for handle Android Button click. But, the point is, if you add onClickListener to the Button, you may call display or just the setText inside onClickListener and no need for another method.(Simplifying)

Also add:

TextView displayInteger;

In above of onCreate to have access all over the Activity and the current class.


To send string to server side, follow this link : How send data to website by using android app

You'll need to first get the text by getText().toString() then sending as string to server: How to get EditText value and display it on screen through TextView?

ʍѳђઽ૯ท
  • 16,646
  • 7
  • 53
  • 108