我已经编写了一个代码来将json项目显示到listview中,当我调试代码时我可以正确地看到数据,当我将列表设置为listview时我只看到最后一项
我试过的代码:
try {
foodintervallist.clear();
final String result = response.body().string();
JSONObject jsonObject = new JSONObject(result);
String data = jsonObject.getString("data");
JSONArray foodintervalarray = new JSONArray(data);
HashMap<String, String> menuMap =
new HashMap<String, String>();
for (int j = 0; j < foodintervalarray.length(); j++) {
String key1 = "";
JSONObject jsonObject1 =
foodintervalarray.getJSONObject(j);
final String food_interval =
jsonObject1.getString(FOOD_INTERVAL);
if (jsonObject1.isNull(ITEM_NAME)) {
item_name = "";
} else {
item_name = jsonObject1.getString(ITEM_NAME);
}
if (!menuMap.containsKey(food_interval)) {
menuMap.put(food_interval, item_name);
} else {
String key = menuMap.get(food_interval);
key1=key+","+item_name;
menuMap.put(food_interval, key1);
}
runOnUiThread(new Runnable() {
@Override
public void run() {
foodintervallist.add(menuMap);
listViewAdapter = new ListViewAdapter(Diet_Order_Activity_New.this, foodintervallist);
listView.setAdapter(listViewAdapter);
listViewAdapter.notifyDataSetChanged();
}
});
}
我的BaseAdapter类
@Override
public View getView(int position, View convertView, ViewGroup parent) {
inflater = (LayoutInflater)
activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.table_row, parent,
false);
resultp = fooditervallist.get(position);
txtFoodInterval = (TextView)
itemView.findViewById(R.id.foodInterval);
txtFoodItem = (TextView) itemView.findViewById(R.id.foodItems);
for (Map.Entry<String, String> entry : resultp.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
txtFoodInterval.setText(key);
Toast.makeText(activity, "" + key, Toast.LENGTH_LONG).show();
txtFoodItem.setText(value);
// do what you have to do here
// In your case, an other loop.
}
return itemView;
}
我在我的主要活动类中全局声明foodintervallist以及我在oncreate方法中初始化的listviewadapter
我在arraylist中获取数据,但我只能显示最后一项,该怎么办?
感谢名单
答案 0 :(得分:1)
我建议您检查一下Adapter的getCount()的实现。由于没有提供它看起来如何,我会看到那里...... 它应该是:
public int getCount (){ return fooditervallist.get(0).size() }
当您提供仅包含一个项目的列表时。另外我在getView()中看到了一些问题:
fooditervallist.get(position);
- &gt;不要在那里使用位置,你的列表总是只有一个项目因此使用0而不是你会得到空指针尽管如此,我会以不同的方式实施它:
class FoodItem {int interval;字符串名称; // getter和setter 这里}
我会将这些项目放在您放置地图的列表中
在适配器中,您可以非常轻松地使用此对象,而不需要任何for循环:
@Override public View getView(int position, View convertView, ViewGroup parent) { inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View itemView = inflater.inflate(R.layout.table_row, parent, false); FoodItem item = fooditervallist.get(position); txtFoodInterval = (TextView) itemView.findViewById(R.id.foodInterval); txtFoodItem = (TextView) itemView.findViewById(R.id.foodItems); txtFoodInterval.setText(item.interval) ; txtFoodItem.setText(item.name); return itemView;
}
另外,我建议您查看Retrofit库。它会让你的生活更轻松......
答案 1 :(得分:0)
将你的Hashmap放在for循环中。因为当您全局使用时,数据将被覆盖,并且您只获得最后一项。
答案 2 :(得分:0)
将以下代码放在循环外部//将适配器放在循环外
listViewAdapter = new ListViewAdapter(Diet_Order_Activity_New.this,foodintervallist);
listView.setAdapter(listViewAdapter);
listViewAdapter.notifyDataSetChanged();