Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/213.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android返回两个项目,但只显示一个_Android_Listview_Return_Items - Fatal编程技术网

Android返回两个项目,但只显示一个

Android返回两个项目,但只显示一个,android,listview,return,items,Android,Listview,Return,Items,我的代码有点问题 我想在for循环中获取两个值,但只显示一个 这是我的密码 public class ReturnValue { public String item; public String item_intro; } public ArrayList<ReturnValue> populate() { ArrayList<ReturnValue> returnValues = new ArrayList<ReturnValue&

我的代码有点问题

我想在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;
}
公共类返回值
{
公共字符串项;
公共字符串项目介绍;
}
公共ArrayList填充(){
ArrayList returnValues=新的ArrayList();
当{
ReturnValue rv=新的ReturnValue();
试一试{
URL=新URL
("http://www.wvvzondag2.nl/android/fillverslag.php");
HttpURLConnection-urlConnection=
(HttpURLConnection)url.openConnection();
urlConnection.setRequestMethod(“GET”);
urlConnection.connect();
//获取服务器json数据
缓冲读取器缓冲读取器=
新的BufferedReader(新的InputStreamReader(
urlConnection.getInputStream());
下一个字符串;
//While循环遍历查询直到到达末尾
while((next=bufferedReader.readLine())!=null){
JSONArray ja=新JSONArray(下一个);
对于(int i=0;i
经过tom的一些修改后,代码的主要部分如下所示

但我还是有一些错误

令牌“while”上出现语法错误,请删除此令牌

无法解析项目介绍

无法解析项目

我不知道如何解决这个问题

致以亲切的问候,
Patrick

不能返回两个值。回归不是这样的。Return结束方法并返回一(1)个值。对于你正在做的,考虑返回一个列表(或列表)或使用一个包。

你可以创建另一个包含对象项和ItssIn的数组,然后返回这个2元素数组。假设items是第一个元素,items是第二个元素

您只能返回一个对象。一个选项是创建一个包含列表的“包装器对象”,并返回该列表

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();
        //...
        //other code
        //...
        rv.item = var;
        rv.item_intro = var;

        returnValues.add(rv);
    }
    return returnValues;
}
public ArrayList populate(){
ArrayList returnValues=新的ArrayList();
当{
ReturnValue rv=新的ReturnValue();
//...
//其他代码
//...
rv.item=var;
rv.item_intro=var;
返回值。添加(rv);
}
返回值;
}

方法只能返回一个值。但是您可以创建两个不同的方法,一个返回一种类型,另一个返回另一种类型。通过这种方式,您可以检索两个值。

首先,您只能返回一个对象。如果要返回两条arrray,必须使用它们创建一个对象,但是。。。为什么你有两个相等的变量(items和items\u intro),为什么你想同时返回它们?@luanjot我怀疑内部循环中有一个输入错误,他想做
items\u intro.add(var\u intro 2)
。所以这两个ArrayList是不同的。谢谢你的快速回复,我尝试了你的方式,但它给了我一个信息。。类型不匹配:无法从VerslagJSONParser.ReturnValue转换为ArrayListops,我的错误,我编辑了我的代码。populate方法应该返回ReturnValue而不是ArrayList现在我在setListAdapter的onCreate中遇到一个错误构造函数ArrayAdapter(VerslagJSONParser,int,VerslagJSONParser.ReturnValue)是未定义的我确实需要这个函数,因为如果我更改/删除它,那么我的列表将不会被填充OK,完全重写代码。我想我至少给了你前进的道路。我还建议您阅读有关ListView的内容。。