我的代碼有點問題。Android返回兩個項目,但只顯示一個
我想在for循環中獲取兩個值,但只顯示一個值。
這裏是我的代碼
public class ReturnValue
{
public String item;
public String item_intro;
}
public ArrayList<ReturnValue> populate() {
ArrayList<ReturnValue> returnValues = new ArrayList<ReturnValue>();
while {
ReturnValue rv = new ReturnValue();
try {
URL url = new URL
("http://www.wvvzondag2.nl/android/fillverslag.php");
HttpURLConnection urlConnection =
(HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
// gets the server json data
BufferedReader bufferedReader =
new BufferedReader(new InputStreamReader(
urlConnection.getInputStream()));
String next;
//While loop to go through the query until reached the end
while ((next = bufferedReader.readLine()) != null){
JSONArray ja = new JSONArray(next);
for (int i = 0; i < ja.length(); i++) {
String var = "";
String var_intro = "";
JSONObject jo = (JSONObject) ja.get(i);
var = jo.getString("title");
var_intro = jo.getString("introtext");
items_intro.add(var_intro);
items.add(var);
}
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
rv.item = var;
rv.item_intro = var_intro;
returnValues.add(rv);
}
return returnValues;
}
湯姆一些修改後,現在該代碼的主要部分看起來是這樣的。
但我仍然有一些錯誤
令牌「而」刪除語法錯誤此令牌
items_intro不能得到解決
項目不能得到解決
不知道怎麼修復此...
親切的問候, 帕特里克
首先,你只能返回一個對象。如果你想返回兩個陣列,你必須用它們兩個創建一個對象,但是......爲什麼你有兩個相同的變量(items和items_intro),你爲什麼要返回這兩個變量? – luanjot 2012-08-06 19:21:41
@luanjot我懷疑在內部循環中有一個輸入錯誤,他打算執行'items_intro.add(var_intro2)'。所以這兩個ArrayLists會有所不同。 – Izkata 2012-08-06 19:23:42