使用另一个类的Double填充TextView

时间:2015-12-04 05:18:23

标签: java android android-studio settext

我有一个应用程序可以获取您当前的位置。它将返回您当前的地址,纬度和经度。单击转换后,将转到新类。

我希望将纬度和经度(如果有东西)放入另一个类的textView中。

第一类

public class MainActivity extends AppCompatActivity {

private static Button newWindowBtn; //new window button

Button btnShowLocation;
GPSTracker gps;
Double latitude;
Double longitude;
String addressStr;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            shareIt();
        }
    });


    final TextView address = (TextView)findViewById(R.id.textView);

    onClickButtonListener(); //NEW WINDOW CLICK LISTENER

    btnShowLocation = (Button) findViewById(R.id.show_location); //GPS BUTTON

    final Geocoder geocoder = new Geocoder(this,Locale.ENGLISH);

    btnShowLocation.setOnClickListener(new View.OnClickListener()
    { //GPS CLICK LISTENER
        @Override
        public void onClick(View v) { //SHOWS LOCATION
            gps = new GPSTracker(MainActivity.this);

            if(gps.canGetLocation()){
                latitude =  gps.getLatitude();
                longitude = gps.getLongitude();


                try{
                    List<Address> addresses = geocoder.getFromLocation(latitude,longitude,1);
                    if(addresses != null)
                    {
                        Address returnedAddress = addresses.get(0);
                        StringBuilder strReturnedAddress = new StringBuilder("Address:\n");
                        for (int i = 0; i < returnedAddress.getMaxAddressLineIndex();i++){
                            strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("\n");
                        }


                        address.setText(strReturnedAddress.toString() + "Latitude: " + latitude + "\nLongitude: " + longitude);

                        addressStr = strReturnedAddress.toString();

                    }
                    else
                    {
                        address.setText("No address returned");
                    }
                }
                catch(IOException e){
                    e.printStackTrace();
                    address.setText("Can't get address");
                }
            }else{
                gps.showSettingsAlert();
            }
        }
    });
}

SECOND CLASS

public class secondActivity extends AppCompatActivity {

    Button convertBtn;
    TextView latitudeTxt;
    TextView longitudeTxt;
    TextView latitudeDMS;
    TextView longitudeDMS;



    convertTo convert = new convertTo();

    MainActivity mainActivity = new MainActivity();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                shareIt();
            }
        });

        convertBtn = (Button) findViewById(R.id.convertBtn);

        latitudeTxt = (TextView)findViewById(R.id.latitudeTxt);
        longitudeTxt = (TextView)findViewById(R.id.longitudeTxt);

        latitudeDMS = (TextView)findViewById(R.id.latitudeDMS);
        longitudeDMS = (TextView)findViewById(R.id.longitudeDMS);



        convertBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    }

所以我希望latitudeTxt拥有上一个屏幕(第一堂课)的纬度。我是一名学生并且正在努力学习。

我已尝试将其添加到第一堂课:

secondActivity secondActivity = new secondActivity();
secondActivity.latitudeTxt.setText(latitude);

这没有用。

我试过第二节课

MainActivity mainActivity= new MainActivity();
latitudeTxt.setText(mainActivity.latitude);

3 个答案:

答案 0 :(得分:0)

这种方法完全错误,并且不鼓励Android。

MainActivity mainActivity= new MainActivity();

永远不会这样做。

您实际上正在寻找的是在活动之间共享数据(社区中有很多这样的问题) and this is how this works

fisrt类中的

(实际上是活动)使用它作为参考:

static final int PICK_CONTACT_REQUEST = 1;  // The request code
...
private void pickContact() {
    Intent pickContactIntent = new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts"));
    pickContactIntent.setType(Phone.CONTENT_TYPE); // Show user only contacts w/ phone numbers
    startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST);
}

在第二个:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // Check which request we're responding to
    if (requestCode == PICK_CONTACT_REQUEST) {
        // Make sure the request was successful
        if (resultCode == RESULT_OK) {
            // The user picked a contact.
            // The Intent's data Uri identifies which contact was selected.

            // Do something with the contact here (bigger example below)
        }
    }
}

答案 1 :(得分:0)

使用捆绑附加功能将纬度,经度值从第1级发送到第2级。提取第二类中的值并将值设置为相应的文本视图。

答案 2 :(得分:0)

你不应该创建像这样的活动对象,

MainActivity mainActivity= new MainActivity();

使用Intents启动活动,如果您需要引用活动的当前对象,则可以MainActivity.this执行此操作。

"This didn't work."而言,这是因为您正在创建Activity类的新对象(这是正确的语法)。新对象将拥有自己的实例变量副本。因此它不会具有已存在的Activity对象的值(显示给您)。

要将您的值从FirstActivity传递给SecondActivity,请执行类似的操作,

<强> FirstActivity

Intent secondActivity = new Intent(FirstActivity.this, SecondActivity.class);
secondActivity.putExtra("latitude", value);
secondActivity.putExtra("longitude", value);
startActivity(secondActivity);

然后在 SecondActivity

Intent intent = getIntent();
String latitude = intent.getStringExtra("latitude");
String longitude = intent.getStringExtra("longitude");