I am trying to show data in an expandable listview from server in android. I am trying to implement hashmap within the AsyncTask. Please see the following code.
private class HttpAsyncTaskReports extends AsyncTask<String, Void, String> {
private ProgressDialog dialog;
public HttpAsyncTaskReports(ExpandableActivity activity) {
dialog = new ProgressDialog(activity);
}
#Override
protected String doInBackground(String... urls) {
return GET(urls[0]);
}
#Override
protected void onPreExecute() {
// Removes all markers, overlays, and polylines from the map.
dialog.setMessage("Loading reports, please wait.");
dialog.show();
}
#Override
protected void onPostExecute(String result) {
if (dialog.isShowing()) {
dialog.dismiss();
}
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
// Header, Child data
List<String> top250 = new ArrayList<String>();
top250.add("The Shawshank Redemption");
top250.add("The Godfather");
try {
JSONArray jArray = new JSONArray(result);
JSONObject jsonobject = jArray.getJSONObject(0);
JSONArray jsonAssets = jsonobject.getJSONArray("last_data");
Log.d("LASTLATLONG", result);
for (i = 0; i < jsonAssets.length(); i++) {
JSONObject nlmJsonObject = jsonAssets.getJSONObject(i);
JSONObject lastData_JsonObject = nlmJsonObject.getJSONObject("last_data");
JSONObject lastData_timeJsonObject = lastData_JsonObject.getJSONObject("time");
// JSONObject lastData_dateJsonObject = lastData_timeJsonObject.getJSONObject("$date");
// JSONObject lastData_speedJsonObject = lastData_JsonObject.getJSONObject("speed");
// Adding child data
listDataHeader.add(lastData_timeJsonObject.getString("$date"));
listDataChild.put(listDataHeader.get(i), top250);
}
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Server Error :( Please wait!", Toast.LENGTH_LONG).show();
}
}
}
Here is the ExpandableListAdapter class
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 the error....
java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3124)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3233)
at android.app.ActivityThread.access$1000(ActivityThread.java:197)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1656)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6856)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
at gps.finder.com.findermaterial.ExpandableListAdapter.getGroupCount(ExpandableListAdapter.java:75)
at android.widget.ExpandableListConnector.getCount(ExpandableListConnector.java:397)
at android.widget.ListView.setAdapter(ListView.java:502)
at android.widget.ExpandableListView.setAdapter(ExpandableListView.java:604)
at gps.finder.com.findermaterial.ExpandableActivity.onCreate(ExpandableActivity.java:102)
at android.app.Activity.performCreate(Activity.java:6550)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1120)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3077)
Can anybody tell me how should I solve this issue?
If I use a function like the following one to list data, it works successfully.
private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
// Adding child data
listDataHeader.add("Top 50");
listDataHeader.add("Now Showing");
listDataHeader.add("Coming Soon..");
// Adding child data
List<String> top250 = new ArrayList<String>();
top250.add("The Shawshank Redemption");
top250.add("The Godfather");
top250.add("The Godfather: Part II");
top250.add("Pulp Fiction");
top250.add("The Good, the Bad and the Ugly");
top250.add("The Dark Knight");
top250.add("12 Angry Men");
List<String> nowShowing = new ArrayList<String>();
nowShowing.add("The Conjuring");
nowShowing.add("Despicable Me 2");
nowShowing.add("Turbo");
nowShowing.add("Grown Ups 2");
nowShowing.add("Red 2");
nowShowing.add("The Wolverine");
List<String> comingSoon = new ArrayList<String>();
comingSoon.add("2 Guns");
comingSoon.add("The Smurfs 2");
comingSoon.add("The Spectacular Now");
comingSoon.add("The Canyons");
comingSoon.add("Europa Report");
listDataChild.put(listDataHeader.get(0), top250); // Header, Child data
listDataChild.put(listDataHeader.get(1), nowShowing);
listDataChild.put(listDataHeader.get(2), comingSoon);
}
But I need to implement the hashmap using the data retrieve from server.
I appreciate your suggestions. Thanks in advance.
Related
I have tabs with three different fragments. Attendance, Exam and Result. On some phones when the result fragment starts the application crashes saying.
java.lang.NullPointerException: Attempt to invoke virtual method
'java.lang.Object
android.content.Context.getSystemService(java.lang.String)' on a null
object reference
here is the logcat
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
at android.view.LayoutInflater.from(LayoutInflater.java:220)
at com.bu.innovate.bustudentportal.Result_Class$ExampleAdapter.<init>(Result_Class.java:886)
at com.bu.innovate.bustudentportal.Result_Class$results.onPostExecute(Result_Class.java:544)
at com.bu.innovate.bustudentportal.Result_Class$results.onPostExecute(Result_Class.java:245)
at android.os.AsyncTask.finish(AsyncTask.java:632)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5942)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
and the code its pointing to is this
public class Result_Class extends Fragment implements java.io.Serializable {
private AnimatedExpandableListView listView;
private ExampleAdapter adapter;
String url;
String url2;
String url3;
static String urladder = "Transcript.aspx";
static String urladder2 = "Result.aspx";
static String urladder3 = "Result_Exam.aspx";
HashMap<String, String> hashMaps;
List<String> semester = new ArrayList<>();
List<Result_Gdata> GPA = new ArrayList<>();
List<Result_data> data_for_gra = new ArrayList<>();
List<Result_data> loop_finder = new ArrayList<>();
List<String> semester2 = new ArrayList<>();
List<Result_Gdata> GPA2 = new ArrayList<>();
List<Result_data> data_for_gra2 = new ArrayList<>();
List<Result_data> loop_finder2 = new ArrayList<>();
View rootview;
int num = 0;
HashMap<String, List<Result_data>> listDataChild;
FileOutputStream outputStream;
FileOutputStream outputStream2;
FileInputStream inputStream;
FileInputStream inputStream2;
String filename = "newResultData";
String filename2 = "oldResultData";
List<Result_data> data_for_gra3 = null;
List<String> semesters3 = null;
List<Result_data> loop_finder3 = null;
List<Result_Gdata> GPA3 = null;
List<Result_data> data_for_gra4 = null;
List<String> semesters4 = null;
List<Result_data> loop_finder4 = null;
List<Result_Gdata> GPA4 = null;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
super.onCreate(savedInstanceState);
rootview = inflater.inflate(R.layout.anim_result_layout, container, false);
listView = (AnimatedExpandableListView) rootview.findViewById(R.id.listView);
hashMaps = Data.map;
listView.setDivider(null);
listView.setDividerHeight(0);
if (Data.mychoice == 1) {
num = 0;
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
new result_getter_second().execute();
}
}, 1800);
} else {
Handler handlers2 = new Handler();
handlers2.postDelayed(new Runnable() {
#Override
public void run() {
new results().execute();
}
}, 2000);
}
return rootview;
}
private class result_getter_second extends AsyncTask<Void, Void, Void> {
#Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
num = 0;
listDataChild = new HashMap<>();
List<GroupItem> items = new ArrayList<>();
if (semester.size() != 0) {
for (int m = 0; m < semester.size(); m++) {
GroupItem item = new GroupItem();
item.title = semester.get(m);
if (m < GPA.size()) {
item.gpa = GPA.get(m).getGPA();
item.cgpa = GPA.get(m).getCGPA();
}
List<Result_data> datw = new ArrayList<>();
for (int n = num; n < loop_finder.get(m).getSize_of_the_result(); n++) {
ChildItem child = new ChildItem();
child.course = data_for_gra.get(n).getSubject();
child.grade = data_for_gra.get(n).getGrade();
item.items.add(child);
datw.add(new Result_data(data_for_gra.get(n).getSubject(), data_for_gra.get(n).getGrade()));
num++;
}
items.add(item);
if(getActivity()!=null)
{
adapter = new ExampleAdapter(getActivity()); // line number 544
adapter.setData(items);
listView.setAdapter(adapter);
}
listView.setOnGroupClickListener(new OnGroupClickListener() {
#Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
if (listView.isGroupExpanded(groupPosition)) {
listView.collapseGroupWithAnimation(groupPosition);
} else {
listView.expandGroupWithAnimation(groupPosition);
}
return true;
}
});
}
}
}
}
private static class GroupItem {
String title;
String gpa;
String cgpa;
List<ChildItem> items = new ArrayList<ChildItem>();
}
private static class ChildItem {
String course;
String grade;
}
private static class ChildHolder {
TextView course;
TextView grade;
}
private static class GroupHolder {
TextView title;
TextView gpa;
TextView cgpa;
}
/**
* Adapter for our list of {#link GroupItem}s.
*/
private class ExampleAdapter extends AnimatedExpandableListView.AnimatedExpandableListAdapter {
private LayoutInflater inflater;
private List<GroupItem> items;
public ExampleAdapter(Context context) {
if(context!=null)
{
inflater = LayoutInflater.from(context); // line number 886
}
}
public void setData(List<GroupItem> items) {
this.items = items;
}
#Override
public ChildItem getChild(int groupPosition, int childPosition) {
return items.get(groupPosition).items.get(childPosition);
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
#Override
public View getRealChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
ChildHolder holder;
ChildItem item = getChild(groupPosition, childPosition);
if (childPosition == 0) {
holder = new ChildHolder();
convertView = inflater.inflate(R.layout.anim_list_item_result, parent, false);
holder.course = (TextView) convertView.findViewById(R.id.textcourse);
holder.grade = (TextView) convertView.findViewById(R.id.textgrade);
convertView.setTag(holder);
holder.course.setText(item.course);
holder.grade.setText(item.grade);
} else if (isLastChild) {
holder = new ChildHolder();
convertView = inflater.inflate(R.layout.anim_list_item_result_last_child, parent, false);
holder.course = (TextView) convertView.findViewById(R.id.textcourse);
holder.grade = (TextView) convertView.findViewById(R.id.textgrade);
convertView.setTag(holder);
holder.course.setText(item.course);
holder.grade.setText(item.grade);
} else {
holder = new ChildHolder();
convertView = inflater.inflate(R.layout.anim_list_item_result_middle, parent, false);
holder.course = (TextView) convertView.findViewById(R.id.textcourse);
holder.grade = (TextView) convertView.findViewById(R.id.textgrade);
convertView.setTag(holder);
holder.course.setText(item.course);
holder.grade.setText(item.grade);
}
return convertView;
}
#Override
public int getRealChildrenCount(int groupPosition) {
return items.get(groupPosition).items.size();
}
#Override
public GroupItem getGroup(int groupPosition) {
return items.get(groupPosition);
}
#Override
public int getGroupCount() {
return items.size();
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
GroupHolder holder;
GroupItem item = getGroup(groupPosition);
if (groupPosition == getGroupCount() - 1) {
holder = new GroupHolder();
convertView = inflater.inflate(R.layout.anim_group_item_result_last_child, parent, false);
holder.title = (TextView) convertView.findViewById(R.id.textTitle);
holder.gpa = (TextView) convertView.findViewById(R.id.textgpa);
holder.cgpa = (TextView) convertView.findViewById(R.id.textcgpa);
convertView.setTag(holder);
holder.title.setText(item.title);
holder.gpa.setText(item.gpa);
holder.cgpa.setText(item.cgpa);
} else {
holder = new GroupHolder();
convertView = inflater.inflate(R.layout.anim_group_item_result, parent, false);
holder.title = (TextView) convertView.findViewById(R.id.textTitle);
holder.gpa = (TextView) convertView.findViewById(R.id.textgpa);
holder.cgpa = (TextView) convertView.findViewById(R.id.textcgpa);
convertView.setTag(holder);
holder.title.setText(item.title);
holder.gpa.setText(item.gpa);
holder.cgpa.setText(item.cgpa);
}
if (groupPosition == 0) {
ExpandableListView elv = (ExpandableListView) parent;
elv.expandGroup(groupPosition);
}
return convertView;
}
#Override
public boolean hasStableIds() {
return true;
}
#Override
public boolean isChildSelectable(int arg0, int arg1) {
return true;
}
}
private boolean isOnline() {
ConnectivityManager cm = (ConnectivityManager) getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
//should check null because in air plan mode it will be null
return (netInfo != null && netInfo.isConnected());
}
}
The problem is in the first line where the getcontext() is called I have tried so much to solve this problem but couldn't. It would be really helpful If you guys help me solve this problem.
Does you make it onCreate in fragment?
Add checking for getContext()!=null
Before block
adapter = new ExampleAdapter(getContext());
adapter.setData(items);
listView.setAdapter(adapter);
And better replace for getActivity()!=null
For example:
if (getActivity()!=null){
adapter = new ExampleAdapter(getActivity());
adapter.setData(items);
listView.setAdapter(adapter);
}
I'm struggling with multilevel Expandable ListViews. I manage to make a 3 level Expandable ListView it is to say :
-- GrandParent
--- Parent
----- Child
But whenever I try to add a 4th sub child, the view seems to appear behind the last level. Here are the elements I use to achieve this :
CustomExpandableListView.java
public class CustomExpandableListView extends ExpandableListView {
public CustomExpandableListView(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
}
public CustomExpandableListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomExpandableListView(Context context) {
super(context);
}
#Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
heightMeasureSpec = MeasureSpec.makeMeasureSpec(2000, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
#Override
protected void onDetachedFromWindow() {
try {
super.onDetachedFromWindow();
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
}
}
ExpandableListAdapter.java
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private List<String> listDataHeader; // header titles
private HashMap<String, List<ExpandableItem>> listDataChild;
public ExpandableListAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<ExpandableItem>> 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) {
if (listDataChild.get(listDataHeader.get(groupPosition)).get(childPosition).getNumberOfChildren() > 0) {
DPPCustomExpandableListView subList = new DPPCustomExpandableListView(context);
List<ExpandableItem> list = listDataChild.get(listDataHeader.get(groupPosition)).get(childPosition).getItems();
ArrayList<String> headers = new ArrayList<String>();
HashMap<String, List<ExpandableItem>> newMap = new HashMap<>();
String name = "Expandable";
headers.add(name);
newMap.put(name, list);
MultiExpandableListAdapter multiAdapter = new MultiExpandableListAdapter(context, headers, newMap);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
subList.setLayoutParams(params);
subList.setAdapter(multiAdapter);
MySalesFragment currentFragment = (MySalesFragment) ((MainActivity) context).getFragmentManager().findFragmentById(R.id.container);
if (currentFragment.getCurrentPagerFragment().getClass().equals(SalesDeclarationFragment.class)) {
SalesDeclarationFragment salesDeclarationFragment = (SalesDeclarationFragment) currentFragment.getCurrentPagerFragment();
subList.setOnChildClickListener(salesDeclarationFragment);
}
return subList;
}
LayoutInflater infalInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.sales_declaration_expandable_listview_item, null);
TextView txtListChild = (TextView) convertView
.findViewById(R.id.child_textview);
//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) {
List<ExpandableItem> items = listDataChild.get(listDataHeader.get(groupPosition));
return items;
}
#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.sales_declaration_expandable_list_view_group_header, null);
}
return convertView;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
MultiExpandableListAdapter.java
public class MultiExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private List<String> listDataHeader; // header titles
private HashMap<String, List<ExpandableItem>> listDataChild;
public static int count = 0;
public MultiExpandableListAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<ExpandableItem>> 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) {
if(listDataChild.get(listDataHeader.get(groupPosition)).get(childPosition).getNumberOfChildren() > 0) {
subList = new DPPNLevelCustomEpandableListView(context);
List<ExpandableItem> list = listDataChild.get(listDataHeader.get(groupPosition)).get(childPosition).getItems();
ArrayList<String> headers = new ArrayList<String>();
HashMap<String, List<ExpandableItem>> newMap = new HashMap<>();
headers.add(name);
newMap.put(name, list);
MultiExpandableListAdapter multiAdapter = new MultiExpandableListAdapter(context, headers, newMap);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
subList.setLayoutParams(params);
subList.setAdapter(multiAdapter);
subList.setBackgroundColor(context.getResources().getColor(R.color.loyalty_store_theme_yellow));
MySalesFragment currentFragment = (MySalesFragment)((MainActivity)context).getFragmentManager().findFragmentById(R.id.container);
if (currentFragment.getCurrentPagerFragment().getClass().equals(SalesDeclarationFragment.class)) {
SalesDeclarationFragment salesDeclarationFragment = (SalesDeclarationFragment)currentFragment.getCurrentPagerFragment();
subList.setOnChildClickListener(salesDeclarationFragment);
}
return subList;
}
LayoutInflater infalInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.sales_declaration_expandable_listview_item, null);
convertView.setBackgroundColor(context.getResources().getColor(R.color.cpb_blue));
return convertView;
}
#Override
public int getChildrenCount(int groupPosition) {
int count = this.listDataChild.get(listDataHeader.get(groupPosition)).size();
return count;
}
#Override
public Object getGroup(int groupPosition) {
List<ExpandableItem> items = listDataChild.get(listDataHeader.get(groupPosition));
return items;
}
#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) {
LayoutInflater infalInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.sales_declaration_expandable_listview_item, null);
convertView.setBackgroundColor(context.getResources().getColor(R.color.cpb_green));
return convertView;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
ExpandableItem.java
public interface ExpandableItem {
int getNumberOfChildren();
ArrayList<ExpandableItem> getItems();
void addExpandableItem(ExpandableItem item);
String getName();
}
ListTextItem.java
public class ListTextItem implements ExpandableItem{
private ArrayList<ExpandableItem> items;
private String name;
public ListTextItem(String name){
this.name = name;
items = new ArrayList<ExpandableItem>();
}
#Override
public int getNumberOfChildren() {
return items.size();
}
public ArrayList<ExpandableItem> getItems() {
return items;
}
public void addExpandableItem(ExpandableItem itemToAdd) {
items.add(itemToAdd);
}
public String getName() {
return name;
}
}
ExampleFragment.java
public class ExampleFragment extends Fragment implements ExpandableListView.OnChildClickListener {
private View root;
ExpandableListAdapter listAdapter;
#InjectView(R.id.sales_declaration_list)
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<ExpandableItem>> listDataChild;
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
this.root = inflater.inflate(R.layout.fragment_sales_declaration, container, false);
ButterKnife.inject(this, root);
return root;
}
#Override
public void onStart() {
super.onStart();
// preparing list data
prepareListData();
listAdapter = new ExpandableListAdapter(getActivity(), listDataHeader, listDataChild);
// setting list adapter
expListView.setAdapter(listAdapter);
expListView.setGroupIndicator(null);
expListView.setDividerHeight(0);
expListView.setOnChildClickListener(this);
}
#Override
public void onDetach() {
super.onDetach();
}
private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<ExpandableItem>>();
// Adding child data
listDataHeader.add("Top 250");
listDataHeader.add("Now Showing");
listDataHeader.add("Coming Soon..");
// Adding child data
List<ExpandableItem> top250 = new ArrayList<ExpandableItem>();
top250.add(new ListTextItem("The Shawshank Redemption"));
top250.add(new ListTextItem("The Godfather"));
top250.add(new ListTextItem("The Godfather: Part II"));
top250.add(new ListTextItem("Pulp Fiction"));
top250.add(new ListTextItem("The Good, the Bad and the Ugly"));
top250.add(new ListTextItem("The Dark Knight"));
top250.add(new ListTextItem("12 Angry Men"));
List<ExpandableItem> nowShowing = new ArrayList<ExpandableItem>();
nowShowing.add(new ListTextItem("The Conjuring"));
nowShowing.add(new ListTextItem("Despicable Me 2"));
nowShowing.add(new ListTextItem("Turbo"));
nowShowing.add(new ListTextItem("Grown Ups 2"));
nowShowing.add(new ListTextItem("Red 2"));
nowShowing.add(new ListTextItem("The Wolverine"));
nowShowing.add(new ListTextItem("The Wolverine2"));
nowShowing.add(new ListTextItem("The Wolverine3"));
nowShowing.add(new ListTextItem("The Wolverine4"));
List<ExpandableItem> comingSoon = new ArrayList<ExpandableItem>();
comingSoon.add(new ListTextItem("2 Guns"));
comingSoon.add(new ListTextItem("The Smurfs 2"));
comingSoon.add(new ListTextItem("The Spectacular Now"));
comingSoon.add(new ListTextItem("The Canyons"));
comingSoon.add(new ListTextItem("Europa Report"));
for(ExpandableItem item : comingSoon) {
top250.get(1).addExpandableItem(item);
}
for(ExpandableItem item : nowShowing) {
top250.get(1).getItems().get(0).addExpandableItem(item);
}
for(ExpandableItem item : comingSoon) {
top250.get(3).addExpandableItem(item);
}
listDataChild.put(listDataHeader.get(0), top250); // Header, Child data
listDataChild.put(listDataHeader.get(1), nowShowing);
listDataChild.put(listDataHeader.get(2), comingSoon);
}
#Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
if(!(((ExpandableItem)parent.getExpandableListAdapter().getChild(groupPosition, childPosition)).getNumberOfChildren() > 0)){
//Toast.makeText(getActivity(), listAdapter.getChild(groupPosition, childPosition).toString(), Toast.LENGTH_SHORT).show();
LSFragmentManager fragmentManager = new LSFragmentManager(getActivity());
fragmentManager.showFragment(Fragment2.class.getName());
}
return false;
}
}
Sorry For the long post, hope someone can help me!
Hi I an using an expandable listview in android. For populating the list I am using an hashmap as i want to map the corresponding categories to its child. My hashmap is like Hashmap> .Now the following code is used :
public class ExpandableListAdapter extends BaseExpandableListAdapter
{
private Context _context;
// child data in format of header title, child title
private Map<SaleDetailsMenuItems, List<ModifList>> _listDataChild;
public ExpandableListAdapter(Context context, Map<SaleDetailsMenuItems, List<ModifList>> listChildData)
{
this._context = context;
this._listDataChild = listChildData;
}
#Override
public Object getChild(int groupPosition, int childPosititon)
{
Set<SaleDetailsMenuItems> s = _listDataChild.keySet();
SaleDetailsMenuItems key = (SaleDetailsMenuItems) s.toArray()[groupPosition];
//List<SaleDetailsMenuItems> list = new ArrayList<SaleDetailsMenuItems>(s);
//Log.e("Child in adapter get Child","get child "+this._listDataChild.get(key).get(childPosititon));
return this._listDataChild.get(key).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)
{
String childText = ((ModifList)getChild(groupPosition, childPosition)).getName();
String childText1 = "1";
String childText2 = ((ModifList)getChild(groupPosition, childPosition)).getPrice();
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);
TextView txtListChild1 = (TextView) convertView.findViewById(R.id.lblListItem1);
TextView txtListChild2 = (TextView) convertView.findViewById(R.id.lblListItem2);
txtListChild.setText(childText);
txtListChild1.setText(childText1);
txtListChild2.setText(childText2);
return convertView;
}
#Override
public int getChildrenCount(int groupPosition)
{
SaleDetailsMenuItems key ;
try
{
Set<SaleDetailsMenuItems> s = _listDataChild.keySet();
key = (SaleDetailsMenuItems) s.toArray()[groupPosition];
// List<SaleDetailsMenuItems> list = new ArrayList<SaleDetailsMenuItems>(s);
//Log.e("Child size in adapter ","get children count "+this._listDataChild.get(key).size());
this._listDataChild.get(key).size();
}
catch(NullPointerException e)
{
e.printStackTrace();
return 0;
}
return this._listDataChild.get(key).size();
}
#Override
public Object getGroup(int groupPosition)
{
Set<SaleDetailsMenuItems> s = _listDataChild.keySet();
//List<SaleDetailsMenuItems> list = new ArrayList<SaleDetailsMenuItems>(s);
SaleDetailsMenuItems key = (SaleDetailsMenuItems) s.toArray()[groupPosition];
//Log.e("adapter get group ","get group "+key);
return key;
}
#Override
public int getGroupCount()
{
//Log.e("Group Count","Group Count "+_listDataChild.keySet().size());
return _listDataChild.keySet().size();
}
#Override
public long getGroupId(int groupPosition)
{
return groupPosition;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded,View convertView, ViewGroup parent)
{
final int index = groupPosition;
ExpandableListView mExpandableListView = (ExpandableListView) parent;
mExpandableListView.expandGroup(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);
TextView lblListHeader1 = (TextView) convertView.findViewById(R.id.lblListquantity1);
TextView lblListHeader2 = (TextView) convertView.findViewById(R.id.lblListprice1);
Button add = (Button) convertView.findViewById(R.id.btnadd);
Button minus = (Button) convertView.findViewById(R.id.Button01);
lblListHeader.setText(((SaleDetailsMenuItems) getGroup(groupPosition)).getName().toString());
lblListHeader1.setText(((SaleDetailsMenuItems) getGroup(groupPosition)).getQuantity().toString());
lblListHeader2.setText(((SaleDetailsMenuItems) getGroup(groupPosition)).getPrice().toString());
minus.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
// TODO Auto-generated method stub
listviewAdapter.notifyDataSetChanged();
putminusquantity(index);
//listviewAdapter.notifyDataSetChanged();
}
});
add.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
// TODO Auto-generated method stub
//Toast.makeText(context, shankar, Toast.LENGTH_SHORT).show();
//DeleteOrderScreen del = new DeleteOrderScreen();
listviewAdapter.notifyDataSetChanged();
putaddquantity(index);
//listviewAdapter.notifyDataSetChanged();
}
});
return convertView;
}
#Override
public boolean hasStableIds()
{
return false;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition)
{
return true;
}
}
In this,set is used to get the keyset from Hashmap.as set is unordered i got the list in unordered form .I want the expandable list in ordered form. So how to make my list ordered?? Please reply me as soon as possible.
One possibility is that you dont use HashMap or HashSet but TreeMap and TreeSet.
eithe rthe objects you enter already have a correct compareTo methods, or You have to use a custom comparator which you provide in the constructor of TreeMap or TreeSet.
Then the map or set are sorted: Example:
// auto sorted because Integer implements Comparable
SortedSet set = new TreeSet<Integer>();
// asume MyObject implements Comparable
SortedSet set = new TreeSet<MyObject>();
// asume MyObject needs a special other sorting
SortedSet set = new TreeSet<MyObject>(MyCustomComparator);
similar for Maps:
Map map = new TreeMap<Integer>;
or you also can use
SortedMap map = new TreeMap<Integer>;
I want to call another activity when i click button which is child view of expandable listview in Android.When i run the activity does not show the expandable listview.How to work on exapandable list view.Can some one give me idea how to do this.
Here is my Activity code
public class My_Project extends ExpandableListActivity
{
ExpandableListView expListView;
Button btnNewProject;
Button btn_myprojectdefinemyteam;
DBHelper databaseHelper;
SQLiteDatabase db;
ImageView imgButtonBack;
List dispDataList;
String[] array;
String[] Task_Title_Size;
String[] Task_Start_Date_Size;
String[] Task_CompletionDate_Size;
String[] Task_CompletionTime_Size;
String[] Task_Description_Size;
String[] Task_Status_Size;
String[] Task_IsActive_Size;
String[] dtrProjectNAmeSize;
public static final String Task_Title = "Task_Titles";
public static final String Task_Start_Date = "start_date";
public static final String Task_CompletionDate = "completion_date";
public static final String Task_CompletionTime = "completion_time";
public static final String Task_Description = "task_description";
public static final String Task_Status = "task_status";
public static final String Task_IsActive ="IS_Active";
private int ParentClickStatus=-1;
private int ChildClickStatus=-1;
Myexpandable_ListAdapter mAdapter;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.my_project);
imgButtonBack = (ImageView)findViewById(R.id.imagBackButton);//ImageView imgButtonBack;
imgButtonBack.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent iBack = new Intent(My_Project.this , Menu.class);
startActivity(iBack);
finish();
}
});
expListView = (ExpandableListView) findViewById(android.R.id.list);
databaseHelper = new DBHelper(getApplicationContext());
dispDataList=databaseHelper.viewMyProjectDetals();
dtrProjectNAmeSize=new String[dispDataList.size()];
System.out.println(" dtrProjectNAmeSize = " + dtrProjectNAmeSize);
if ( dispDataList.size() > 6 )
{
Task_Title_Size=(String[])dispDataList.get(0);
Task_Start_Date_Size=(String[]) dispDataList.get(1);
Task_CompletionDate_Size = (String[])dispDataList.get(2);
Task_CompletionTime_Size=(String[])dispDataList.get(3);
Task_Description_Size=(String[]) dispDataList.get(4);
Task_Status_Size = (String[])dispDataList.get(5);
Task_IsActive_Size = (String[])dispDataList.get(6);
for(int i=0;i<Task_Title_Size.length;i++)
{
System.out.println("New data :"+Task_Title_Size[i]);
}
List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();
for (int i = 0; i < Task_Title_Size.length ; i++)
{
Map<String, String> curGroupMap = new HashMap<String, String>();
groupData.add(curGroupMap);
curGroupMap.put(Task_Title,"" +Task_Title_Size[i]);
List<Map<String, String>> children = new ArrayList<Map<String, String>>();
Map<String, String> curChildMap = new HashMap<String, String>();
children.add(curChildMap);
curChildMap.put(Task_Start_Date,"" +Task_Start_Date_Size[i]);
curChildMap.put(Task_CompletionDate,"" +Task_CompletionDate_Size[i]);
curChildMap.put(Task_CompletionTime,"" +Task_CompletionTime_Size[i]);
curChildMap.put(Task_Description,"" +Task_Description_Size[i]);
curChildMap.put(Task_Status,"" +Task_Status_Size[i]);
curChildMap.put(Task_IsActive,"" +Task_IsActive_Size[i]);
childData.add(children);
}
mAdapter =new Myexpandable_ListAdapter(
this,
groupData,
R.layout.list_group,
new String[] { Task_Title },
new int[] {R.id.lblListHeader },
childData,
R.layout.child_item,
new String[] { Task_Start_Date , Task_CompletionDate , Task_CompletionTime , Task_Description , Task_Status , Task_IsActive},
new int[] { R.id.TextView_Projectdetails , R.id.textOne , R.id.textTwo , R.id.textThree , R.id.textFour, R.id.textFive}
);
expListView.setAdapter(mAdapter);
}
expListView.setOnGroupClickListener(new OnGroupClickListener()
{
#Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id)
{
return false;
}
});
//editTextTaskName = (EditText)findViewById(R.id.Task_Name);
expListView.setOnGroupExpandListener(new OnGroupExpandListener() {
#Override
public void onGroupExpand(int groupPosition) {
Toast.makeText(getApplicationContext(),
dtrProjectNAmeSize.length + " Expanded",
Toast.LENGTH_SHORT).show();
}
});
expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {
#Override
public void onGroupCollapse(int groupPosition) {
Toast.makeText(getApplicationContext(),
dtrProjectNAmeSize.length + " Collapsed",
Toast.LENGTH_SHORT).show();
}
});
btnNewProject = (Button)findViewById(R.id.buttonmyproject_NewProject);
btnNewProject.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(My_Project.this , Add_Project.class);
startActivity(i);
finish();
}
});
}
private class Myexpandable_ListAdapter extends BaseExpandableListAdapter {
public Myexpandable_ListAdapter(Context context,
List<? extends Map<String, ?>> groupData,
int expandedGroupLayout,
String[] groupFrom, int[] groupTo,
List<? extends List<? extends Map<String, ?>>> childData,
int childLayout, String[] childFrom,
int[] childTo) {
super();
// TODO Auto-generated constructor stub
}
private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;
Context context;
public Object getChild(int groupPosition, int childPosition)
{
//this.get(groupPosition).getChildren().get(childPosition);
String strchildPosition = this._listDataChild.get(this._listDataHeader.get(groupPosition)).get(childPosition);
System.out.println("Child Position =" + strchildPosition);
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosition);
}
//Call when child row clicked
#Override
public long getChildId(int groupPosition, int childPosition)
{
/****** When Child row clicked then this function call *******/
//Log.i("Noise", "parent == "+groupPosition+"= child : =="+childPosition);
if( ChildClickStatus!=childPosition)
{
ChildClickStatus = childPosition;
Toast.makeText(getApplicationContext(), "Parent :"+groupPosition + " Child :"+childPosition ,
Toast.LENGTH_LONG).show();
}
return childPosition;
}
public View getChildView1(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
return parent;
}
#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.child_item, null);
}
TextView txtListChild = (TextView) convertView.findViewById(R.id.TextView_Projectdetails);
TextView txtOneListChild = (TextView)convertView.findViewById(R.id.textOne);
TextView txtTwoListChild = (TextView)convertView.findViewById(R.id.textTwo);
TextView txtThreeListChild = (TextView)convertView.findViewById(R.id.textThree);
TextView txtFourListChild = (TextView)convertView.findViewById(R.id.textFour);
TextView txtFiveListChild = (TextView)convertView.findViewById(R.id.textFive);
txtListChild.setText(childText);
txtOneListChild.setText(childText);
txtTwoListChild.setText(childText);
txtThreeListChild.setText(childText);
txtFourListChild.setText(childText);
txtFiveListChild.setText(childText);
Button btnAssgnTask = (Button)convertView.findViewById(R.id.button_EditedTask);
btnAssgnTask.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
Button btnViewTask = (Button)convertView.findViewById(R.id.buttonViewTask);
btnViewTask.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
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 ParentClickStatus;
}
#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;
}
}
}
Thanks in Advance.
your problem is here:
#Override
public int getGroupCount() {
return ParentClickStatus;
}
as you extends BaseExpandableListAdapter, getChildrenCount and getGroupCount handle number of your view and as ParentClickStatus is equal to -1 getGroupView never called
solution
you need return group row number that you have, so change your code with:
#Override
public int getGroupCount() {
return _listDataHeader.size();
}
for an example you can see section 15. Implementing an expandable ListView
UPDATE
you need change your constructor too:
public Myexpandable_ListAdapter(Context context,
List<? extends Map<String, ?>> groupData,
int expandedGroupLayout,
String[] groupFrom, int[] groupTo,
List<? extends List<? extends Map<String, ?>>> childData,
int childLayout, String[] childFrom,
int[] childTo) {
super();
// TODO Auto-generated constructor stub
}
you passed your data to here, but you don't set to any local data, for example you tried to use _listDataChild in getChild method, but you not initialize that at all, you need initialize that in constructor with your data that passed here.
Can any one help me with this...I'm trying to implement expandable listview dynamically and i used one of the tutorials (Androidhive) to get familiar to it. My problem is that i am getting my data from parse.com and i want to load it dynamically into the expandable listview. First i am getting data from parse into my object classified by each type and then i suppose to convert that to the expandable listview. What i am stuck in is that i don't know how to pass to the adapter more than 1 object to the child (i have 3 textviews on each child). From the below example i got the data of main road from only to be viewed in the child but i still need to get 2 additional values which i don't know how to achieve.
Hereunder my code for the asynctask :
private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
#Override
protected void onPreExecute() {
super.onPreExecute();
// Create a progressdialog
mProgressDialog = new ProgressDialog(MainActivity.this);
// Set progressdialog title
// mProgressDialog.setTitle("Loading...");
// Set progressdialog message
mProgressDialog.setMessage("wait please ...");
mProgressDialog.setIndeterminate(false);
// Show progressdialog
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
mProgressDialog.setCancelable(false);
mProgressDialog.show();
}
#Override
protected Void doInBackground(Void... params) {
// Create the array
listviewPostsGet = new ArrayList<listviewPostsGet>();
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(
"MainRoadsList");
try {
ob = query.find();
for (ParseObject country : ob) {
listviewPostsGet map = new listviewPostsGet();
map.setmainroad(((String) country.get("mainroad")));
map.setmainroadfrom(((String) country.get("from")));
map.setmainroadto((String) country.get("to"));
map.setcategory(((String) country.get("mainroad")));
listviewPostsGet.add(map);
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
String check = "";
for (int i = 0; i < listviewPostsGet.size(); i++)
{
String XX = listviewPostsGet.get(i).getcategory()
.toString();
if (!check.contentEquals(XX.toString())) {
listDataHeader.add(XX);
check = XX;
List<String> TTT = new ArrayList<String>();
for (int j = 0; j < listviewPostsGet.size(); j++) {
String YY = listviewPostsGet.get(j).getcategory().toString();
if (YY.contentEquals(XX.toString()))
{
TTT.add(listviewPostsGet.get(j).getmainroadfrom().toString()+" "+listviewPostsGet.get(j).getmainroadto().toString());
listDataChild.put(XX, TTT);
}
}
}
}
}
}
catch (ParseException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Void result) {
listAdapter = new ExpandableListAdapter1(MainActivity.this,
listDataHeader, listDataChild);
// setting list adapter
expListView.setAdapter(listAdapter);
mProgressDialog.dismiss();
}
}
Also here is the adapter :
public class ExpandableListAdapter1 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 ExpandableListAdapter1(Context context, List<String> listDataHeader,
HashMap<String, List<String>> listDataChild ) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listDataChild;
}
public Object getChild(int groupPosition, int childPosititon) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition).toString())
.get(childPosititon);
}
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
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.mainlayout, null);
}
TextView from = (TextView) convertView
.findViewById(R.id.fromroad);
TextView to = (TextView) convertView
.findViewById(R.id.toroad);
from.setText(childText);
return convertView;
}
public int getChildrenCount(int groupPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.size();
}
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}
public int getGroupCount() {
return this._listDataHeader.size();
}
public long getGroupId(int groupPosition) {
return groupPosition;
}
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;
}
public boolean hasStableIds() {
return false;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
Edit :
to be clear , My problem is that I want to assign 3 values from my listviewPostsGet to the expandable listview child and what i can actually achieve is to pass only 1 value which is "Main road from" to the TTT array list then assign it to the corresponding textview on the adapter.