【问题标题】:How to print variable from another class如何从另一个类打印变量
【发布时间】:2017-04-21 07:29:30
【问题描述】:

我需要在TextViewonCreate() 方法中打印可变电压和电流。电流和电压变量在clientgetui() 方法中。

这是我的代码:

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


                }



            }

        }

【问题讨论】:

标签: java android android-asynctask textview


【解决方案1】:

当你使用 AsyncTask 时

所有 UI 的数据绑定都在 onPostExecute 方法中完成。

 @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            txtCurrent.setText(String.valueof(current));
            txtVoltage.setText(String.valueof(voltage));
        }

【讨论】:

  • 感谢您的回答,但它会读取代码开头的电流和电压变量。它没有从我的方法中读取
【解决方案2】:

我看到您在 AsynTask 的后台调用 clientgetui() 方法,因此有多种获取数据的方法。

Way1:

创建全局变量 (voltage, current),然后在从这些变量中获取值之前,您应该检查您的 AsynctaskgetUI 是否已完成。

Way2:

将这些变量保存在SharedPreferences 中,并在从 sharedpreferences 获取数据之前确保您的 AsynctaskgetUI 已完成。

Way3:

制作全局静态变量,按照Way1.

通过以上方式: 从 Way1 你可以从类内的变量中获取数据,而从其他两种方式你也可以从类外的变量中获取数据。

【讨论】:

  • 感谢您的回答。我会试试的
  • 欢迎,如果您遇到任何问题,评论将是可观的。
猜你喜欢
  • 2020-11-27
  • 2017-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-12
相关资源
最近更新 更多