用 StringBuilder 附加一个逗号
Append a comma with StringBuilder
我遇到了一个小问题,我的代码可以运行并向我发送经度和纬度的输出。我只想用逗号和 space 来分隔文本视图中的值。我不想要行分隔符。
我得到的是:32.67543-55.986454
我想要的:32.67543,-55.986454(逗号和space)
有什么想法吗?
代码:
/**
* After completing background task Dismiss the progress dialog
* *
*/
protected void onPostExecute(JSONObject product) {
if (product != null) {
// product with this pid found
// Edit Text
lblDummyLocation = (TextView) findViewById(R.id.lblDummyLocation);
StringBuilder jsonStringBuilder = new StringBuilder(); //Create StringBuilder for concatenation of JSON results
// display profile data in EditText
try {
//txtMessage.setText(product.getString(TAG_FIRSTNAME)); //Don't set the text here
jsonStringBuilder.append(product.getString(TAG_LATITUDE)); //Concatenate each separate item
jsonStringBuilder.append(System.getProperty("line.separator"));
} catch (JSONException e) {
e.printStackTrace();
}
try {
//txtMessage.setText(product.getString(TAG_LASTNAME));
jsonStringBuilder.append(product.getString(TAG_LONGITUDE));
//jsonStringBuilder.append(System.getProperty("line.separator"));
} catch (JSONException e) {
e.printStackTrace();
}
lblDummyLocation.setText(jsonStringBuilder.toString());
}
// dismiss the dialog once got all details
pDialog.dismiss();
}
}
试试
jsonStringBuilder.append(product.getString(TAG_LATITUDE) + ", ");
或者,您可以将经度放在前面:
jsonStringBuilder.append(", " + product.getString(TAG_LONGITUDE));
那个 line.separator 获取纬度后会在它后面换行,我认为你不需要那样。
要添加逗号,只需再添加一个。
失败时只需要一个try/catch块。
StringBuilder jsonStringBuilder = new StringBuilder();
// display profile data in EditText
try
{
jsonStringBuilder.append(product.getString(TAG_LATITUDE));
jsonStringBuilder.append(", ");
jsonStringBuilder.append(product.getString(TAG_LONGITUDE));
lblDummyLocation.setText(jsonStringBuilder.toString());
}
catch (JSONException e)
{
e.printStackTrace(); // use a logger for this ideally
lblDummyLocation.setText("Failed to get co-ordinates");
}
我遇到了一个小问题,我的代码可以运行并向我发送经度和纬度的输出。我只想用逗号和 space 来分隔文本视图中的值。我不想要行分隔符。
我得到的是:32.67543-55.986454
我想要的:32.67543,-55.986454(逗号和space)
有什么想法吗?
代码:
/**
* After completing background task Dismiss the progress dialog
* *
*/
protected void onPostExecute(JSONObject product) {
if (product != null) {
// product with this pid found
// Edit Text
lblDummyLocation = (TextView) findViewById(R.id.lblDummyLocation);
StringBuilder jsonStringBuilder = new StringBuilder(); //Create StringBuilder for concatenation of JSON results
// display profile data in EditText
try {
//txtMessage.setText(product.getString(TAG_FIRSTNAME)); //Don't set the text here
jsonStringBuilder.append(product.getString(TAG_LATITUDE)); //Concatenate each separate item
jsonStringBuilder.append(System.getProperty("line.separator"));
} catch (JSONException e) {
e.printStackTrace();
}
try {
//txtMessage.setText(product.getString(TAG_LASTNAME));
jsonStringBuilder.append(product.getString(TAG_LONGITUDE));
//jsonStringBuilder.append(System.getProperty("line.separator"));
} catch (JSONException e) {
e.printStackTrace();
}
lblDummyLocation.setText(jsonStringBuilder.toString());
}
// dismiss the dialog once got all details
pDialog.dismiss();
}
}
试试
jsonStringBuilder.append(product.getString(TAG_LATITUDE) + ", ");
或者,您可以将经度放在前面:
jsonStringBuilder.append(", " + product.getString(TAG_LONGITUDE));
那个 line.separator 获取纬度后会在它后面换行,我认为你不需要那样。
要添加逗号,只需再添加一个。
失败时只需要一个try/catch块。
StringBuilder jsonStringBuilder = new StringBuilder();
// display profile data in EditText
try
{
jsonStringBuilder.append(product.getString(TAG_LATITUDE));
jsonStringBuilder.append(", ");
jsonStringBuilder.append(product.getString(TAG_LONGITUDE));
lblDummyLocation.setText(jsonStringBuilder.toString());
}
catch (JSONException e)
{
e.printStackTrace(); // use a logger for this ideally
lblDummyLocation.setText("Failed to get co-ordinates");
}