java – Android ExpandableListView填充两次
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – Android ExpandableListView填充两次,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含7706字,纯文字阅读大概需要12分钟。
内容图文
![java – Android ExpandableListView填充两次](/upload/InfoBanner/zyjiaocheng/756/68e60be41e234711a3ed4504d316fbac.jpg)
我正在使用可扩展列表视图分别在菜单中显示选项和子选项.进来的数据是正确的,但奇怪的是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是正确的.我通过调试验证了.
解决方法:
我终于找到了.我非常感谢您的反馈和帮助.
在该组的单击事件中,我没有返回true表示已处理单击.我正在使用click事件来切换扩展.
我不知道为什么会导致重复,并且还需要两次单击才能关闭该组,但是为该组上的click事件切换该返回会产生重大影响.
内容总结
以上是互联网集市为您收集整理的java – Android ExpandableListView填充两次全部内容,希望文章能够帮你解决java – Android ExpandableListView填充两次所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。