apologies for the code dump - if you prefer the github version this is the code I'm working from:
https://github.com/Barbelos/ThreeLevelExpandableListVIew
The list is working fine except for one thing - the only way I could figure to set the height in onMeasure was by measuring the size of the RelativeLayout the list expands into - so if the list is bigger than the screen you can scroll it.
For some reason (I don't really understand the code), the second level expands fine - you can put as many items in there and it will just expand to fit. The problem happens on the third level - the items get truncated at what looks to me like the height of the screen. They're still there - if you hold down on some other part of the screen you can scroll the third level, but that's not very intuitive. Can anybody see a way to make this work? I've tried changing the xml but got nowhere, and MeasureSpec.UNSPECIFIED seems to do the exact opposite of what I'm trying to do...
MainActivity.java:
package com.example.threeleveltest;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.widget.ExpandableListView;
import android.widget.RelativeLayout;
public class MainActivity extends Activity {
public static CustomExpandableListView list;
static int ht;
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus == true) {
RelativeLayout parent= (RelativeLayout) findViewById(R.id.parent);
ht = parent.getHeight();
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int noObjectsLevel1= 15;
int noObjectsLevel2= 24;
int noObjectsLevel3= 57;
List<Object> objectsLvl1= new ArrayList<Object>();
for (int i=0; i<noObjectsLevel1; i++) {
List<Object> objectsLvl2= new ArrayList<Object>();
for (int j=0; j<noObjectsLevel2; j++) {
List<Object> objectsLvl3= new ArrayList<Object>();
for (int k=0; k<noObjectsLevel3; k++) {
objectsLvl3.add(new Object("lvl3_"+String.valueOf(k), null));
}
objectsLvl2.add(new Object("lvl2_"+String.valueOf(j), objectsLvl3));
}
objectsLvl1.add(new Object("lvl1_"+String.valueOf(i), objectsLvl2));
}
RelativeLayout parent= (RelativeLayout) findViewById(R.id.parent);
list= new CustomExpandableListView(this);
Adapter adapter= new Adapter(this, objectsLvl1);
list.setAdapter(adapter);
parent.addView(list);
}
}
class CustomExpandableListView extends ExpandableListView {
public CustomExpandableListView(Context context) {
super(context);
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
/*
* Adjust height
*/
int h=MainActivity.ht;
heightMeasureSpec = MeasureSpec.makeMeasureSpec(h, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
Adapter.java:
package com.example.threeleveltest;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
public class Adapter extends BaseExpandableListAdapter {
private List<Object> objects;
private Activity activity;
private LayoutInflater inflater;
public Adapter(Activity activity, List<Object> objects) {
this.objects= objects;
this.activity= activity;
this.inflater= (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public Object getChild(int groupPosition, int childPosition) {
return objects.get(groupPosition).getObjects().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) {
Object object= (Object) getChild(groupPosition, childPosition);
CustomExpandableListView subObjects= (CustomExpandableListView) convertView;;
if (convertView==null) {
subObjects= new CustomExpandableListView(activity);
}
Adapter2 adapter= new Adapter2(activity, object);
subObjects.setAdapter(adapter);
return subObjects;
}
#Override
public int getChildrenCount(int groupPosition) {
return objects.get(groupPosition).getObjects().size();
}
#Override
public Object getGroup(int groupPosition) {
return objects.get(groupPosition);
}
#Override
public int getGroupCount() {
return objects.size();
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
Object object= (Object) getGroup(groupPosition);
if (convertView==null) {
convertView= inflater.inflate(R.layout.listview_element, null);
}
TextView name= (TextView) convertView.findViewById(R.id.name);
name.setText(object.getName());
return convertView;
}
#Override
public boolean hasStableIds() {
return true;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
class Adapter2 extends BaseExpandableListAdapter {
private Object object;
private LayoutInflater inflater;
private Activity activity;
public Adapter2(Activity activity, Object object) {
this.activity= activity;
this.object= object;
this.inflater= (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public Object getChild(int groupPosition, int childPosition) {
return object.getObjects().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) {
Object object= (Object) getChild(0, childPosition);
if (convertView==null) {
convertView= inflater.inflate(R.layout.listview_element, null);
Resources r = activity.getResources();
float px40 = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 40, r.getDisplayMetrics());
convertView.setPadding(
convertView.getPaddingLeft() + (int) px40,
convertView.getPaddingTop(),
convertView.getPaddingRight(),
convertView.getPaddingBottom());
}
TextView name= (TextView) convertView.findViewById(R.id.name);
name.setText(object.getName());
return convertView;
}
#Override
public int getChildrenCount(int groupPosition) {
return object.getObjects().size();
}
#Override
public Object getGroup(int groupPosition) {
return object;
}
#Override
public int getGroupCount() {
return 1;
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
if (convertView==null) {
convertView= inflater.inflate(R.layout.listview_element, null);
Resources r = activity.getResources();
float px20 = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20, r.getDisplayMetrics());
convertView.setPadding(
convertView.getPaddingLeft() + (int) px20,
convertView.getPaddingTop(),
convertView.getPaddingRight(),
convertView.getPaddingBottom());
}
TextView name= (TextView) convertView.findViewById(R.id.name);
name.setText(object.getName());
return convertView;
}
#Override
public boolean hasStableIds() {
return true;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
Object.java:
package com.example.threeleveltest;
import java.util.List;
public class Object {
String name;
List<Object> objects;
public Object(String name, List<Object> objects) {
this.name= name;
this.objects= objects;
}
public String getName() {
return this.name;
}
public List<Object> getObjects() {
return this.objects;
}
}
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/parent"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
</RelativeLayout>
listview_element.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:textSize="40dp"
android:paddingLeft="40dp" />
</RelativeLayout>
This isn't much of an answer, more of a workaround, really, but I post it here in case anybody comes up against the same problem in future - I'm not nearly smart enough to solve the original problem, so I ended up just using a standard two-level expandable list, and when you click on the 2nd level it brings up a dialog with a listview of the third level items. Looks ok - actually works better on small screens, and you can add as many items as you like.
I'm too late but I hope this can help other people with the same issue. The way I fixed this was to increase the size in makeMeasureSpec() in CustomExpandableView to a very large number. It really depends on how many items you wanna show on the 3rd level.
heightMeasureSpec = MeasureSpec.makeMeasureSpec(5000, MeasureSpec.AT_MOST);
Related
I have created a expandable list view and two radio buttons in it.
I want to get the value of the radio button which is selected when some button is pressed in my main Activity. My ExpandableListView is working properly but i am not able to get the radio button which is selected in main activity.
public class ExpandableMenuListAdapter extends BaseExpandableListAdapter
{
private Context context;
private List<String> listDataHeader;
private HashMap<String,List<List<String>>> listHashMap;
public ExpandableMenuListAdapter(Context context, List<String> listDataHeader, HashMap<String, List<List<String>>> listHashMap) {
this.context = context;
this.listDataHeader = listDataHeader;
this.listHashMap = listHashMap;
}
#Override
public int getGroupCount() {
return listDataHeader.size();
}
#Override
public int getChildrenCount(int groupPosition) {
return Objects.requireNonNull(listHashMap.get(listDataHeader.get(groupPosition))).size();
}
#Override
public Object getGroup(int groupPosition) {
return listDataHeader.get(groupPosition);
}
#Override
public Object getChild(int groupPosition, int childPosition) {
return Objects.requireNonNull(listHashMap.get(listDataHeader.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) {
String headerTitle = (String)(getGroup(groupPosition)).toString();
if (convertView==null)
{
LayoutInflater inflater =(LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_menu_days_group,null);
}
TextView lblListHeader = convertView.findViewById(R.id.list_days);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
return convertView;
}
#Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
List<String> childText = (List<String>) getChild(groupPosition,childPosition);
if (convertView==null)
{
LayoutInflater inflater =(LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_menu_item,null);
}
RadioButton txtListChild = convertView.findViewById(R.id.menu_list_daily1);
RadioButton txtListChild2 = convertView.findViewById(R.id.menu_list_daily2);
txtListChild.setText(childText.get(0));
txtListChild2.setText(childText.get(1));
ViewsList.menu.add(convertView);
return convertView;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
}
Use onChildClick method in your code.This method is automatically called when you click on child item
#Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
//get your radio here from the view v and do whatever you want
return true;
}
I have a ExpandableListView in a Fragment.
I'm posting a request to the server using RoboSpice onCreateView.
I want to be able to update the list twice, once when I get the data from the cache and second after the response returns.
For some reason the list is not populated.
I was able to populate it when I instantiate a new adapter and set the adapter for list, but for some reason it doesn't work on some android devices.
This is the code I'm using:
Fragment
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
_expandableListAdapter = new GroupsExpandableListAdapter(getActivity(), getSpiceManager());
_groupListView.setAdapter(_expandableListAdapter);
DBServerHelper.Groups.getByBusinessId(getActivity(), getSpiceManager(), getDBHelper(), _clientBusiness.getId(),
new CachedRequestListener<ClientGroup.List>(getDBHelper(), getActivity()) {
#Override
// This method runs twice, after the data comes from the cache and once the request succeed
public void onRequestFromCache(ClientGroup.List clientGroups) {
if (clientGroups == null || clientGroups.isEmpty()) {
Ln.d("Received clientGroups " + clientGroups == null ? "null" : "empty" + " form request GetByBusinessId");
return;
}
_expandableListAdapter.clear();
_expandableListAdapter.setGroups(clientGroups);
_expandableListAdapter.notifyDataSetChanged();
_groupListView.invalidateViews();
}
#Override
public void onRequestFailure(SpiceException spiceException) {
super.onRequestFailure(spiceException);
Ln.e(spiceException, "Failed to get groups ");
Toaster.showLong(getActivity(), "Could not get businesses groups: " + spiceException.getMessage());
}
});
}
GroupsExpandableListAdapter
public class GroupsExpandableListAdapter extends BaseExpandableListAdapter {
private ClientGroup.List _data;
private Activity _activity;
/**
* It is only possible to create a group using the factory method create below
* #param activity
*/
public GroupsExpandableListAdapter(Activity activity, SpiceManager manager) {
super();
_data = new ClientGroup.List();
_activity = activity;
_manager = manager;
}
#Override
public int getGroupCount() {
return _data.size();
}
#Override
public int getChildrenCount(int groupPosition) {
return _data.size();
}
#Override
public Object getGroup(int groupPosition) {
return _data.get(groupPosition).getName();
}
#Override
public Object getChild(int groupPosition, int childPosition) {
return _data.get(groupPosition).getDeleteReason();
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
#Override
public boolean hasStableIds() {
return false;
}
public void clear() {
_data.clear();
}
public void setGroups(ClientGroup.List groups) {
_groups = groups;
}
#Override
public View getGroupView(final int groupPosition, final boolean isExpanded, final View convertView,
final ViewGroup parent) {
View v;
if (convertView == null) {
LayoutInflater li = (LayoutInflater) _activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = li.inflate(R.layout.group_list_group, parent, false);
TextView textView = (TextView) v.findViewById(R.id.groupName);
textView.setText((CharSequence) _data.get(groupPosition).getName());
((ImageView) v.findViewById(R.id.groupIndicator))
.setImageResource(isExpanded ? R.drawable.arrow_up_turquoise : R.drawable.arrow_down_white);
} else {
v = convertView;
}
return v;
}
#Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
View v;
if (convertView == null) {
LayoutInflater li = (LayoutInflater) _activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = li.inflate(R.layout.group_list_child, parent, false);
} else {
v = convertView;
}
TextView joinGroupButton = (TextView) v.findViewById(R.id.joinGroupButton);
final ClientGroup group = _data.get(groupPosition);
String leaveGroupStr = _activity.getString(R.string.leave_group);
String joinGroupStr = _activity.getString(R.string.join_group);
return v;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
}
Any suggestions?
Thanks!
I want to display a tree having the individual name of its parent, second node, and its child nodes. I've coded by using Google help. This code display tree has all second and child nodes with the same names. How can I give an unique name to each node of a tree?
My Java code is:
package com.example.tree;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;
public class MainActivity extends Activity {
ExpandableListView explvlist;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
explvlist = (ExpandableListView)findViewById(R.id.ParentLevel);
explvlist.setAdapter(new ParentLevel());
}
public class ParentLevel extends BaseExpandableListAdapter
{
#Override
public Object getChild(int arg0, int arg1)
{
return arg1;
}
#Override
public long getChildId(int groupPosition, int childPosition)
{
return childPosition;
}
#Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent)
{
CustExpListview SecondLevelexplv = new CustExpListview(MainActivity.this);
SecondLevelexplv.setAdapter(new SecondLevelAdapter());
SecondLevelexplv.setGroupIndicator(null);
return SecondLevelexplv;
}
#Override
public int getChildrenCount(int groupPosition)
{
return 4;
}
#Override
public Object getGroup(int groupPosition)
{
return groupPosition;
}
#Override
public int getGroupCount()
{
return 1;
}
#Override
public long getGroupId(int groupPosition)
{
return groupPosition;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent)
{
TextView tv = new TextView(MainActivity.this);
tv.setText("Unit Testing");
tv.setBackgroundColor(Color.BLUE);
tv.setPadding(10, 7, 7, 7);
return tv;
}
#Override
public boolean hasStableIds()
{
return true;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition)
{
return true;
}
}
public class CustExpListview extends ExpandableListView
{
int intGroupPosition, intChildPosition, intGroupid;
public CustExpListview(Context context)
{
super(context);
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
widthMeasureSpec = MeasureSpec.makeMeasureSpec(960, MeasureSpec.AT_MOST);
heightMeasureSpec = MeasureSpec.makeMeasureSpec(600, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
public class SecondLevelAdapter extends BaseExpandableListAdapter
{
#Override
public Object getChild(int groupPosition, int childPosition)
{
return 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)
{
TextView tv = new TextView(MainActivity.this);
tv.setText("Campaign Page should not be null");
tv.setPadding(15, 5, 5, 5);
tv.setBackgroundColor(Color.RED);
tv.setLayoutParams(new ListView.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
return tv;
}
#Override
public int getChildrenCount(int groupPosition)
{
return 2;
}
#Override
public Object getGroup(int groupPosition)
{
return groupPosition;
}
#Override
public int getGroupCount()
{
return 1;
}
#Override
public long getGroupId(int groupPosition)
{
return groupPosition;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent)
{
TextView tv = new TextView(MainActivity.this);
tv.setText("Get Campaign");
tv.setPadding(12, 7, 7, 7);
tv.setBackgroundColor(Color.CYAN);
return tv;
}
#Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return true;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return true;
}
}
}
I want each second and child nodes of tree to have an unique name and different colors.
Means you want to see the tree of data depending on the parent name.? So you used expandable list view.? right..?
Use these code..
main.xml having
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="HardCodedText" >
<ExpandableListView
android:id="#+id/expandableListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="40dip"
android:groupIndicator="#null"
android:scrollbars="none" >
</ExpandableListView>
</RelativeLayout>
And create the one ExpandListGroup.java file and add these code
public class ExpandListGroup {
private String Name;
private ArrayList<ExpandListChild> Items;
public ExpandListGroup(String name, ArrayList<ExpandListChild> items) {
super();
Name = name;
Items = items;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public ArrayList<ExpandListChild> getItems() {
return Items;
}
public void setItems(ArrayList<ExpandListChild> items) {
Items = items;
}
}
Add the another one ExpandListChild.java file for child
public class ExpandListChild {
private String Name;
private String Tag;
public ExpandListChild(String name, String tag) {
super();
Name = name;
Tag = tag;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getTag() {
return Tag;
}
public void setTag(String tag) {
Tag = tag;
}
}
And add these code in your MainActivity.java
public class ConfigureMyOrderItem extends MainActivity {
private ArrayList<ExpandableConfigureGroup> group_list;
private ArrayList<ExpandableConfigureChild> child_list;
ExpandableListView mExpandableListView;
ConfigureMyOrderAdapter adapter;
Button btn_confirm;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_configure_my_order_item);
initView();
}
public void initView() {
mExpandableListView = (ExpandableListView) findViewById(R.id.expandableListViewConfigure);
btn_confirm = (Button) findViewById(R.id.btn_Confirm_order);
btn_confirm.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(ConfigureMyOrderItem.this,
MyOrderActivity.class);
startActivity(intent);
}
});
group_list = SetStandardGroups();
adapter = new ConfigureMyOrderAdapter(ConfigureMyOrderItem.this,
mExpandableListView, group_list);
mExpandableListView.setAdapter(adapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater()
.inflate(R.menu.activity_configure_my_order_item, menu);
return true;
}
public ArrayList<ExpandableConfigureGroup> SetStandardGroups() {
group_list = new ArrayList<ExpandableConfigureGroup>();
child_list = new ArrayList<ExpandableConfigureChild>();
group_list.add(new ExpandableConfigureGroup("Group", child_list));
child_list.add(new ExpandableConfigureChild("Child1"));
child_list.add(new ExpandableConfigureChild("Child2"));
child_list.add(new ExpandableConfigureChild("Child3"));
child_list.add(new ExpandableConfigureChild("Child4"));
child_list = new ArrayList<ExpandableConfigureChild>();
group_list.add(new ExpandableConfigureGroup("Category",
child_list));
child_list.add(new ExpandableConfigureChild("Item1"));
child_list.add(new ExpandableConfigureChild("Item2"));
child_list.add(new ExpandableConfigureChild("Item3"));
child_list.add(new ExpandableConfigureChild("Item4"));
}
public void HomeButton(View v) {
startActivity(new Intent(v.getContext(), MainActivity.class));
}
#Override
public void onClickQuickView(View v) {
// TODO Auto-generated method stub
super.onClickQuickView(v);
}
#Override
public void onClickQuickViewStatus(View v) {
// TODO Auto-generated method stub
super.onClickQuickViewStatus(v);
}
}
Also create the ConfigureMyOrderAdapter.java file
public class ConfigureMyOrderAdapter extends BaseExpandableListAdapter {
private Context context;
private ArrayList<ExpandableConfigureGroup> groups;
private ExpandableListView mExpandableListView;
private int[] groupStatus;
public ConfigureMyOrderAdapter(Context context,
ExpandableListView mExpandableListView,
ArrayList<ExpandableConfigureGroup> groups) {
this.context = context;
this.groups = groups;
this.mExpandableListView = mExpandableListView;
groupStatus = new int[groups.size()];
setListEvent();
}
private void setListEvent() {
mExpandableListView
.setOnGroupExpandListener(new OnGroupExpandListener() {
#Override
public void onGroupExpand(int arg0) {
// TODO Auto-generated method stub
groupStatus[arg0] = 1;
}
});
mExpandableListView
.setOnGroupCollapseListener(new OnGroupCollapseListener() {
#Override
public void onGroupCollapse(int arg0) {
// TODO Auto-generated method stub
groupStatus[arg0] = 0;
}
});
}
public void addItem(ExpandableConfigureChild item,
ExpandableConfigureGroup group) {
if (!groups.contains(group)) {
groups.add(group);
}
int index = groups.indexOf(group);
ArrayList<ExpandableConfigureChild> ch = groups.get(index).getItems();
ch.add(item);
groups.get(index).setItems(ch);
}
public Object getChild(int groupPosition, int childPosition) {
ArrayList<ExpandableConfigureChild> chList = groups.get(groupPosition)
.getItems();
return chList.get(childPosition);
}
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
#Override
public View getChildView(int groupPosition, int childPosition,
boolean arg2, View view, ViewGroup arg4) {
ExpandableConfigureChild child = (ExpandableConfigureChild) getChild(
groupPosition, childPosition);
if (view == null) {
#SuppressWarnings("static-access")
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(context.LAYOUT_INFLATER_SERVICE);
view = infalInflater.inflate(
R.layout.configure_list_raw_group_item, null);
}
TextView tv_price = (TextView) view.findViewById(R.id.item_price);
tv_price.setText(child.getTag().toString());
tv_price.setTag(child.getTag());
return view;
}
#Override
public int getChildrenCount(int groupPosition) {
ArrayList<ExpandableConfigureChild> chList = groups.get(groupPosition)
.getItems();
return chList.size();
}
#Override
public Object getGroup(int groupPosition) {
return groups.get(groupPosition);
}
#Override
public int getGroupCount() {
return groups.size();
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#SuppressWarnings("static-access")
#Override
public View getGroupView(int groupPosition, boolean arg1, View view,
ViewGroup arg3) {
ExpandableConfigureGroup group = (ExpandableConfigureGroup) getGroup(groupPosition);
if (view == null) {
LayoutInflater inf = (LayoutInflater) context
.getSystemService(context.LAYOUT_INFLATER_SERVICE);
view = inf.inflate(R.layout.configure_list_raw_group, null);
}
TextView tv = (TextView) view.findViewById(R.id.txtRestaurantMenuName);
tv.setText(group.getName());
ImageView img = (ImageView) view.findViewById(R.id.img_rightarrow);
if (groupStatus[groupPosition] == 0) {
img.setImageResource(R.drawable.navigation_next);
} else {
img.setImageResource(R.drawable.navigation_expandable);
}
return view;
}
#Override
public boolean hasStableIds() {
return true;
}
#Override
public boolean isChildSelectable(int arg0, int arg1) {
return true;
}
}
and also create the two xml for the custom configure_list_raw_group.xml layout. so it is
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="45dip"
android:orientation="horizontal"
android:weightSum="100"
tools:ignore="HardCodedText" >
<LinearLayout
android:layout_width="0dip"
android:layout_height="45dip"
android:layout_marginLeft="4dip"
android:layout_weight="88"
android:gravity="center_vertical"
android:orientation="vertical" >
<TextView
android:id="#+id/txtRestaurantMenuName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RestaurantMenu Name"
android:textSize="14sp" />
</LinearLayout>
<ImageView
android:id="#+id/img_rightarrow"
android:layout_width="0dip"
android:layout_height="45dip"
android:layout_gravity="center_vertical"
android:layout_weight="7"
android:contentDescription="#string/hello_world"
android:src="#drawable/navigation_next_item" />
</LinearLayout>
and second for the child of group configure_list_raw_group_item.xml.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/groupItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:orientation="horizontal"
android:weightSum="100"
tools:ignore="HardCodedText" >
<TextView
android:id="#+id/item_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_weight="75"
android:text="sample"
android:textSize="12sp" />
</LinearLayout>
I am attempting to start an activity from the children of my expandable list This framework is taken from the sample in the Android SDK, and is the core for my application. Here is teh code and I will identify which areas are not working.
package com.soraingraven.suprRef;
import android.app.ExpandableListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;
public class SuperReferenceActivity extends ExpandableListActivity {
ExpandableListAdapter mAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set up our adapter
mAdapter = new MyExpandableListAdapter();
setListAdapter(mAdapter);
//registerForContextMenu(getExpandableListView());
getExpandableListView().setOnChildClickListener(this);
}
#Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
super.onListItemClick(parent, v, groupPosition, childPosition, id);
String testName[][] = children[groupPosition][childPosition];
try {
Class clazz = Class.forName("com.soraingraven.suprRef." + testName);
Intent intent = new Intent(this, clazz);
startActivity(intent);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
// use groupPosition and childPosition to locate the current item in the adapter
return true;
}
/*#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
menu.setHeaderTitle("Sample menu");
menu.add(0, 0, 0, "Sample action");
}
#Override
public boolean onContextItemSelected(MenuItem item) {
ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) item.getMenuInfo();
String title = ((TextView) info.targetView).getText().toString();
int type = ExpandableListView.getPackedPositionType(info.packedPosition);
if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition);
int childPos = ExpandableListView.getPackedPositionChild(info.packedPosition);
Toast.makeText(this, title + ": Child " + childPos + " clicked in group " + groupPos,
Toast.LENGTH_SHORT).show();
return true;
} else if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition);
Toast.makeText(this, title + ": Group " + groupPos + " clicked", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}*/ //Context Menu Stuff May or may not use
//Indexes for all the options
public class MyExpandableListAdapter extends BaseExpandableListAdapter {
private String[] groups = {"Math Formulas and Equations"}; //Main Menu
private String[][] children = { //Sub Menus
{ "PerfectGasLaw" } //Math Equations Sub Menu
};
public Object getChild(int groupPosition, int childPosition) {
return children[groupPosition][childPosition];
}
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
public int getChildrenCount(int groupPosition) {
return children[groupPosition].length;
}
public TextView getGenericView() {
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, 128);
TextView textView = new TextView(SuperReferenceActivity.this);
textView.setLayoutParams(lp);
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
textView.setPadding(128, 0, 0, 0);
return textView;
}
public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
View convertView, ViewGroup parent) {
TextView textView = getGenericView();
textView.setText(getChild(groupPosition, childPosition).toString());
return textView;
}
public Object getGroup(int groupPosition) {
return groups[groupPosition];
}
public int getGroupCount() {
return groups.length;
}
public long getGroupId(int groupPosition) {
return groupPosition;
}
public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
ViewGroup parent) {
TextView textView = getGenericView();
textView.setText(getGroup(groupPosition).toString());
return textView;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
public boolean hasStableIds() {
return true;
}
}
}
Line 31 I cannot figure out how to get the super reference class to accept the children array, as it is listed as private to another class. Please help. Im not sure if i need to create a 2D array inside the activity class and pass it the data contained in children or what exactly needs to be done. Thanks.
You're trying to concatenate a 2d String array to a String! Remove the [][]
String testName = children[groupPosition][childPosition];
try {
Class clazz = Class.forName("com.soraingraven.suprRef." + testName);
I'm having trouble with setting an OnClickListener to list items in my ExpandableListActivity. The code structure matches the following example:
ExpandableListAdapter mAdapter;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Assign the adapter
mAdapter = new MyExpandableListAdapter();
setListAdapter(mAdapter);
}
public class MyExpandableListAdapter extends BaseExpandableListAdapter {
private String[] groups = { "foo", "bar"};
private String[][] children = {
{"fooA", "barA"},
{"fooB", "barB"}
};
public Object getChild(int groupPosition, int childPosition) {
return children[groupPosition][childPosition];
}
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
public int getChildrenCount(int groupPosition) {
return children[groupPosition].length;
}
public TextView getGenericView() {
// Layout parameters for the ExpandableListView
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, 64);
TextView textView = new TextView(Calculations.this);
textView.setLayoutParams(lp);
// Center the text vertically
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
// Set the text starting position
textView.setPadding(64, 0, 0, 0);
return textView;
}
public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
View convertView, ViewGroup parent) {
TextView textView = getGenericView();
textView.setText(getChild(groupPosition, childPosition).toString());
textView.setPadding(98, 0, 0, 0);
return textView;
}
public Object getGroup(int groupPosition) {
return groups[groupPosition];
}
public int getGroupCount() {
return groups.length;
}
public long getGroupId(int groupPosition) {
return groupPosition;
}
public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
ViewGroup parent) {
TextView textView = getGenericView();
textView.setText(getGroup(groupPosition).toString());
return textView;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
public boolean hasStableIds() {
return true;
}
}
Once I'm able to set the listener, I'll most likely do something like this:
// Listener
{
switch(groupPosition) {
case 0:
switch(childPosition) {
case 0:
selected = 00;
return true;
}
}
}// Close listener
Please tell me if I'm not being clear enough.
You should override onChildClick() in your activity.
boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id)
Override this for receiving callbacks when a child has been clicked.