如何使用 volley 在 Expandable ListView 中映射 childView
How to map childView in Expandable ListView using volley
我正在使用 Volley 在 android 中制作可扩展列表视图。我已成功映射 parentView。但是我遇到了如何在可扩展列表视图中映射 childView 的问题。在我的情况下,所有子类别都显示在一个父类别中。我该如何处理。我如何在可扩展列表视图中正确映射 childView。
这是我在 logcat.
中显示的列表快照
截图:
这是我的代码,我从中得到了 JSON 的回复。
String ChildName = null;
JSONObject object = null,childObject= null;
try {
object =new JSONObject(response);
JSONArray headerArray = object.getJSONArray("ListCategories");
for (int i=0;i<headerArray.length();i++){
JSONObject jsonObject = headerArray.getJSONObject(i);
final int Id = jsonObject.getInt("CategoryID");
final String name = jsonObject.getString("CategoryName");
JSONArray childArray = headerArray.getJSONObject(i).getJSONArray("ListChildCategories");
for (int j=0;j<childArray.length();j++) {
childObject = childArray.getJSONObject(j);
ChildId = childObject.getInt("ID");
ChildName = childObject.getString("Name");
child.add(new ChildCategory(ChildId, ChildName));
}
Log.e("childArray", childArray.toString());
header.add(new Category(Id, name));
}
} catch (JSONException e) {
e.printStackTrace();
}
adapter = new ExpandableCategoryAdapter(context,header,child);
expandableListView.setAdapter(adapter);
adapter.notifyDataSetChanged();
这里是可扩展类别适配器:
public class ExpandableCategoryAdapter extends BaseExpandableListAdapter {
private Context _context;
private ArrayList<Category> header;
private ArrayList<ChildCategory> childCategories;
private HashMap<String, String> child;
final String name = null;
private TextView header_text;
public ExpandableCategoryAdapter(Context context, ArrayList<Category> listDataHeader,ArrayList<ChildCategory> listDataChild) {
this._context = context;
this.header = listDataHeader;
this.childCategories = listDataChild;
}
@Override
public int getGroupCount() {
// Get header size
return header.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return childCategories.size();
}
@Override
public Object getGroup(int groupPosition) {
return header.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return childCategories.get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
ChildCategory child = (ChildCategory) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item_category, parent, false);
}
TextView child_text = (TextView) convertView.findViewById(R.id.expandedListItem);
child_text.setText(child.getName());
return convertView;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return true;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
Category category = (Category) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_group_category, parent, false);
}
header_text = (TextView) convertView.findViewById(R.id.listTitle);
header_text.setText(category.getCategoryName());
if (isExpanded) {
header_text.setTypeface(null, Typeface.BOLD);
header_text.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_extract_black_24dp, 0);
} else {
header_text.setTypeface(null, Typeface.NORMAL);
header_text.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_add_black_24dp, 0);
}
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
我不知道我哪里做错了。提前谢谢你
child list应该按照header/group打散,所以有一个allChild list为ArrayList<ArrayList<ChildCategory>>
。试试这个:
String ChildName = null;
JSONObject object = null,childObject= null;
ArrayList<ArrayList<ChildCategory>> allChild = new ArrayList<ArrayList<ChildCategory>>(); // Added
try {
object =new JSONObject(response);
JSONArray headerArray = object.getJSONArray("ListCategories");
for (int i=0;i<headerArray.length();i++){
JSONObject jsonObject = headerArray.getJSONObject(i);
final int Id = jsonObject.getInt("CategoryID");
final String name = jsonObject.getString("CategoryName");
JSONArray childArray = headerArray.getJSONObject(i).getJSONArray("ListChildCategories");
child = new ArrayList<ChildCategory>(); // Added
for (int j=0;j<childArray.length();j++) {
childObject = childArray.getJSONObject(j);
ChildId = childObject.getInt("ID");
ChildName = childObject.getString("Name");
child.add(new ChildCategory(ChildId, ChildName));
}
Log.e("childArray", childArray.toString());
allChild.add(child); // Added
header.add(new Category(Id, name));
}
} catch (JSONException e) {
e.printStackTrace();
}
adapter = new ExpandableCategoryAdapter(context,header,allChild); // Changed
expandableListView.setAdapter(adapter);
适配器:
public class ExpandableCategoryAdapter extends BaseExpandableListAdapter {
private Context _context;
private ArrayList<Category> header;
private ArrayList<ArrayList<ChildCategory>> childCategories; // Changed
private HashMap<String, String> child;
final String name = null;
private TextView header_text;
public ExpandableCategoryAdapter(Context context, ArrayList<Category> listDataHeader,ArrayList<ArrayList<ChildCategory>> listDataChild) { //Changed
this._context = context;
this.header = listDataHeader;
this.childCategories = listDataChild;
}
@Override
public int getGroupCount() {
// Get header size
return header.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return childCategories.get(groupPosition).size(); // Changed
}
@Override
public Object getGroup(int groupPosition) {
return header.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return childCategories.get(groupPosition).get(childPosition); // Changed
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
ChildCategory child = (ChildCategory) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item_category, parent, false);
}
TextView child_text = (TextView) convertView.findViewById(R.id.expandedListItem);
child_text.setText(child.getName());
return convertView;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return true;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
Category category = (Category) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_group_category, parent, false);
}
header_text = (TextView) convertView.findViewById(R.id.listTitle);
header_text.setText(category.getCategoryName());
if (isExpanded) {
header_text.setTypeface(null, Typeface.BOLD);
header_text.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_extract_black_24dp, 0);
} else {
header_text.setTypeface(null, Typeface.NORMAL);
header_text.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_add_black_24dp, 0);
}
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
希望对您有所帮助!
我正在使用 Volley 在 android 中制作可扩展列表视图。我已成功映射 parentView。但是我遇到了如何在可扩展列表视图中映射 childView 的问题。在我的情况下,所有子类别都显示在一个父类别中。我该如何处理。我如何在可扩展列表视图中正确映射 childView。 这是我在 logcat.
中显示的列表快照截图:
这是我的代码,我从中得到了 JSON 的回复。
String ChildName = null;
JSONObject object = null,childObject= null;
try {
object =new JSONObject(response);
JSONArray headerArray = object.getJSONArray("ListCategories");
for (int i=0;i<headerArray.length();i++){
JSONObject jsonObject = headerArray.getJSONObject(i);
final int Id = jsonObject.getInt("CategoryID");
final String name = jsonObject.getString("CategoryName");
JSONArray childArray = headerArray.getJSONObject(i).getJSONArray("ListChildCategories");
for (int j=0;j<childArray.length();j++) {
childObject = childArray.getJSONObject(j);
ChildId = childObject.getInt("ID");
ChildName = childObject.getString("Name");
child.add(new ChildCategory(ChildId, ChildName));
}
Log.e("childArray", childArray.toString());
header.add(new Category(Id, name));
}
} catch (JSONException e) {
e.printStackTrace();
}
adapter = new ExpandableCategoryAdapter(context,header,child);
expandableListView.setAdapter(adapter);
adapter.notifyDataSetChanged();
这里是可扩展类别适配器:
public class ExpandableCategoryAdapter extends BaseExpandableListAdapter {
private Context _context;
private ArrayList<Category> header;
private ArrayList<ChildCategory> childCategories;
private HashMap<String, String> child;
final String name = null;
private TextView header_text;
public ExpandableCategoryAdapter(Context context, ArrayList<Category> listDataHeader,ArrayList<ChildCategory> listDataChild) {
this._context = context;
this.header = listDataHeader;
this.childCategories = listDataChild;
}
@Override
public int getGroupCount() {
// Get header size
return header.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return childCategories.size();
}
@Override
public Object getGroup(int groupPosition) {
return header.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return childCategories.get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
ChildCategory child = (ChildCategory) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item_category, parent, false);
}
TextView child_text = (TextView) convertView.findViewById(R.id.expandedListItem);
child_text.setText(child.getName());
return convertView;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return true;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
Category category = (Category) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_group_category, parent, false);
}
header_text = (TextView) convertView.findViewById(R.id.listTitle);
header_text.setText(category.getCategoryName());
if (isExpanded) {
header_text.setTypeface(null, Typeface.BOLD);
header_text.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_extract_black_24dp, 0);
} else {
header_text.setTypeface(null, Typeface.NORMAL);
header_text.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_add_black_24dp, 0);
}
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
我不知道我哪里做错了。提前谢谢你
child list应该按照header/group打散,所以有一个allChild list为ArrayList<ArrayList<ChildCategory>>
。试试这个:
String ChildName = null;
JSONObject object = null,childObject= null;
ArrayList<ArrayList<ChildCategory>> allChild = new ArrayList<ArrayList<ChildCategory>>(); // Added
try {
object =new JSONObject(response);
JSONArray headerArray = object.getJSONArray("ListCategories");
for (int i=0;i<headerArray.length();i++){
JSONObject jsonObject = headerArray.getJSONObject(i);
final int Id = jsonObject.getInt("CategoryID");
final String name = jsonObject.getString("CategoryName");
JSONArray childArray = headerArray.getJSONObject(i).getJSONArray("ListChildCategories");
child = new ArrayList<ChildCategory>(); // Added
for (int j=0;j<childArray.length();j++) {
childObject = childArray.getJSONObject(j);
ChildId = childObject.getInt("ID");
ChildName = childObject.getString("Name");
child.add(new ChildCategory(ChildId, ChildName));
}
Log.e("childArray", childArray.toString());
allChild.add(child); // Added
header.add(new Category(Id, name));
}
} catch (JSONException e) {
e.printStackTrace();
}
adapter = new ExpandableCategoryAdapter(context,header,allChild); // Changed
expandableListView.setAdapter(adapter);
适配器:
public class ExpandableCategoryAdapter extends BaseExpandableListAdapter {
private Context _context;
private ArrayList<Category> header;
private ArrayList<ArrayList<ChildCategory>> childCategories; // Changed
private HashMap<String, String> child;
final String name = null;
private TextView header_text;
public ExpandableCategoryAdapter(Context context, ArrayList<Category> listDataHeader,ArrayList<ArrayList<ChildCategory>> listDataChild) { //Changed
this._context = context;
this.header = listDataHeader;
this.childCategories = listDataChild;
}
@Override
public int getGroupCount() {
// Get header size
return header.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return childCategories.get(groupPosition).size(); // Changed
}
@Override
public Object getGroup(int groupPosition) {
return header.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return childCategories.get(groupPosition).get(childPosition); // Changed
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
ChildCategory child = (ChildCategory) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item_category, parent, false);
}
TextView child_text = (TextView) convertView.findViewById(R.id.expandedListItem);
child_text.setText(child.getName());
return convertView;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return true;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
Category category = (Category) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_group_category, parent, false);
}
header_text = (TextView) convertView.findViewById(R.id.listTitle);
header_text.setText(category.getCategoryName());
if (isExpanded) {
header_text.setTypeface(null, Typeface.BOLD);
header_text.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_extract_black_24dp, 0);
} else {
header_text.setTypeface(null, Typeface.NORMAL);
header_text.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_add_black_24dp, 0);
}
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
希望对您有所帮助!