我想从字符串数组转换字符串。这个字符串是 temp1 和 hum1 。我想把温度和湿度的值放在android.Here station对象的表中包含整个json respose.So.How我可以将字符串转换为字符串数组。请帮助。谢谢。
public class LocationActivity extends ListActivity {
private static final String TAG_PARAM_NAME = "PARAM_NAME";
private static final String TAG_PARAM_VALUE = "PARAM_VALUE";
private static final String TAG_PARAM_UNIT = "PARAM_UNIT";
TableLayout tl;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_table__weather_forecast);
class GetWeatherDetails extends AsyncTask<String, String, String>
{
protected String doInBackground(String... params)
{
runOnUiThread(new Runnable()
{
public void run()
{
try {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("LAT", LAT));
params.add(new BasicNameValuePair("LONGITUDE", LONGITUDE));
JSONObject json = jsonParser.makeHttpRequest(
url_Weather_details, "GET", params);
Log.d("Weather Details", json.toString());
JSONArray stationObj = json.getJSONArray(TAG_WEATHER); // JSON Array
int lengthJsonArr = stationObj.length();
// get weather info object from JSON Array
for(int i=0;i<lengthJsonArr;i++)
{
JSONObject station = stationObj.getJSONObject(i);
if(station.getString(TAG_PARAM_NAME).contains("Temperature"))
{
String temp1=station.getString(TAG_PARAM_VALUE);
}
if(station.getString(TAG_PARAM_NAME).contains("Humidity"))
{
String hum1=station.getString(TAG_PARAM_VALUE);
}
}
}
}
}
}
}
}
}
我试过这个例子,但它只给了我22 的值而不是整个字符串
package com.example.string;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView textview;
String temp;
String tempArray[];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String temp11[]={"21","31","22"};
for(int i=0;i<=2;i++)
{
temp=temp11[i];
}
tempArray = temp.split(",");
Log.d("temparray",tempArray.toString());
textview=(TextView)findViewById(R.id.textView1);
for (int i = 0; i < tempArray.length; i++)
{
textview.setText(tempArray[i]);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
EDIT:
String temp;
String tempArray[];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
temp="21,31,22";
tempArray = temp.split(",");
for (String eachTemp : tempArray){
Log.d("tempArray",tempArray.toString());
}
textview=(TextView)findViewById(R.id.textView1);
for (int i = 0; i < tempArray.length; i++)
{
textview.setText(tempArray[i]);
}
}
答案 0 :(得分:0)
最简单的方法是:
String[] tempArray = temp1.split(",");
编辑:
String temp="21,23,21,24,26";
tempArray = temp.split(",");
for (String eachTemp : tempArray){
do whatever you want here
}
答案 1 :(得分:0)
这段代码毫无意义:
String temp11[]={"21","31","22"};
for(int i=0;i<=2;i++)
{
temp=temp11[i];
}
tempArray = temp.split(",");
你有一个临时数组,然后你迭代每一个,什么都不做。当你完成时,你拿最后一个(“22”)并尝试将它分成逗号,它什么都不做。
也许你想要的是这样的:
String someTemps = "21,31,22";
for (String temp : someTemps.split(",")) {
// use the value stored in the variable 'temp' here
System.out.println("Do something interesting with temp " + temp);
}