Is It Possible To Change The Childposition Id Of The Expandablelistview?
just want to know if it is possible to change the childPosition id of the expandablelistview? because i want to change the childPosition id to my category id.. this is my DataChild
Solution 1:
First you need to create a Model class (lets say Category), that contains the name
and the id
:
publicclassCategory{
publicString name;
publicString id;
}
Then load a list of Category object from your json:
List<Category> CATEGORY = newArrayList<Category>();
for(int i=0;i<categories.length();i++){
JSONObjectc= categories.getJSONObject(i);
Categorycategory=newCategory();
category.name = c.getString(TAG_NAME);
category.id = c.getString(TAG_ID);
CATEGORY.add(category);
}
Finally pass a HashMap<String, List<Category>>
as parameter in your adapter instead of HashMap<String, List<String>>
. And change your adapter methods to support Category object instead of String.
So when you call getChild()
method, it will return a Category
object where you can find the name
and the id
(Category.name
, Category.id
)
Update
Change your adapter class to:
publicclassExpandableListAdapterextendsBaseExpandableListAdapter{
private Context _context;
private List<String> _listDataHeader; // header titles// child data in format of header title, child titleprivate HashMap<String, List<Category>> _listDataChild;
public ExpandableListAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<Category>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}
@Overridepublic Category getChild(int groupPosition, int childPosititon) {
returnthis._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosititon);
}
@Overridepublic long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Overridepublic View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final Category childCategory = (Category) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item, null);
}
TextView txtListChild = (TextView) convertView
.findViewById(R.id.lblListItem);
txtListChild.setText(childCategory.name);
return convertView;
}
@Overridepublic int getChildrenCount(int groupPosition) {
returnthis._listDataChild.get(this._listDataHeader.get(groupPosition))
.size();
}
@Overridepublic Object getGroup(int groupPosition) {
returnthis._listDataHeader.get(groupPosition);
}
@Overridepublic int getGroupCount() {
returnthis._listDataHeader.size();
}
@Overridepublic long getGroupId(int groupPosition) {
return groupPosition;
}
@Overridepublic 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.list_group, null);
}
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.lblListHeader);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
return convertView;
}
@Overridepublic boolean hasStableIds() {
returnfalse;
}
@Overridepublic boolean isChildSelectable(int groupPosition, int childPosition) {
returntrue;
}
}
And in your click listener:
expListView.setOnChildClickListener(newExpandableListView.OnChildClickListener() {
@OverridepublicbooleanonChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stubStringheader= listDataHeader.get(groupPosition);
Categorychild= listDataChild.get(header).get(childPosition);
// child.name to get the name// child.id to get the id
Toast.makeText(getActivity(),"ChildNme: "+child.name+" ChildId: "+child.id, Toast.LENGTH_SHORT).show();
returnfalse;
}
});
Post a Comment for "Is It Possible To Change The Childposition Id Of The Expandablelistview?"