【发布时间】:2014-12-16 00:02:52
【问题描述】:
我正在使用可展开的列表视图在菜单中分别显示选项和子选项。传入的数据是正确的,但奇怪的是,listview 似乎为每个数据条目调用了两次 getChild 和 getChildView,因此它被添加到视图中两次。它迭代,然后似乎再次迭代。
例子:
分类: -玩具 -电子产品 -汽车 -玩具 -电子产品 -汽车
预期: 分类: -玩具 -电子产品 -汽车
我对 Android 和 Java 非常陌生,所以我可能犯了一个新手错误。我已经逐步尝试找到会复制数据的点,但我能找到的只是 getChild/getChildView 被调用了两次。
这里是 expandableListView 适配器类:
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private ArrayList<String> filterHeaderList;
// child data in format of header title, child title
private HashMap<String, ArrayList<JSONObject>> filterOptionsMap;
public ExpandableListAdapter(Context context, ArrayList<String> filterHeaders, HashMap<String, ArrayList<JSONObject>> filterChildListMap) {
this.context = context;
this.filterHeaderList = filterHeaders;
this.filterOptionsMap = filterChildListMap;
}
@Override
public String[] getChild(int groupPosition, int childPosititon) {
JSONObject childObject = this.filterOptionsMap.get(this.filterHeaderList.get(groupPosition)).get(childPosititon);
if (childObject != null) {
Iterator<?> it = childObject.keys();
String key = null;
String value = null;
if (it.hasNext()) {
key = it.next().toString();
try {
value = childObject.getString(key);
} catch (JSONException e) {
e.printStackTrace();
}
String[] data = new String[2];
data[0] = key;
data[1] = value;
return data;
} else {
return null;
}
}
return null;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
String childNum = null;
String childText = null;
final String[] childData = (String[]) getChild(groupPosition, childPosition);
if (childData != null) {
childNum = childData[0];
childText = childData[1];
}
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.ps_filter_sort_filter_expandable_child, null);
}
TextView tvChildTitle = (TextView) convertView.findViewById(R.id.ps_filter_sort_filter_expandable_child_title);
tvChildTitle.setText(childText);
tvChildTitle.setTag(R.id.ps_filter_sort_filter_expandable_child_title + groupPosition + childPosition, childNum);
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return this.filterOptionsMap.get(this.filterHeaderList.get(groupPosition)).size();
}
@Override
public String getGroup(int groupPosition) {
return this.filterHeaderList.get(groupPosition);
}
@Override
public int getGroupCount() {
return this.filterHeaderList.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.ps_filter_sort_filter_expandable_parent, null);
}
TextView tvGroupTitle = (TextView) convertView.findViewById(R.id.ps_filter_sort_filter_expandable_parent_title);
tvGroupTitle.setTypeface(null, Typeface.BOLD);
tvGroupTitle.setText(headerTitle);
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
然后我这样称呼它:
filterListAdapter = new ExpandableListAdapter(this.getActivity(), filterHeaderList, filterOptionsMap);
// setting list adapter
expListView.setAdapter(filterListAdapter);
filterListAdapter.notifyDataSetChanged();
以这种方式组织数据是有原因的,我不想深入探讨。我能说的是HashMap“filterOptionsMap”是正确的,它或“filterHeaderList”没有重复
任何帮助将不胜感激。我找不到遇到类似问题的人,为此我一直在敲键盘。
谢谢。
附注 您还需要点击组标题两次以使其折叠,但要打开一次。我不知道这是否是相关的或预期的行为,但我希望它是一个点击来切换它打开或关闭。在这方面的帮助也可以加分。
这里要求的是一些与 HashMap 相关的附加代码:
private void prepareFilterListData(ArrayList<String> headerList, JSONObject[] categoryList, JSONObject[] sellerList, JSONObject[] typeList) {
filterHeaderArrayList = headerList;
filterOptionsMap = new HashMap<String, ArrayList<JSONObject>>();
// Adding child data
ArrayList<JSONObject> categories = new ArrayList<JSONObject>();
if (categoryList != null && categoryList.length > 0) {
for (JSONObject category : categoryList) {
categories.add(category);
}
}
ArrayList<JSONObject> sellers = new ArrayList<JSONObject>();
if (sellerList != null && sellerList.length > 0) {
for (JSONObject seller : sellerList) {
sellers.add(seller);
}
}
ArrayList<JSONObject> types = new ArrayList<JSONObject>();
if (typeList != null && typeList.length > 0) {
for (JSONObject type : typeList) {
types.add(type);
}
}
filterOptionsMap.put(filterHeaderArrayList.get(0), categories);
filterOptionsMap.put(filterHeaderArrayList.get(1), sellers);
filterOptionsMap.put(filterHeaderArrayList.get(2), types);
}
filterHeaderList = Directory.getFilterOptions();
filterCategoryList = Directory.getFilterCategories();
filterSellerList = Directory.getFilterSellers();
filterTypeList = Directory.getFilterTypes();
// Forms data structures from given input
if (filterHeaderList != null && filterCategoryList != null && filterSellerList != null && filterTypeList != null) {
prepareFilterListData(filterHeaderList, filterCategoryList, filterSellerList, filterTypeList);
private JSONObject[] getFilterTypes(JSONObject productSearchSettings, String filterTypes) {
JSONArray typesObj;
JSONObject[] types;
try {
typesObj = productSearchSettings.optJSONArray(filterTypes);
if (typesObj != null) {
types = new JSONObject[typesObj.length()];
for (int i = 0; i < typesObj.length(); i++) {
String key = "type" + String.valueOf(i);
String val = (String) typesObj.get(i).toString();
types[i] = new JSONObject().put(key, val);
}
return types;
} else {
return null;
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
private JSONObject[] getFilterSellers(JSONObject productSearchSettings, String filterSellers) {
JSONObject sellersObj = null;
JSONObject[] sellers;
try {
sellersObj = productSearchSettings.getJSONObject(filterSellers);
if (sellersObj != null) {
sellers = new JSONObject[sellersObj.length()];
Iterator<?> it = sellersObj.keys();
int i = 0;
while (it.hasNext()) {
String key = (String) String.valueOf(it.next().toString());
String val = sellersObj.getString(key);
sellers[i] = new JSONObject().put(key, val);
i++;
}
return sellers;
} else {
return null;
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
private JSONObject[] getFilterCategories(JSONObject productSearchSettings, String filterCategories) {
JSONObject categoriesObj = null;
JSONObject[] categories;
try {
categoriesObj = productSearchSettings.getJSONObject(filterCategories);
if (categoriesObj != null) {
categories = new JSONObject[categoriesObj.length()];
Iterator<?> it = categoriesObj.keys();
int i = 0;
while (it.hasNext()) {
String key = (String) String.valueOf(it.next().toString());
String val = categoriesObj.getString(key);
categories[i] = new JSONObject().put(key, val);
i++;
}
return categories;
} else {
return null;
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
如前所述,进入适配器的 hashmap 是正确的。我通过调试验证了。
【问题讨论】:
-
对于
ExpandableListAdapter,您的数据组织选择是正常的。看到getChildView和getGroupView的多次调用是预期行为。添加两次不是。您的适配器实现似乎没有什么突出的。您可以发布更多代码吗?基本上是与适配器或用于构造适配器的数据交互的任何代码部分。还包括它们所在的方法。 -
我添加了额外的代码。还可能值得注意的是,我正在尝试为组视图和子视图使用自定义视图,但我认为这些视图中没有任何可能导致重复的内容。
标签: java android json expandablelistview