Kripzy Kripzy - 6 months ago 16
JSON Question

listview isnt being filled from my forloop, what have i done wrong?

i have a for loop which should fill my listview with data, however its only getting the data from the first position out of three in my json file, any help would be appreciated!

Heres my the code in question;

Edited version of code where i think the error lies

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;
}


So this code returns from this json - http://api.champion.gg/champion/ekko/" this image enter image description here

Oh and if this helps heres the XML of where the listview is;

<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>



Answer

You are returning your arraylist inside the forloop, therefor it will only add one object to the list. You need to place the return statement outside of the for loop.

return LayoutModelList;