1

I need to print to variable voltage and current in my TextView in onCreate() method. Current and Voltage variables are in the clientgetui() method.

Here is my code:

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Arrays;

public class getUI extends AppCompatActivity {

    String IP;
    TextView txtCurrent, textVoltage;


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

         IP = getIntent().getExtras().getString("IP");





       TextView txtCurrent = (TextView) findViewById(R.id.textViewCurrent);
       TextView textVoltage = (TextView) findViewById(R.id.textViewVoltage);

       // txtCurrent.setText(String.valueOf(current));

        Button buttongetvalue = (Button) findViewById(R.id.buttongetUI);
        buttongetvalue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try{
                    new AsynctaskgetUI().execute();
                }catch (Exception e){

                }


            }
        });


    } //ONCREATE!



        public class AsynctaskgetUI extends AsyncTask<Void, Integer, Void> {


            public int mVoltage;
            public int mCurrent;

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
            }

            @Override
            protected void onPostExecute(Void aVoid) {
                super.onPostExecute(aVoid);
            }

            @Override
            protected void onProgressUpdate(Integer... values) {
                super.onProgressUpdate(values);

            }

            @Override
            protected Void doInBackground(Void... voids) {
                try {
                    clientgetui();
                } catch (IOException e) {
                    e.printStackTrace();
                };

                return null;
            }

                //____________________CLIENT1_____________________________

                public void clientgetui() throws IOException {

                    DatagramSocket ds = new DatagramSocket();
                    byte b[] = new byte[]{0x7F, 0x03, 0x01, 0x00, 0x01, 0x15, 0x00, 0x00, 0x00, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x20, 0x10, 0x00, 0x00, 0x00, 0x00};


                    // byte[] ipA = new byte[]{(byte) 192, (byte) 168, 1, (byte) 100};
                    // IPAdress = String.valueOf(getIntent().getExtras().getByte("IP insert"));


                    // String ipA = IP;

                    InetAddress ia = InetAddress.getByName(IP);

                    DatagramPacket dp = new DatagramPacket(b, 28, ia, 21234);
                    ds.send(dp);

                    byte buffer[] = new byte[28];
                    DatagramPacket reply = new DatagramPacket(buffer, buffer.length);


                    ds.receive(reply);

                    ds.close();

                    byte[] dp23 = Arrays.copyOfRange(buffer, 8, 12);

                    int voltage = (dp23[0] * 256 + dp23[1]) & 0xFF; // PRINT VOLTAGE
                    int current = (dp23[2] * 256 + dp23[3]) & 0xFF; // PRINT CURRENT in TextView


                }



            }

        }
PEHLAJ
  • 9,980
  • 9
  • 41
  • 53
TLDima
  • 45
  • 3
  • 1
    Have a look here http://stackoverflow.com/a/9963705/2890156 – Denny Apr 21 '17 at 07:31
  • https://developer.android.com/reference/android/os/AsyncTask.html – Arun Shankar Apr 21 '17 at 07:53
  • Possible duplicate of [How to get the result of OnPostExecute() to main activity because AsyncTask is a separate class?](http://stackoverflow.com/questions/12575068/how-to-get-the-result-of-onpostexecute-to-main-activity-because-asynctask-is-a) – pringi Apr 21 '17 at 09:07

2 Answers2

0

As you are using AsyncTask

All the UI's data binding is done in onPostExecute method.

 @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            txtCurrent.setText(String.valueof(current));
            txtVoltage.setText(String.valueof(voltage));
        }
garry
  • 419
  • 5
  • 10
  • Thanks for your answer, but it reading current and voltage variables in the beginning of the code. it doesn't reading from my method – TLDima Apr 21 '17 at 08:43
-1

As i am seeing that you are calling clientgetui() method in background of a AsynTask so there are some various ways to get data.

Way1:

create global variables (voltage, current) and then before getting the value from these variables you should check that your AsynctaskgetUI has been completed or not.

Way2:

Save these variables in SharedPreferences and before getting data from sharedpreferences make sure that Your AsynctaskgetUI has been completed.

Way3:

Make global static variables and follow Way1.

from these above ways: from Way1 you can get the data from variables with in class while from other two ways you can get the data from variables outside class also.

singh.indolia
  • 1,292
  • 13
  • 26