我会尽力解释发生了什么,所以我的代码有一个forloop填充列表视图,这个listview显示json文件中的所有正确数据,图像的excpet,图像总是从最后位置在列表视图中,然后复制到上面的那个,例如
这是一个单一的活动,我只是为了使其适合而进行片段化。我猜它与我为创建图像而创建的字符串有关,它们以某种方式被使用了两次,然后再次用于填充列表视图,即使所有其他字符串都被重置并在其获取时被填充来自JSON的新信息。如果有人能告诉我为什么这只发生在图像而不是创建项目名称的字符串
String ChampionName;
String item2;
String item3;
String item4;
private String ItemName;
private String ItemName2;
private String ItemName3;
private String ItemName4;
private String ItemName5;
private String ItemName6;
private String ItemNameHW;
private String ItemName2HW;
private String ItemName3HW;
private String ItemName4HW;
private String ItemName5HW;
private String ItemName6HW;
private ListView Champ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// http://api.champion.gg/champion/Ekko/
new JSONTask().execute("http://api.champion.gg/champion/aatrox/");
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));
StringBuilder buffer = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
String finalJson = buffer.toString();
JSONArray jsonarray = new JSONArray(finalJson);
List<Layoutmodel> LayoutModelList = new ArrayList<>();
//Number changes to what ever role you want it to be
//JSONObject finalObject = jsonarray.getJSONObject(jsonarray.length()-1);
for (int k = 0; k < jsonarray.length(); k++) {
JSONObject finalObject = jsonarray.getJSONObject(k);
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");
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;
} catch (JSONException | IOException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(List<Layoutmodel> result) {
super.onPostExecute(result);
LayoutAdapter adapter2 = new LayoutAdapter(getApplicationContext(), R.layout.rows, result);
Champ.setAdapter(adapter2);
}
}
public class LayoutAdapter extends ArrayAdapter {
private List<Layoutmodel> LayoutModelList2;
private int resource;
private LayoutInflater inflater;
public LayoutAdapter(Context context2, int resource, List<Layoutmodel> objects) {
super(context2, resource, objects);
LayoutModelList2 = objects;
this.resource = resource;
inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position2, View convertView2, ViewGroup parent2) {
ViewHolder holder = null;
if (convertView2 == null) {
holder = new ViewHolder();
convertView2 = inflater.inflate(resource, null);
holder.nameofchamp = (TextView) convertView2.findViewById(R.id.textView);
holder.position = (TextView) convertView2.findViewById(R.id.smalltxt);
holder.Champposition = (TextView) convertView2.findViewById(R.id.textView17);
String ItemNameInLowerCase;
String ItemName2InLowerCase;
String ItemName3InLowerCase;
String ItemName4InLowerCase;
String ItemName5InLowerCase;
String ItemName6InLowerCase;
String ItemName2InLowerCaseHW;
String ItemName3InLowerCaseHW;
String ItemName4InLowerCaseHW;
String ItemName5InLowerCaseHW;
String ItemName6InLowerCaseHW;
holder.Champitemimg1 = (ImageView) convertView2.findViewById(R.id.imageView7);
holder.Champitemimg2 = (ImageView) convertView2.findViewById(R.id.imageView6);
holder.Champitemimg3 = (ImageView) convertView2.findViewById(R.id.imageView5);
holder.Champitemimg4 = (ImageView) convertView2.findViewById(R.id.imageView4);
holder.Champitemimg5 = (ImageView) convertView2.findViewById(R.id.imageView3);
holder.Champitemimg6 = (ImageView) convertView2.findViewById(R.id.imageView2);
holder.Champitemimg1HW = (ImageView) convertView2.findViewById(R.id.imageView8);
holder.Champitemimg2HW = (ImageView) convertView2.findViewById(R.id.imageView9);
holder.Champitemimg3HW = (ImageView) convertView2.findViewById(R.id.imageView10);
holder.Champitemimg4HW = (ImageView) convertView2.findViewById(R.id.imageView11);
holder.Champitemimg5HW = (ImageView) convertView2.findViewById(R.id.imageView12);
holder.Champitemimg6HW = (ImageView) convertView2.findViewById(R.id.imageView13);
ImageView imgtesta;
String ChampionNameinlower;
imgtesta = (ImageView) convertView2.findViewById(R.id.imageView);
ChampionNameinlower = ChampionName.toLowerCase().replaceAll("'", "").replaceAll(" ", "");
int id10 = getResources().getIdentifier("com.example.kripzy.url:drawable/" + ChampionNameinlower + "_square_0", null, null);
imgtesta.setImageResource(id10);
holder.champitem1 = (TextView) convertView2.findViewById(R.id.textView9);
holder.champitem2 = (TextView) convertView2.findViewById(R.id.textView8);
holder.champitem3 = (TextView) convertView2.findViewById(R.id.textView7);
holder.champitem4 = (TextView) convertView2.findViewById(R.id.textView6);
holder.champitem5 = (TextView) convertView2.findViewById(R.id.textView5);
holder.champitem6 = (TextView) convertView2.findViewById(R.id.textView4);
holder.champitem1HW = (TextView) convertView2.findViewById(R.id.textView10);
holder.champitem2HW = (TextView) convertView2.findViewById(R.id.textView11);
holder.champitem3HW = (TextView) convertView2.findViewById(R.id.textView12);
holder.champitem4HW = (TextView) convertView2.findViewById(R.id.textView13);
holder.champitem5HW = (TextView) convertView2.findViewById(R.id.textView14);
holder.champitem6HW = (TextView) convertView2.findViewById(R.id.textView15);
holder.nameofchamp.setText(LayoutModelList2.get(position2).getChampionName2());
holder.position.setText(LayoutModelList2.get(position2).getRole());
holder.Champposition.setText(LayoutModelList2.get(position2).getChamppos());
holder.champitem1.setText(LayoutModelList2.get(position2).getItem1());
holder.champitem2.setText(LayoutModelList2.get(position2).getItem2());
holder.champitem3.setText(LayoutModelList2.get(position2).getItem3());
holder.champitem4.setText(LayoutModelList2.get(position2).getItem4());
holder.champitem5.setText(LayoutModelList2.get(position2).getItem5());
holder.champitem6.setText(LayoutModelList2.get(position2).getItem6());
holder.champitem1HW.setText(LayoutModelList2.get(position2).getItem1HW());
holder.champitem2HW.setText(LayoutModelList2.get(position2).getItem2HW());
holder.champitem3HW.setText(LayoutModelList2.get(position2).getItem3HW());
holder.champitem4HW.setText(LayoutModelList2.get(position2).getItem4HW());
holder.champitem5HW.setText(LayoutModelList2.get(position2).getItem5HW());
holder.champitem6HW.setText(LayoutModelList2.get(position2).getItem6HW());
ItemNameInLowerCase = ItemName.toLowerCase().replaceAll("'", "").replaceAll(" ", "");
ItemName2InLowerCase = ItemName2.toLowerCase().replaceAll("'", "").replaceAll(" ", "");
ItemName3InLowerCase = ItemName3.toLowerCase().replaceAll("'", "").replaceAll(" ", "");
ItemName4InLowerCase = ItemName4.toLowerCase().replaceAll("'", "").replaceAll(" ", "");
ItemName5InLowerCase = ItemName5.toLowerCase().replaceAll("'", "").replaceAll(" ", "");
ItemName6InLowerCase = ItemName6.toLowerCase().replaceAll("'", "").replaceAll(" ", "");
String itemNameInLowerCaseHW = ItemNameHW.toLowerCase().replaceAll("'", "").replaceAll(" ", "");
ItemName2InLowerCaseHW = ItemName2HW.toLowerCase().replaceAll("'", "").replaceAll(" ", "");
ItemName3InLowerCaseHW = ItemName3HW.toLowerCase().replaceAll("'", "").replaceAll(" ", "");
ItemName4InLowerCaseHW = ItemName4HW.toLowerCase().replaceAll("'", "").replaceAll(" ", "");
ItemName5InLowerCaseHW = ItemName5HW.toLowerCase().replaceAll("'", "").replaceAll(" ", "");
ItemName6InLowerCaseHW = ItemName6HW.toLowerCase().replaceAll("'", "").replaceAll(" ", "");
int id = getResources().getIdentifier("com.example.kripzy.url:drawable/" + ItemNameInLowerCase, null, null);
holder.Champitemimg1.setImageResource(id);
int id2 = getResources().getIdentifier("com.example.kripzy.url:drawable/" + ItemName2InLowerCase, null, null);
holder.Champitemimg2.setImageResource(id2);
int id3 = getResources().getIdentifier("com.example.kripzy.url:drawable/" + ItemName3InLowerCase, null, null);
holder.Champitemimg3.setImageResource(id3);
int id4 = getResources().getIdentifier("com.example.kripzy.url:drawable/" + ItemName4InLowerCase, null, null);
holder.Champitemimg4.setImageResource(id4);
int id5 = getResources().getIdentifier("com.example.kripzy.url:drawable/" + ItemName5InLowerCase, null, null);
holder.Champitemimg5.setImageResource(id5);
int id6 = getResources().getIdentifier("com.example.kripzy.url:drawable/" + ItemName6InLowerCase, null, null);
holder.Champitemimg6.setImageResource(id6);
int idHW = getResources().getIdentifier("com.example.kripzy.url:drawable/" + itemNameInLowerCaseHW, null, null);
holder.Champitemimg1HW.setImageResource(idHW);
int id2HW = getResources().getIdentifier("com.example.kripzy.url:drawable/" + ItemName2InLowerCaseHW, null, null);
holder.Champitemimg2HW.setImageResource(id2HW);
int id3HW = getResources().getIdentifier("com.example.kripzy.url:drawable/" + ItemName3InLowerCaseHW, null, null);
holder.Champitemimg3HW.setImageResource(id3HW);
int id4HW = getResources().getIdentifier("com.example.kripzy.url:drawable/" + ItemName4InLowerCaseHW, null, null);
holder.Champitemimg4HW.setImageResource(id4HW);
int id5HW = getResources().getIdentifier("com.example.kripzy.url:drawable/" + ItemName5InLowerCaseHW, null, null);
holder.Champitemimg5HW.setImageResource(id5HW);
int id6HW = getResources().getIdentifier("com.example.kripzy.url:drawable/" + ItemName6InLowerCaseHW, null, null);
holder.Champitemimg6HW.setImageResource(id6HW);
convertView2.setTag(holder);
}
else{
holder = (ViewHolder) convertView2.getTag();
}
return convertView2;
}
新添加的持有人;
TextView nameofchamp;
TextView position;
TextView Champposition;
TextView champitem1;
TextView champitem2;
TextView champitem3;
TextView champitem4;
TextView champitem5;
TextView champitem6;
TextView champitem1HW;
TextView champitem2HW;
TextView champitem3HW;
TextView champitem4HW;
TextView champitem5HW;
TextView champitem6HW;
ImageView Champitemimg1;
ImageView Champitemimg2;
ImageView Champitemimg3;
ImageView Champitemimg4;
ImageView Champitemimg5;
ImageView Champitemimg6;
ImageView Champitemimg1HW;
ImageView Champitemimg2HW;
ImageView Champitemimg3HW;
ImageView Champitemimg4HW;
ImageView Champitemimg5HW;
ImageView Champitemimg6HW;
}
}
当我将log.v放在代码的底部时,它总是回复此
05-21 14:41:40.646 4536-4536/com.example.kripzy.url V/Where is my error: enchantment:bloodrazor
05-21 14:41:40.659 4536-4536/com.example.kripzy.url V/Where is my error: enchantment:bloodrazor
答案 0 :(得分:0)
所以我做错了是因为我只是使用了一个字符串,它只包含listview上最后一个条目的信息,因此为什么它只显示来自它的图像,所以为了解决这个问题,我必须将其更改为以下;
ItemNameInLowerCase = holder.champitem1.getText().toString().toLowerCase().replaceAll("'", "").replaceAll(" ", "");
ItemName2InLowerCase = holder.champitem2.getText().toString().toLowerCase().replaceAll("'", "").replaceAll(" ", "");
ItemName3InLowerCase = holder.champitem3.getText().toString().toLowerCase().replaceAll("'", "").replaceAll(" ", "");
ItemName4InLowerCase = holder.champitem4.getText().toString().toLowerCase().replaceAll("'", "").replaceAll(" ", "");
ItemName5InLowerCase = holder.champitem5.getText().toString().toLowerCase().replaceAll("'", "").replaceAll(" ", "");
ItemName6InLowerCase = holder.champitem6.getText().toString().toLowerCase().replaceAll("'", "").replaceAll(" ", "");
ItemNameInLowerCaseHW = holder.champitem1HW.getText().toString().toLowerCase().replaceAll("'", "").replaceAll(" ", "");
ItemName2InLowerCaseHW = holder.champitem2HW.getText().toString().toLowerCase().replaceAll("'", "").replaceAll(" ", "");
ItemName3InLowerCaseHW = holder.champitem3HW.getText().toString().toLowerCase().replaceAll("'", "").replaceAll(" ", "");
ItemName4InLowerCaseHW = holder.champitem4HW.getText().toString().toLowerCase().replaceAll("'", "").replaceAll(" ", "");
ItemName5InLowerCaseHW = holder.champitem5HW.getText().toString().toLowerCase().replaceAll("'", "").replaceAll(" ", "");
ItemName6InLowerCaseHW = holder.champitem6HW.getText().toString().toLowerCase().replaceAll("'", "").replaceAll(" ", "");