My Fragment didn't show anything for my expandable view. there's not error as well. and i don't know where went wrong so i need help Can someone let me know my error? There's no error on the page/didn't have any crashes as well
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
import java.util.HashMap;
import java.util.List;
public class MyExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private List<String> listGroup;
private HashMap<String, List<String>> listChild;
public MyExpandableListAdapter(Context context, List<String> listGroup,
HashMap<String, List<String>> listChild) {
this.context = context;
this.listGroup = listGroup;
this.listChild = listChild;
}
#Override
public int getGroupCount() {
return listGroup.size();
}
#Override
public int getChildrenCount(int groupPosition) {
return listChild.get(listGroup.get(groupPosition)).size();
}
#Override
public Object getGroup(int groupPosition) {
return listGroup.get(groupPosition);
}
#Override
public Object getChild(int groupPosition, int childPosition) {
return listChild.get(listGroup.get(groupPosition)).get(childPosition);
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.group_layout, null);
}
String textGroup = (String) getGroup(groupPosition);
TextView textViewGroup = (TextView) convertView
.findViewById(R.id.group);
textViewGroup.setText(textGroup);
return convertView;
}
#Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.item_layout, null);
}
TextView textViewItem = (TextView) convertView.findViewById(R.id.item);
String text = (String) getChild(groupPosition, childPosition);
textViewItem.setText(text);
return convertView;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return false;
}
}
public class Hotel extends Fragment {
ExpandableListView expandableListView;
MyExpandableListAdapter myExpandableListAdapter;
List<String> groupList;
HashMap<String, List<String>> childMap;
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.hotel_frag, container, false);
init();
expandableListView = (ExpandableListView) v.findViewById(R.id.mylist);
expandableListView.setAdapter(myExpandableListAdapter);
myExpandableListAdapter = new MyExpandableListAdapter(Hotel.this.getActivity().getApplicationContext(), groupList, childMap);
return v;
}
private void init() {
groupList = new ArrayList<String>();
childMap = new HashMap<String, List<String>>();
List<String> groupList0 = new ArrayList<String>();
groupList0.add("groupList0 - 1");
groupList0.add("groupList0 - 2");
groupList0.add("groupList0 - 3");
List<String> groupList1 = new ArrayList<String>();
groupList1.add("groupList1 - 1");
groupList1.add("groupList1 - 2");
groupList1.add("groupList1 - 3");
List<String> groupList2 = new ArrayList<String>();
groupList2.add("groupList2 - 1");
groupList2.add("groupList2 - 2");
groupList2.add("groupList2 - 3");
List<String> groupList3 = new ArrayList<String>();
groupList3.add("groupList3 - 1");
groupList3.add("groupList3 - 2");
groupList3.add("groupList3 - 3");
groupList.add("Group List 0");
groupList.add("Group List 1");
groupList.add("Group List 2");
groupList.add("Group List 3");
childMap.put(groupList.get(0), groupList0);
childMap.put(groupList.get(1), groupList1);
childMap.put(groupList.get(2), groupList2);
childMap.put(groupList.get(3), groupList3);
}
}
http://i.stack.imgur.com/dnM7Q.png
http://i.stack.imgur.com/eXJOz.png
http://i.stack.imgur.com/VmaUj.png
You have to construct the adapter before you assign it to the ListView:
init();
myExpandableListAdapter = new MyExpandableListAdapter(Hotel.this.getActivity().getApplicationContext(), groupList, childMap);
expandableListView = (ExpandableListView) v.findViewById(R.id.mylist);
expandableListView.setAdapter(myExpandableListAdapter);
Related
I am using Exapandable listview inside a fragment,i am passing 8 values but it is showing only 6 values and that too the exapnadable listview is not scrolling.the same if i put it in activity..everything works fine.but when i use within fragment it is showing error.can anyone tell me how to solve it...here is my code
public class GroupsFragment extends Fragment {
View groupview;
List<String> tdp, congress, ysrcp, bjp;
ExpandableListView expandableListView;
HashMap<String, ArrayList<GroupModel>> expandableListDetail;
ArrayList<GroupModel> contacts;
List<String> expandableListTitle;
ExpandableListAdapter expandableListAdapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the dailog_layout for this fragment
groupview = inflater.inflate(R.layout.contacts_expandable_list, container, false);
expandableListView = groupview.findViewById(R.id.expanded_list_view_contacts);
contacts = new ArrayList<>();
return groupview;
}
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
expandableListDetail = new HashMap<String, ArrayList<GroupModel>>();
showContactsList();
}
#SuppressLint("ClickableViewAccessibility")
private void showContactsList() {
getExpandableListItems();
expandableListTitle = new ArrayList<String>(expandableListDetail.keySet());
expandableListAdapter = new CustomContactsExapandableListAdapter(getActivity(), expandableListTitle, expandableListDetail);
expandableListView.setAdapter(expandableListAdapter);
}
private HashMap<String, ArrayList<GroupModel>> getExpandableListItems() {
contacts.add(new GroupModel("gopi", "Officer", "9999999999", "abcd0"));
expandableListDetail.put("TDP", contacts);
expandableListDetail.put("YSRCP", contacts);
expandableListDetail.put("Congress", contacts);
expandableListDetail.put("BJP", contacts);
expandableListDetail.put("ABC", contacts);
expandableListDetail.put("CDE", contacts);
expandableListDetail.put("DEF", contacts);
expandableListDetail.put("FED", contacts);
return expandableListDetail;
}
}
and here is the adapter
public class CustomContactsExapandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private List<String> expandableListTitle;
private HashMap<String, ArrayList<GroupModel>> expandableListDetail;
public CustomContactsExapandableListAdapter(Context context, List<String> expandableListTitle,
HashMap<String, ArrayList<GroupModel>> expandableListDetail) {
this.context = context;
this.expandableListTitle = expandableListTitle;
this.expandableListDetail = expandableListDetail;
}
#Override
public Object getChild(int groupPosition, int childPosition) {
return this.expandableListDetail.get(this.expandableListTitle.get(groupPosition))
.get(childPosition);
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
#Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
Log.i("test", "the value in exapandable is " + getChild(groupPosition, childPosition));
final GroupModel expandedListText = (GroupModel) getChild(groupPosition, childPosition);
Log.i("test", "the text is " + expandedListText.getName()+ " "+ expandedListText.getDesignation() + " " +expandedListText.getMobile() );
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.single_contact, null);
}
TextView expandedListTextViewName = (TextView) convertView
.findViewById(R.id.contact_name);
TextView expandedListTextViewMobile = (TextView) convertView
.findViewById(R.id.contact_mobile_num);
expandedListTextViewMobile.setText(expandedListText.getDesignation());
expandedListTextViewName.setText(expandedListText.getName());
return convertView;
}
#Override
public int getChildrenCount(int groupPosition) {
return this.expandableListDetail.get(this.expandableListTitle.get(groupPosition))
.size();
}
#Override
public Object getGroup(int groupPosition) {
return this.expandableListTitle.get(groupPosition);
}
#Override
public int getGroupCount() {
Log.i("test", "the value in exapandable group is " + expandableListTitle.size());
return expandableListTitle.size();
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
String listTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.single_party, null);
}
TextView listTitleTextView = (TextView) convertView
.findViewById(R.id.party_name);
ImageView imageView=convertView.findViewById(R.id.party_expandable_image);
if(isExpanded){
imageView.setImageResource(R.drawable.minus_icon_contacts);
}
else {
imageView.setImageResource(R.drawable.plus_icon_contacts);
}
listTitleTextView.setTypeface(null, Typeface.BOLD);
listTitleTextView.setText(listTitle);
return convertView;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
I'm creating a navigation drawer with a ExpandableListView, but I can't figure out how to handle empty group clicks.
Everytime I try to click on an empty group, I get a NullPointerException that says "Attempt to invoke interface method 'int java.util.List.size()' on a null object reference" and it points to the getChildrenCount() method.
This is my custom ExpandableListAdapter:
ExpandableListAdapter.java
package co.eshg.drawertest;
import java.util.HashMap;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private List<String> listDataItem; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> listDataChild;
public ExpandableListAdapter(Context context, List<String> listDataItem,
HashMap<String, List<String>> listChildData) {
this.context = context;
this.listDataItem = listDataItem;
this.listDataChild = listChildData;
}
#Override
public Object getChild(int groupPosition, int childPosititon) {
return this.listDataChild.get(this.listDataItem.get(groupPosition))
.get(childPosititon);
}
#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) {
final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.drawer_child_item, null);
}
TextView txtListChild = (TextView) convertView
.findViewById(R.id.tvChildItem);
txtListChild.setText(childText);
return convertView;
}
#Override
public int getChildrenCount(int groupPosition) {
if (this.listDataChild.get(this.listDataItem.get(groupPosition)).size() != 0) {
return this.listDataChild.get(this.listDataItem.get(groupPosition)).size();
}
return 1;
}
#Override
public Object getGroup(int groupPosition) {
return this.listDataItem.get(groupPosition);
}
#Override
public int getGroupCount() {
return this.listDataItem.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 inflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.drawer_list_item, null);
}
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.tvListItem);
lblListHeader.setText(headerTitle);
return convertView;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
Any help would be appreciated.
The list you are calling size() on is null, so you have to check for that first.
Try this:
#Override
public int getChildrenCount(int groupPosition) {
List childList = listDataChild.get(listDataItem.get(groupPosition));
if (childList != null && ! childList.isEmpty()) {
return childList.size();
}
return 1;
}
I'm an Android developer, I just want to expand the list as show in this image and I indicate it by red arrow when I click it, the list should be expand just like Youtube.
Please suggest me what should I do. How can I expand this on arrow click. If there is any suggestion for it please help me.
you can either use expandable listview or expandable recyclerview
refer this http://developer.android.com/reference/android/widget/ExpandableListView.html
https://www.bignerdranch.com/blog/expand-a-recyclerview-in-four-steps/
1.Create an expandable list adapter class extending base expandable list adapter as shown here...
import java.util.HashMap;
import java.util.List;
import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private List<String> expandableListTitle;
private HashMap<String, List<String>> expandableListDetail;
public ExpandableListAdapter(Context context, List<String> expandableListTitle,
HashMap<String, List<String>> expandableListDetail) {
this.context = context;
this.expandableListTitle = expandableListTitle;
this.expandableListDetail = expandableListDetail;
}
#Override
public Object getChild(int listPosition, int expandedListPosition) {
return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
.get(expandedListPosition);
}
#Override
public long getChildId(int listPosition, int expandedListPosition) {
return expandedListPosition;
}
#Override
public View getChildView(int listPosition, final int expandedListPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String expandedListText = (String) getChild(listPosition, expandedListPosition);
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.list_item, null);
}
TextView expandedListTextView = (TextView) convertView
.findViewById(R.id.expandedListItem);
expandedListTextView.setText(expandedListText);
return convertView;
}
#Override
public int getChildrenCount(int listPosition) {
return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
.size();
}
#Override
public Object getGroup(int listPosition) {
return this.expandableListTitle.get(listPosition);
}
#Override
public int getGroupCount() {
return this.expandableListTitle.size();
}
#Override
public long getGroupId(int listPosition) {
return listPosition;
}
#Override
public View getGroupView(int listPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String listTitle = (String) getGroup(listPosition);
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.list_group, null);
}
TextView listTitleTextView = (TextView) convertView
.findViewById(R.id.listTitle);
listTitleTextView.setTypeface(null, Typeface.BOLD);
listTitleTextView.setText(listTitle);
return convertView;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public boolean isChildSelectable(int listPosition, int expandedListPosition) {
return true;
}
}
Create your list view. Define a expandable list view in layout xml
expandableListView = (ExpandableListView) findViewById(R.id.expandableListView);
expandableListDetail = <ArrayList>; // Provide your array list here
expandableListTitle = new ArrayList(<Title of your list goes here>);
expandableListAdapter = new ExpandableListAdapter(this, expandableListTitle, expandableListDetail);
expandableListView.setAdapter(expandableListAdapter);
expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
#Override
public void onGroupExpand(int groupPosition) {
Toast.makeText(getApplicationContext(),
expandableListTitle.get(groupPosition) + " List Expanded.",
Toast.LENGTH_SHORT).show();
}
});
expandableListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
#Override
public void onGroupCollapse(int groupPosition) {
Toast.makeText(getApplicationContext(),
expandableListTitle.get(groupPosition) + " List Collapsed.",
Toast.LENGTH_SHORT).show();
}
});
expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
Toast.makeText(
getApplicationContext(),
expandableListTitle.get(groupPosition)
+ " -> "
+ expandableListDetail.get(
expandableListTitle.get(groupPosition)).get(
childPosition), Toast.LENGTH_SHORT
)
.show();
}
});
This is my ListAdapter coding and my error is on the context on the png image file. if i am not wrong my context i have to change something? but i don't know what to change
http://i.stack.imgur.com/MhEWr.png
package com.example.user.myapplication;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
import java.util.HashMap;
import java.util.List;
public class MyExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private List<String> listGroup;
private HashMap<String, List<String>> listChild;
public MyExpandableListAdapter(Context context, List<String> listGroup,
HashMap<String, List<String>> listChild) {
this.context = context;
this.listGroup = listGroup;
this.listChild = listChild;
}
#Override
public int getGroupCount() {
return listGroup.size();
}
#Override
public int getChildrenCount(int groupPosition) {
return listChild.get(listGroup.get(groupPosition)).size();
}
#Override
public Object getGroup(int groupPosition) {
return listGroup.get(groupPosition);
}
#Override
public Object getChild(int groupPosition, int childPosition) {
return listChild.get(listGroup.get(groupPosition)).get(childPosition);
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.group_layout, null);
}
String textGroup = (String) getGroup(groupPosition);
TextView textViewGroup = (TextView) convertView
.findViewById(R.id.group);
textViewGroup.setText(textGroup);
return convertView;
}
#Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.item_layout, null);
}
TextView textViewItem = (TextView) convertView.findViewById(R.id.item);
String text = (String) getChild(groupPosition, childPosition);
textViewItem.setText(text);
return convertView;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return false;
}
}
The error occurs because you start it from a fragement. Use
Hotel.this.getActivity().getApplicationContext()
In class Hotel like this:
myExpandableListAdapter = new MyExpandableListAdapter(Hotel.this.getActivity().getApplicationContext(), groupList, childMap);
I have created an expandable list view,On clicking the child element another activity is started.But this happens only for the first three child elements in first parent group, when i click on the subsequent child elements the app crashes an the error
java.lang.IndexOutOfBounds:Invalid index 3,size is 3.
I don't know where I have gone wrong.
This is MainActivity.java file
package com.example.expandablelistview;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
public class MainActivity extends Activity {
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// get the listview
expListView = (ExpandableListView) findViewById(R.id.lvExp);
// preparing list data
prepareListData();
listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
// setting list adapter
expListView.setAdapter(listAdapter);
expListView.setOnChildClickListener(new OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View view,
int groupPosition, int childPosition, long id) {
// selected item
if(listDataHeader.get(groupPosition)=="Catalog"){
Intent i = new Intent(MainActivity.this, SingleListItem.class);
// sending data to new activity
startActivity(i);
}
if(listDataHeader.get(groupPosition)=="My Account"){
Intent i1 = new Intent(MainActivity.this,CheckHolds.class);
startActivity(i1);
}
if(listDataHeader.get(groupPosition)=="Library Info"){
Intent i2 = new Intent(MainActivity.this,LibraryHours.class);
startActivity(i2);
}
return false;
}
});
}
/*
* Preparing the list data
*/
private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataHeader.add("Catalog");
listDataHeader.add("My Account");
listDataHeader.add("Library Info");
// Adding child data
List<String> catalog = new ArrayList<String>();
catalog.add("Automobile");
catalog.add("Civil");
catalog.add("Electronics and Communication");
catalog.add("Electrical and Electronics");
catalog.add("Information Science");
catalog.add("Industrial Production");
catalog.add("Mechanical");
catalog.add("Basic Sciences");
List<String> myaccount = new ArrayList<String>();
myaccount.add("Check Holds");
myaccount.add("Unreserve Books");
List<String> libraryinfo = new ArrayList<String>();
libraryinfo.add("Library Hours");
libraryinfo.add("Contact Library");
listDataChild.put(listDataHeader.get(0), catalog); // Header, Child data
listDataChild.put(listDataHeader.get(1), myaccount);
listDataChild.put(listDataHeader.get(2), libraryinfo);
}
}
And this is the ExpandableListAdapter.java file
package com.example.expandablelistview;
import java.util.HashMap;
import java.util.List;
import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;
public ExpandableListAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<String>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}
#Override
public Object getChild(int groupPosition, int childPosititon) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosititon);
}
#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) {
final String childText = (String) 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(childText);
return convertView;
}
#Override
public int getChildrenCount(int groupPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.size();
}
#Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}
#Override
public int getGroupCount() {
return this._listDataHeader.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.list_group, null);
}
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.lblListHeader);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
return convertView;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
Please suggest me what changes should I do to solve the error.
Lets consider this if condition:
if(listDataHeader.get(groupPosition)=="My Account"){
Here you should first check whether the groupPosition is inside the size of the list listDataHeader to avoid this Exception. For example groupPosition < listDataHeader.size()
Also here you are doing the string comparison in a wrong way. Java doesn't support such comparision. You should use either .equals("My Account") or .equalsIgnoreCase("My Account") for this instead of ==
So finally it will be:
if(groupPosition < listDataHeader.size() &&
listDataHeader.get(groupPosition).equalsIgnoreCase("My Account")){
Try to access the Group elements as below:
listDataHeader.get(groupPosition).equalsIgnoreCase("My Account")
Also to access the child elements you can get it as below:
listDataChild.get(listDataHeader.get(groupPosition)).get(childPosition).equalsIgnoreCase("Automobile");
Change your child Click listener as below:
expListView.setOnChildClickListener(new OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View view,
int groupPosition, int childPosition, long id) {
// selected item
if(listDataHeader.get(groupPosition).equalsIgnoreCase("Catalog")){
Intent i = new Intent(MainActivity.this, SingleListItem.class);
// sending data to new activity
startActivity(i);
}
if(listDataHeader.get(groupPosition).equalsIgnoreCase("My Account"){
Intent i1 = new Intent(MainActivity.this,CheckHolds.class);
startActivity(i1);
}
if(listDataHeader.get(groupPosition).equalsIgnoreCase("Library Info"){
Intent i2 = new Intent(MainActivity.this,LibraryHours.class);
startActivity(i2);
}
return false;
}