listview不是从我的forloop中填写的,我做错了什么?

时间:2016-05-20 15:44:26

标签: android json listview

我有一个for循环,它应该用我的数据填充我的listview,但它只从我的json文件中的三个中的第一个位置获取数据,任何帮助将不胜感激!

继承我的相关代码;

编辑的代码版本,我认为错误在于

    imgtest = (ImageView) findViewById(R.id.imageView);

    // http://api.champion.gg/champion/Ekko/
    new JSONTask().execute("http://api.champion.gg/champion/ekko/");

      Champ = (ListView) findViewById(R.id.listView);
}

public class JSONTask extends AsyncTask<String, String, List<Layoutmodel>> {
    @Override
    protected List<Layoutmodel> doInBackground(String... params) {
        HttpURLConnection connection = null;
        BufferedReader reader = null;


        try {
            URL url = new URL(params[0]);

            connection = (HttpURLConnection) url.openConnection();
            connection.connect();

            InputStream stream = connection.getInputStream();

            reader = new BufferedReader(new InputStreamReader(stream));

            StringBuffer buffer = new StringBuffer();

            String line = "";

            while ((line = reader.readLine()) != null) {
                buffer.append(line);
            }

            String finalJson = buffer.toString();

            JSONArray jsonarray = new JSONArray(finalJson);
            List<Layoutmodel> LayoutModelList = new ArrayList<>();

            for (int i = 0; i < jsonarray.length(); i++) {
                JSONObject finalObject = jsonarray.getJSONObject(i);

            Layoutmodel layoutmodel = new Layoutmodel();
            layoutmodel.setChampionName2(finalObject.getString("key"));
            layoutmodel.setRole(finalObject.getString("role"));

            ChampionName = finalObject.getString("key");
            String role = finalObject.getString("role");
            String overallPosition = finalObject.getString("overallPosition");

            JSONObject ItemArray4 = new JSONObject(overallPosition);
            String champpos = ItemArray4.getString("position");
            Log.v("d", champpos);

            String items = finalObject.getString("items");

            JSONObject ItemArray = new JSONObject(items);


            item2 = ItemArray.getString("mostGames");
            item3 = ItemArray.getString("highestWinPercent");
            JSONObject ItemArray2 = new JSONObject(item2);
            JSONObject ItemArray3 = new JSONObject(item3);
            item3 = ItemArray2.getString("items");
            item4 = ItemArray3.getString("items");


            JSONArray jsonarray2 = new JSONArray(item3);
            JSONArray jsonarray3 = new JSONArray(item4);

            JSONObject finalObject2 = jsonarray2.getJSONObject(jsonarray2.length() - 6);
            ItemName = finalObject2.getString("name");

            JSONObject finalObject3 = jsonarray2.getJSONObject(jsonarray2.length() - 5);
            ItemName2 = finalObject3.getString("name");

            JSONObject finalObject4 = jsonarray2.getJSONObject(jsonarray2.length() - 4);
            ItemName3 = finalObject4.getString("name");

            JSONObject finalObject5 = jsonarray2.getJSONObject(jsonarray2.length() - 3);
            ItemName4 = finalObject5.getString("name");

            JSONObject finalObject6 = jsonarray2.getJSONObject(jsonarray2.length() - 2);
            ItemName5 = finalObject6.getString("name");

            JSONObject finalObject7 = jsonarray2.getJSONObject(jsonarray2.length() - 1);
            ItemName6 = finalObject7.getString("name");

            //Highest win names
            JSONObject finalObject8 = jsonarray3.getJSONObject(jsonarray3.length() - 6);
            ItemNameHW = finalObject8.getString("name");

            JSONObject finalObject9 = jsonarray3.getJSONObject(jsonarray3.length() - 5);
            ItemName2HW = finalObject9.getString("name");

            JSONObject finalObject10 = jsonarray3.getJSONObject(jsonarray3.length() - 4);
            ItemName3HW = finalObject10.getString("name");

            JSONObject finalObject11 = jsonarray3.getJSONObject(jsonarray3.length() - 3);
            ItemName4HW = finalObject11.getString("name");

            JSONObject finalObject12 = jsonarray3.getJSONObject(jsonarray3.length() - 2);
            ItemName5HW = finalObject12.getString("name");

            JSONObject finalObject13 = jsonarray3.getJSONObject(jsonarray3.length() - 1);
            ItemName6HW = finalObject13.getString("name");


            layoutmodel.setItem1(ItemName);
            layoutmodel.setItem2(ItemName2);
            layoutmodel.setItem3(ItemName3);
            layoutmodel.setItem4(ItemName4);
            layoutmodel.setItem5(ItemName5);
            layoutmodel.setItem6(ItemName6);

            layoutmodel.setItem1HW(ItemNameHW);
            layoutmodel.setItem2HW(ItemName2HW);
            layoutmodel.setItem3HW(ItemName3HW);
            layoutmodel.setItem4HW(ItemName4HW);
            layoutmodel.setItem5HW(ItemName5HW);
            layoutmodel.setItem6HW(ItemName6HW);


            layoutmodel.setRole(role);
            layoutmodel.setChampionName2(ChampionName);
            layoutmodel.setChamppos(champpos);
            LayoutModelList.add(layoutmodel);

            return LayoutModelList;
        }

所以这段代码从这个json返回 - http://api.champion.gg/champion/ekko/&#34;这张图片enter image description here

哦,如果这有助于继承listview所在的XML;

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/scrollView" >

        <ListView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/listView"
            android:layout_weight="1"
            android:layout_alignParentTop="true" />
    </ScrollView>
</LinearLayout>

2 个答案:

答案 0 :(得分:2)

您将在forloop中返回您的arraylist,因此它只会向列表中添加一个对象。您需要将return语句放在for循环之外。

for (int i = 0; i < jsonarray.length(); i++) {
    //Your json logic
    LayoutModelList.add(layoutmodel);
}
return LayoutModelList;

答案 1 :(得分:1)

你的问题在这里

android:layout_height="wrap_content"
中的

listviews不能包装内容,它们必须是设置大小或填充父级。为什么在滚动视图中有列表视图?我认为你应该只有一个listview

试试这个:

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


        <ListView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/listView" />

</LinearLayout>