gpt4 book ai didi

java - 只有最后一个元素在 listView 中多次显示

转载 作者:行者123 更新时间:2023-12-02 04:10:26 27 4
gpt4 key购买 nike

我有这个方法,可以将元素添加到 GetterSetter 类中,并将它们添加到 arrayList 中,并添加到 listView 中。事情是在 listView 中,它只多次显示数组中的最后一项。
我不明白为什么?有什么解决办法吗?

这是我的代码:

protected void onPostExecute(String str) {

myArrayList = new ArrayList<GetterSetter>();
addValues = new GetterSetter();
String rating="-NA-";
try {

JSONObject root = new JSONObject(str);
JSONArray results = root.getJSONArray("results");
for (int i = 0; i < results.length(); i++) {
JSONObject arrayItems = results.getJSONObject(i);
JSONObject geometry = arrayItems.getJSONObject("geometry");
JSONObject location = geometry.getJSONObject("location");

if(!arrayItems.isNull("rating")){
rating = arrayItems.getString("rating");
}

addValues.setLat(location.getString("lat"));
addValues.setLon(location.getString("lng"));
addValues.setName(arrayItems.getString("name").toString());
addValues.setRating(rating);
addValues.setVicinity(arrayItems.getString("vicinity").toString());

Log.d("Before", myArrayList.toString());

myArrayList.add(addValues);

}
} catch (Exception e) {

}
System.out
.println("############################################################################");
Log.d("After:", myArrayList.toString());
nodata = (TextView) findViewById(R.id.nodata);
nodata.setVisibility(View.GONE);
adapter = new CustomAdapter(ListActivity.this, R.layout.list_row, myArrayList);
myList.setAdapter(adapter);
//adapter.notifyDataSetChanged();
dialog.dismiss();
}

}

这是我的猫日志:

11-21 21:13:03.845  32431-32431/pfe.com.neighborhoodserviceslbs D/Before﹕ []
11-21 21:13:03.845 32431-32431/pfe.com.neighborhoodserviceslbs D/Before
[Stefano's4.3El-Gaish Road]
11-21 21:13:03.845 32431-32431/pfe.com.neighborhoodserviceslbs D/Before﹕
[McDonald's4.3Smouha, Victor Emanouil Al Thaleth, Qism Sidi Gabir,
McDonald's4.3Smouha, Victor Emanouil Al Thaleth, Qism Sidi Gabir]
11-21 21:13:03.845 32431-32431/pfe.com.neighborhoodserviceslbs D/Before﹕
[Sudoku Cafe سودوكو كافيه4Alexandria, 49 Mostafa Kamel street 49 شارع
مصطفي كامل, Smouha, Sudoku Cafe سودوكو كافيه4Alexandria, 49 Mostafa Kamel
street 49 شارع مصطفي كامل, Smouha, Sudoku Cafe سودوكو كافيه4Alexandria,
49 Mostafa Kamel street 49 شارع مصطفي كامل, Smouha]
11-21 21:13:03.848 32431-32431/pfe.com.neighborhoodserviceslbs D/Before﹕
[Club 21 Cafe & Restaurant4.3Mustafa Kamel WA Bolkli, Qesm Sidi Gaber,
Club 21 Cafe & Restaurant4.3Mustafa Kamel WA Bolkli, Qesm Sidi Gaber, Club
21 Cafe & Restaurant4.3Mustafa Kamel WA Bolkli, Qesm Sidi Gaber, Club 21
Cafe & Restaurant4.3Mustafa Kamel WA Bolkli, Qesm Sidi Gaber, Club 21 Cafe
& Restaurant4.3Mustafa Kamel WA Bolkli, Qesm Sidi Gaber, Club 21 Cafe &
Restaurant4.3Mustafa Kamel WA Bolkli, Qesm Sidi Gaber, Club 21 Cafe &
Restaurant4.3Mustafa Kamel WA Bolkli, Qesm Sidi Gaber, Club 21 Cafe &
Restaurant4.3Mustafa Kamel WA Bolkli, Qesm Sidi Gaber, Club 21 Cafe &
Restaurant4.3Mustafa Kamel WA Bolkli, Qesm Sidi Gaber, Club 21 Cafe &
Restaurant4.3Mustafa Kamel WA Bolkli, Qesm Sidi Gaber, Club 21 Cafe &
Restaurant4.3Mustafa Kamel WA Bolkli, Qesm Sidi Gaber, Club 21 Cafe &
Restaurant4.3Mustafa Kamel WA Bolkli, Qesm Sidi Gaber, Club 21 Cafe &
Restaurant4.3Mustafa Kamel WA Bolkli, Qesm Sidi Gaber, Club 21 Cafe &
Restaurant4.3Mustafa Kamel WA Bolkli, Qesm Sidi Gaber, Club 21 Cafe &
Restaurant4.3Mustafa Kamel WA Bolkli, Qesm Sidi Gaber, Club 21 Cafe &
Restaurant4.3Mustafa Kamel WA Bolkli, Qesm Sidi Gaber, Club 21 Cafe &
Restaurant4.3Mustafa Kamel WA Bolkli, Qesm Sidi Gaber, Club 21 Cafe &
Restaurant4.3Mustafa Kamel WA Bolkli, Qesm Sidi Gaber, Club 21 Cafe &
Restaurant4.3Mustafa Kamel WA Bolkli, Qesm Sidi Gaber]
11-21 21:13:03.850 32431-32431/pfe.com.neighborhoodserviceslbs I/System.out﹕
############################################################################
11-21 21:13:03.850 32431-32431/pfe.com.neighborhoodserviceslbs D/After:﹕
[Club 21 Cafe & Restaurant4.3Mustafa Kamel WA Bolkli, Qesm Sidi Gaber,
Club 21 Cafe & Restaurant4.3Mustafa Kamel WA Bolkli, Qesm Sidi Gaber, Club
21 Cafe & Restaurant4.3Mustafa Kamel WA Bolkli, Qesm Sidi Gaber, Club 21
Cafe & Restaurant4.3Mustafa Kamel WA Bolkli, Qesm Sidi Gaber, Club 21 Cafe
& Restaurant4.3Mustafa Kamel WA Bolkli, Qesm Sidi Gaber, Club 21 Ca.....]

这是适配器代码:

public class CustomAdapter extends ArrayAdapter<GetterSetter> {

Context context;
ArrayList<GetterSetter> placesArray;
int textViewResourceId;

public CustomAdapter(Context context, int textViewResourceId, List<GetterSetter> objects) {
super(context, textViewResourceId, objects);
this.placesArray = (ArrayList<GetterSetter>) objects;
this.context = context;
this.textViewResourceId = textViewResourceId;

}

@Override public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
TextViewHolder holder = null;

if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(textViewResourceId, parent, false);

holder = new TextViewHolder();
holder.name = (TextView) row.findViewById(R.id.name);
holder.rating = (TextView) row.findViewById(R.id.rating);
holder.ratingText = (TextView) row.findViewById(R.id.ratingRext);
row.setTag(holder);
} else {
holder = (TextViewHolder) row.getTag();
}

holder.name.setText(placesArray.get(position).name);
holder.rating.setText(placesArray.get(position).rating);

return row;
}

static class TextViewHolder {
TextView name;
TextView rating;
TextView ratingText;
}
}

最佳答案

addValues = new GetterSetter(); 您正在使用一个对象,并多次添加同一个对象。

所以你应该在for循环中创建GetterSetter对象

因为你必须构造与解析的对象/数组中相同数量的对象

关于java - 只有最后一个元素在 listView 中多次显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33847543/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com