我试图在listview中显示json数组的值。但它在所有列表视图按钮中显示重复的条目。这是我的代码
MenuFragment:
JSONObject json = jParser.makeHttpRequest(CommonUtilities.SERVERURL_GET_MENUS, "GET", params);
SUCCESS = json.getString(TAG_SUCCESS);
if (SUCCESS.equalsIgnoreCase("false")) {
JSONArray jsonarrays = json.getJSONArray("sub_menus");
HashMap<String, String> sb = new HashMap<String, String>();
for (int k = 0; k < jsonarrays.length(); k++) {
JSONObject main_menus = jsonarrays.getJSONObject(k);
SUB_MENU_ID = main_menus.getString("sub_menu_id");
SUB_MENU_NAME = main_menus.getString("sub_menu_name");
sb.put(SUB_MENU_ID, SUB_MENU_NAME);
submenus.add(sb);
} }
protected void onPostExecute(String result) {
try {
submenuadapter=new SubmenuCategoryAdapter(submenus,getActivity());
sb_list.setAdapter(submenuadapter);
}
SubmenuCategoryAdapter:
适配器包含用于显示列表值的按钮。
public class SubmenuCategoryAdapter extends BaseAdapter {
ArrayList<HashMap<String, String>> mainmenu;
HashMap<String, String> mainmenumap = new HashMap<String, String>();
Context con;
public SubmenuCategoryAdapter(ArrayList<HashMap<String, String>> mainmenu, Context con) {
super();
this.mainmenu = mainmenu;
this.con = con;
}
@Override
public int getCount() {
return mainmenu.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@SuppressLint("ViewHolder")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final Holder holder = new Holder();
LayoutInflater layoutInflater = (LayoutInflater) con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mainmenumap = mainmenu.get(position);
View submenuview = layoutInflater.inflate(R.layout.submenu_button, null);
holder.submenu_btn=(Button)submenuview.findViewById(R.id.submenu_layout_button);
String btn_name=mainmenu.get(position).toString();
holder.submenu_btn.setText(btn_name);
return submenuview;
}
public class Holder {
Button submenu_btn;
}
}
答案 0 :(得分:1)
将getView方法更改为以下
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder viewHolder;
if(convertView == null){
convertView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.submenu_button, parent, false);
viewHolder = new Holder();
viewHolder.submenu_btn = (Button) convertView
.findViewById(R.id.submenu_layout_button);
convertView.setTag(viewHolder);
} else {
viewHolder = (Holder) convertView.getTag();
}
String btn_name=mainmenu.get(position).toString();
viewHolder.submenu_btn.setText(btn_name);
return convertView;
}
<强>更新强>
要在评论中回答您的第二个问题,我不确定您为什么使用HashMaps的ArrayList。为什么不使用普通的java对象?我将在下面展示如何使用POJO(Plain Old Java Object)
创建描述菜单项的POJO
public class MenuItem {
public int MenuId;
public String MenuName;
}
MenuFragment
//initialize submenus
List<MenuItem> submenus = new ArrayList<>();
JSONArray jsonarrays = json.getJSONArray("sub_menus");
for (int k = 0; k < jsonarrays.length(); k++) {
JSONObject main_menus = jsonarrays.getJSONObject(k);
MenuItem item = new MenuItem();
item.MenuId = main_menus.getInt("sub_menu_id");
item.MenuName = main_menus.getString("sub_menu_name");
submenus.add(item);
}
//initializing adapter
submenuadapter = new SubmenuCategoryAdapter(submenus, getActivity());
sb_list.setAdapter(submenuadapter);
适配器类
public class SubmenuCategoryAdapter extends BaseAdapter {
List<MenuItem> mainmenu;
Context con;
public SubmenuCategoryAdapter(List<MenuItem> mainmenu, Context con) {
super();
this.mainmenu = mainmenu;
this.con = con;
}
....
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder viewHolder;
if(convertView == null){
convertView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.submenu_button, parent, false);
viewHolder = new Holder();
viewHolder.submenu_btn = (Button) convertView
.findViewById(R.id.submenu_layout_button);
convertView.setTag(viewHolder);
} else {
viewHolder = (Holder) convertView.getTag();
}
String btn_name = mainmenu.get(position).MenuName;
viewHolder.submenu_btn.setText(btn_name);
return convertView;
}
}