Android 列表视图没有从我的forloop中填充,我做错了什么?
我有一个for循环,它应该用数据填充我的listview,但是它只从json文件中三个位置中的第一个位置获取数据,任何帮助都将不胜感激 这是我的密码 我认为错误所在的代码的编辑版本Android 列表视图没有从我的forloop中填充,我做错了什么?,android,json,listview,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/champi
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;
}
imgtest=(ImageView)findViewById(R.id.ImageView);
// http://api.champion.gg/champion/Ekko/
新建JSONTask()。执行(“http://api.champion.gg/champion/ekko/");
Champ=(ListView)findViewById(R.id.ListView);
}
公共类JSONTask扩展了异步任务{
@凌驾
受保护列表doInBackground(字符串…参数){
HttpURLConnection=null;
BufferedReader reader=null;
试一试{
URL=新URL(参数[0]);
connection=(HttpURLConnection)url.openConnection();
connection.connect();
InputStream=connection.getInputStream();
reader=新的BufferedReader(新的InputStreamReader(流));
StringBuffer=新的StringBuffer();
字符串行=”;
而((line=reader.readLine())!=null){
buffer.append(行);
}
字符串finalJson=buffer.toString();
JSONArray JSONArray=新JSONArray(最终JSON);
List LayoutModelList=新建ArrayList();
for(int i=0;i
这段代码从这个json返回-“这个图像
哦,如果这有帮助的话,下面是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>
您正在for循环内返回arraylist,因此它只会向列表中添加一个对象。您需要将return语句放在for循环外
for (int i = 0; i < jsonarray.length(); i++) {
//Your json logic
LayoutModelList.add(layoutmodel);
}
return LayoutModelList;
for(int i=0;i
您的问题就在这里
android:layout_height="wrap_content"
在您的列表视图中
listview不能是包装内容,它们必须是设置的大小或填充父级。为什么在scrollview中有listview?我认为您应该有一个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>