How to resolve java.lang.IndexOutOfBoundsException in android? - java

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;
}

Related

Different Switch Intents In ExpandableListView Parents - Previous One Doesn't Work

I'm trying to create an ExpandibleListView with intents that switch to new classes. When using only one switch, there is no problem, but when I want different parents to switch diffent classes, only one of them works, the other one stays like empty (Its arrows turn up and down but nothing happens). How can I make them work together?
Here is my codes:
ExpandibleListAdapter.java
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import java.util.HashMap;
import java.util.List;
import android.content.Context;
import android.widget.TextView;
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private List<String> listDataHeader;
private HashMap<String,List<String>> listHashMap;
public ExpandableListAdapter(Context context, List<String> listDataHeader, HashMap<String, List<String>> listHashMap) {
this.context = context;
this.listDataHeader = listDataHeader;
this.listHashMap = listHashMap;
}
#Override
public int getGroupCount() {
return listDataHeader.size();
}
#Override
public int getChildrenCount(int i) {
return listHashMap.get(listDataHeader.get(i)).size();
}
#Override
public Object getGroup(int i) {
return listDataHeader.get(i);
}
#Override
public Object getChild(int i, int i1) {
return listHashMap.get(listDataHeader.get(i)).get(i1); // i = group item , i1= ChildItem
}
#Override
public long getGroupId(int i) {
return i;
}
#Override
public long getChildId(int i, int i1) {
return i1;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public View getGroupView(int i, boolean b, View view, ViewGroup ViewGroup) {
String headerTitle = (String)getGroup(i);
if(view == null)
{
LayoutInflater inflater = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.list_group,null);
}
TextView lblListHeader = (TextView)view.findViewById(R.id.lblListHeader);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
return view;
}
#Override
public View getChildView(int i, int i1, boolean b, View view, ViewGroup ViewGroup) {
final String childText = (String)getChild(i,i1);
if(view == null)
{
LayoutInflater inflater = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.list_item,null);
}
TextView txtListChild = (TextView)view.findViewById(R.id.lblListItem);
txtListChild.setText(childText);
return view;
}
#Override
public boolean isChildSelectable(int i, int i1) {
return true;
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity
{
private ExpandableListView listView;
private ExpandableListAdapter listAdapter;
private List<String> listDataHeader;
private HashMap<String,List<String>> listHash;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ExpandableListView)findViewById(R.id.lvExp);
initData();
listAdapter = new ExpandableListAdapter(this,listDataHeader,listHash);
listView.setAdapter(listAdapter);
}
private void initData() {
listDataHeader = new ArrayList<>();
listHash = new HashMap<>();
listDataHeader.add("Line One");
listDataHeader.add("Line Two");
List<String> genel = new ArrayList<>();
**//The problem starts here**
listView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
#Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
final String selected = (String) listAdapter.getGroup(groupPosition);
Intent intent;
switch (selected) {
case "Line One":
intent = new Intent(MainActivity.this,LineOne.class);
startActivity(intent);
break;
}
return false; //return true doesn't let the other parents to open.
}
});
List<String> terims = new ArrayList<>();
listView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
#Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
final String terims = (String) listAdapter.getGroup(groupPosition);
Intent niyet;//I used "intent" instead of "niyet" as same above but nothing has changed.
switch (terims) {
case "Line Two":
niyet = new Intent(MainActivity.this, LineTwo.class);
startActivity(niyet);
break;
}
return false;
}
});
listHash.put(listDataHeader.get(0),genel);
listHash.put(listDataHeader.get(1),terims);
}
}
You cannot set 2 OnGroupClickListeners. The second one cancels the first one. You should set one listener and then check for both sets of conditions in the one listener.

How to get a specific object onChildClick in an expandablelistview?

