我遇到了一个小问题,我的代码工作并向我发送经度和纬度的输出。我只想用逗号和空格来分隔textview中的值。我不想要行分隔符。
我得到的:32.67543-55.986454
我想要的:32.67543,-55.986454(逗号和空格)
有什么想法吗?
代码:
/**
* 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();
}
}
答案 0 :(得分:5)
获得纬度后的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");
}
答案 1 :(得分:1)
尝试类似
的内容jsonStringBuilder.append(product.getString(TAG_LATITUDE) + ", ");
或者,您可以在经度之前代替:
jsonStringBuilder.append(", " + product.getString(TAG_LONGITUDE));