Android 将TextView更改为位置名称
我想将TextView的文本更改为标记所指示位置的名称。我遇到的问题是,我必须从另一个java文件中调用这个名称,而这似乎不起作用 在PlaceInfo.java-文件中: PlaceInfo文件获取标记位置的名称(您搜索的位置)。这是通过谷歌地图API完成的Android 将TextView更改为位置名称,android,string,textview,Android,String,Textview,我想将TextView的文本更改为标记所指示位置的名称。我遇到的问题是,我必须从另一个java文件中调用这个名称,而这似乎不起作用 在PlaceInfo.java-文件中: PlaceInfo文件获取标记位置的名称(您搜索的位置)。这是通过谷歌地图API完成的 public class PlaceInfo { private String address; private LatLng latLng; private String name; public PlaceInfo(String
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 + '\'' +
'}';
}
}
并在具有文本视图的活动中。这是一个应该改变的问题,因为问题就在这里
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);
}
};
private ResultCallback mUpdatePlaceDetailsCallback=new ResultCallback(){
@凌驾
public void onResult(@NonNull PlaceBuffer places){
如果(!places.getStatus().issucess()){
places.release();
返回;
}
最终地点=地点。获取(0);
mPlace=newplaceinfo();
mPlace.setAddress(place.getAddress().toString());
mPlace.setLatLng(place.getLatLng());
mPlace.setName(place.getName().toString());
移动相机(mPlace.getLatLng(),默认为缩放);
}
};
我希望你能帮助我。谢谢您应该使用
PlaceInfo
类的对象来getName
。但是您正在调用类
对象
应该是下面的样子
textElement.setText(placeInfo.getName());
你得到了价值吗?如果是,那么在你的问题中分享这个。所以我可以帮你。你在这里使用两种不同的文本视图-title
和textfelement
。什么是PlaceInfo.class
?你应该使用一个对象,而不是类本身-PlaceInfo p=newplaceinfo()…
@TDG我改变了这个问题。可能还不够清楚。PlaceInfo文件中的“缺少返回语句”和var名称被分配给了它自己?+PlaceInfo文件提供了位置的名称,可以通过Google Map API找到。因此,设置一个新名称是没有意义的。因此,您只能使用getName()。PlaceInfo pi=new PlaceInfo();textElement.setText(pi.setName());像这样?textElement.setText(pi.getName());如果是静态的,只需使用PlaceInfo.getName()。不要加“.class”是的,我就是这么做的。。。还是不行。请阅读我编辑的问题。您在PlaceInfo类中从何处设置名称?private ResultCallback mUpdatePlaceDetailsCallback=new ResultCallback(){@Override public void onResult(@NonNull PlaceBuffer places){if(!places.getStatus().isSuccess()){places.release();return;}final Place=places.get(0);mPlace=new PlaceInfo();mPlace.setName(Place.getName().toString());};在MapActivity(谷歌地图API)中,我得到了当前的姓名、地址等。。。因此,PlaceInfo.java文件中的名称取决于所选位置(如果有意义的话)
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);
}
textElement.setText(getIntent().getStringExtra("address"));