将TextView更改为位置名称

时间:2018-08-11 12:52:02

标签: android string textview

我想将TextView的文本更改为标记指示的位置的名称。我遇到的问题是,我必须从另一个Java文件中调用此名称,但这似乎不起作用。

在PlaceInfo.java-文件中: PlaceInfo文件获取标记位置的名称(您搜索的位置)。这是通过Google Maps API完成的。

public class PlaceInfo {

private String address;
private LatLng latLng;
private String name;

public PlaceInfo(String name, String address, LatLng latLng) {
    this.name = name;
    this.address = address;
    this.latLng = latLng;
}


public PlaceInfo() {
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getAddress() {
    return address;
}

public void setAddress(String address) {
    this.address = address;
}

public LatLng getLatLng() {
    return latLng;
}

public void setLatLng(LatLng latLng) {
    this.latLng = latLng;
}

@Override
public String toString() {
    return "PlaceInfo{" +
            "address='" + address + '\'' +
            ", latLng=" + latLng +
            ", name='" + name + '\'' +
            '}';



}

}

并在带有textview的Activity中。因为问题在这里,所以应该更改的那个。

public class MarkerActivity extends Activity {

    private PlaceInfo mPlace;
    private TextView textElement;
    private Context context;



    ImageView imageView;

    Random r;

    Integer[] images = {
            R.drawable.markerphoto1,
            R.drawable.markerphoto2,
            R.drawable.markerphoto3,
            R.drawable.markerphoto4
    };



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_marker);
        context = this;





        //random image on page
        imageView = (ImageView) findViewById(R.id.imageView);
        r = new Random();
        imageView.setImageResource(images[r.nextInt(images.length)]);

        //set font for location_title
        TextView textView = (TextView) findViewById(location_title);
        Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/Lato-Regular.ttf");
        textView.setTypeface(typeface);

        //set location_title to name of location

        mPlace = new PlaceInfo();
        textElement = (TextView) findViewById(R.id.location_title);
        textElement.setText(String.valueOf(mPlace.getName()));


    }

}

现在位于MapActivity中的回调代码。

    private ResultCallback<PlaceBuffer> mUpdatePlaceDetailsCallback = new ResultCallback<PlaceBuffer>() {
    @Override
    public void onResult(@NonNull PlaceBuffer places) {
        if (!places.getStatus().isSuccess()){
            places.release();
            return;
        }
        final Place place = places.get(0);
        mPlace = new PlaceInfo();
        mPlace.setAddress(place.getAddress().toString());
        mPlace.setLatLng(place.getLatLng());
        mPlace.setName(place.getName().toString());

        moveCamera(mPlace.getLatLng(), DEFAULT_ZOOM);


    }
};

希望您能帮助我。谢谢

2 个答案:

答案 0 :(得分:0)

您应该使用PlaceInfo classgetName的对象。但是您正在调用class对象。

应该是下面的样子

textElement.setText(placeInfo.getName());

答案 1 :(得分:0)

String address; 
private ResultCallback<PlaceBuffer> mUpdatePlaceDetailsCallback = new ResultCallback<PlaceBuffer>() {
        @Override
        public void onResult(@NonNull PlaceBuffer places) {
            if (!places.getStatus().isSuccess()){
                places.release();
                return;
            }
            final Place place = places.get(0);
            mPlace = new PlaceInfo();
            mPlace.setAddress(place.getAddress().toString());
            mPlace.setLatLng(place.getLatLng());
            mPlace.setName(place.getName().toString());

            address = place.getAddress().toString();

            moveCamera(mPlace.getLatLng(), DEFAULT_ZOOM);

        }

在MarkerActivity中

 textElement.setText(getIntent().getStringExtra("address"));