I have the following problem in my android app(Java):
Each child in the expandablelistview represents an object called Entry,but just the title(child) and a date(header) are stored in the expandablelistview.
When the child is clicked another activity should start, where I can edit the values of the Entry. I need the specific Entry in addition to the new activity if I want to accomplish this feat.
The grand scheme of this is, that each object is an "to-do-task". Each task has a date, title, priority, etc. The tasks are displayed in the expandablelistview and if you click on a date each task-title with that specific date should show up and if you click on the title another screen should comes up where you can edit the task.
Is there anyway this is possible?
Any help would be appreciated.
Edit:
Here is my expandableListAdapter:
ExpandableListAdapter.java
package info.androidhive.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;
}
}
Here is my mainactivity:
package hartmann.todo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import com.google.gson.Gson;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
public class MainActivity extends Activity {
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
ListOfEntries listOfEntries;
Gson gson;
int id;
DateFormat dateFormat;
Calendar cal;
Entry entry;
List<String> day0, day1, day2, day3, day4, day5, day6, rest;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listOfEntries = new ListOfEntries();
cal = Calendar.getInstance();
dateFormat = new SimpleDateFormat("dd.MM.yyyy");
listDataHeader = new ArrayList<>();
listDataChild = new HashMap<>();
id = 0;
entry = new Entry();
gson = new Gson();
day0 = new ArrayList<>();
day1 = new ArrayList<>();
day2 = new ArrayList<>();
day3 = new ArrayList<>();
day4 = new ArrayList<>();
day5 = new ArrayList<>();
day6 = new ArrayList<>();
rest = new ArrayList<>();
// get the listview
expListView = (ExpandableListView) findViewById(R.id.lvExp);
// preparing list data
listDataHeader.add(dateFormat.format(cal.getTime()));
for (int m = 0; m < 6; m++) {
cal.add(Calendar.DATE, 1);
listDataHeader.add(dateFormat.format(cal.getTime()));
}
listDataHeader.add("Später");
listDataChild.put(listDataHeader.get(0), day0);
listDataChild.put(listDataHeader.get(1), day1);
listDataChild.put(listDataHeader.get(2), day2);
listDataChild.put(listDataHeader.get(3), day3);
listDataChild.put(listDataHeader.get(4), day4);
listDataChild.put(listDataHeader.get(5), day5);
listDataChild.put(listDataHeader.get(6), day6);
listDataChild.put(listDataHeader.get(7), rest);
listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
// setting list adapter
expListView.setAdapter(listAdapter);
}
// Preparing the list data
private void prepareListData(Entry entry) {
Calendar cal2 = Calendar.getInstance();
cal2.clear(Calendar.HOUR);
cal2.clear(Calendar.MINUTE);
cal2.clear(Calendar.SECOND);
cal2.clear(Calendar.MILLISECOND);
if (entry.getDate().equals(cal2)) {
day0.add(entry.getTitle());
}
cal2.add(Calendar.DATE, 1);
if (entry.getDate().equals(cal2)) {
day1.add(entry.getTitle());
}
cal2.add(Calendar.DATE, 1);
if (entry.getDate().equals(cal2)) {
day2.add(entry.getTitle());
}
cal2.add(Calendar.DATE, 1);
if (entry.getDate().equals(cal2)) {
day3.add(entry.getTitle());
}
cal2.add(Calendar.DATE, 1);
if (entry.getDate().equals(cal2)) {
day4.add(entry.getTitle());
}
cal2.add(Calendar.DATE, 1);
if (entry.getDate().equals(cal2)) {
day5.add(entry.getTitle());
}
cal2.add(Calendar.DATE, 1);
if (entry.getDate().equals(cal2)) {
day6.add(entry.getTitle());
}
if (entry.getDate().after(cal2)) {
rest.add(entry.getTitle());
}
listDataChild.put(listDataHeader.get(0), day0);
listDataChild.put(listDataHeader.get(1), day1);
listDataChild.put(listDataHeader.get(2), day2);
listDataChild.put(listDataHeader.get(3), day3);
listDataChild.put(listDataHeader.get(4), day4);
listDataChild.put(listDataHeader.get(5), day5);
listDataChild.put(listDataHeader.get(6), day6);
listDataChild.put(listDataHeader.get(7), rest);
listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
// setting list adapter
expListView.setAdapter(listAdapter);
}
public void newTask(View view) {
Intent intent = new Intent(MainActivity.this, EditEntry.class);
startActivityForResult(intent, 1);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if (resultCode == Activity.RESULT_OK) {
String strObj = data.getStringExtra("entry");
entry = gson.fromJson(strObj, Entry.class);
entry.setEntryId(id);
id++;
listOfEntries.addEntry(entry);
prepareListData(entry);
}
if (resultCode == Activity.RESULT_CANCELED) {
//Write your code if there's no result
}
}
}//onActivityResult
}

Handling empty group clicks with ExpandableListView

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;
}

How to make expandable list on arrow click just like Youtube in android

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();
}
});

Error on expandableview Nothing appearing

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);

Categories

Resources