我的价值空白,但我不需要显示空字

时间:2018-04-18 13:19:34

标签: java android

我有4个从我想在一个TextView中显示的不同活动导入的字符串值。

但是当您在手机上显示值时,导入的值为空白,并且它们在屏幕上显示为空,并且我不希望显示单词null。我怎么能取消它?

此代码用于显示值并设置此条件但未成功。代码是错误的吗?

public class Order extends AppCompatActivity {
private EditText nam;
private EditText address;
private EditText phon;
String sandwish;
String snack;
String juice;
String pizza;
int sandwishI;
int snackI;
int juiceI;
int pizzaI;
TextView orderSummaryTextView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate( savedInstanceState );
    setContentView( R.layout.activity_order );
    Intent i = getIntent();
    Bundle b = i.getBundleExtra( "personBdl" );
    sandwish = b.getString( "string" );
    snack = b.getString( "name" );
    juice = b.getString( "value1" );
    pizza = b.getString( "value2" );
    sandwishI = b.getInt( "INT" );
    snackI = b.getInt( "INT1" );
    juiceI = b.getInt( "INT2" );
    pizzaI = b.getInt( "INT3" );
    final String textMessages = allText( sandwish,snack,juice,pizza );
    displayMessageSandwish( textMessages );

private String allText(String sandwish,String snack,String juice,String pizza) {
    String string = "";

    if (TextUtils.isEmpty( sandwish )) {
        orderSummaryTextView.setError( "empty" );
    }

    if (TextUtils.isEmpty( snack )) {
        orderSummaryTextView.setError( "empty" );
    }

    return string;
}

private void displayMessageSandwish(String mSandwish) {
    orderSummaryTextView = (TextView) findViewById( R.id.order_sandwish );
    orderSummaryTextView.setText( mSandwish );
}

我在手机中添加了一张图片,其中显示了导入值为空的单词Null的外观

enter image description here

2 个答案:

答案 0 :(得分:0)

您可以使用此方法解决此问题。

private static String removeIfNull(String clearMe){
   return clearMe == null ? "" : clearMe;
}

并将其用作;

snack = removeIfNull(b.getString( "name" ));
juice = removeIfNull(b.getString( "value1" ));
pizza = removeIfNull(b.getString( "value2" ));

答案 1 :(得分:0)

allText(String, String, String, String)的结果始终为空字符串。因此,始终使用空displayMessageSandwish(String)参数调用String

虽然您没有提供完整的示例,但我认为,由于您在基础布局文件中设置了TextView的文本,或者在代码中的完全不同的位置设置了生成的屏幕