空字段不得显示......!

时间:2015-02-12 11:28:23

标签: android null


我正在开发地图应用程序,
在我的应用中,我显示完整的地址
但我只有一个问题 如果地址字段没有获得任何数据,那么该字段将不会显示 我该如何解决这个问题? 必要的代码如下: ActivityMain.java

String zip = addresses.get(0).getPostalCode();
                String city = addresses.get(0).getLocality();
                String state = addresses.get(0).getAdminArea(); 
                String country = addresses.get(0).getCountryName();

//              if (city.trim().isEmpty()) {
//                  
//              } else if (state.trim().isEmpty()) {
//                  
//              } else if (zip.trim().isEmpty()) {
//                  
//              } else if (country.trim().isEmpty()) {
//                  
//              } else {
//
//              }

                String title = zip + "," + city + "," + state + "," + country;
                showPosition.setText(zip + "\n" + city + "\n" + state + "\n" + country);
                googleMap.addMarker(new MarkerOptions().position(arg0).title(title));

2 个答案:

答案 0 :(得分:1)

showPosition是TextView? 您应该设置"可见性" attribute(showPosition.setVisibility(View.GONE或INVISIBLE)。

您应该重新格式化将在TextView中显示的字符串。

String title = "";
if(zip != null && zip.trim().length()>0) {
   title += zip + "\n"
}

// Same for the other fields (without else)
if(country != null && country.trim().length()>0) {
   title += country + "\n"
}

答案 1 :(得分:0)

试试这个 字符串标题="" + zip +"," + city +"," +州+"," +国家;                     showPosition.setText("" + zip +" \ n" + city +" \ n" + state +" \ n" + country );

相关问题