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);
Related
I have a display problem with my 4-stage expandableList.
I can display my data, expand them but the last child does not display the whole list. How would you do it?
Here is a picture and the part of the code in question, I think it comes from the fact that I declare the size of the last list.
public static class ThirdLevelAdapter extends BaseExpandableListAdapter {
private Context context;
private ArrayList<String> materiau;
private HashMap<String, ArrayList<MatosItem>> materiauItemList;
int c = 1;
LayoutInflater inflater3;
public ThirdLevelAdapter(Context context,HashMap<String, ArrayList<MatosItem>> materiauItemList,ArrayList<String> materiau) {
this.context = context;
this.materiauItemList = materiauItemList;
this.materiau = materiau;
this.inflater3 = LayoutInflater.from(context);
}
#Override
public Object getGroup(int groupPosition) {
return materiau.get(groupPosition);
}
#Override
public int getGroupCount() {
return materiau.size();
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(parent.getContext())
.inflate(android.R.layout.simple_expandable_list_item_1,
parent,false);
//Initialization and assign variable
TextView textView = convertView.findViewById(android.R.id.text1);
//Initial string pour le nom de famille
String sGroup = String.valueOf(getGroup(groupPosition));
//Set text on text view
textView.setText(sGroup);
//Set text style bold
textView.setTypeface(null, Typeface.BOLD);
//Set text color
//textView.setBackgroundColor(Color.parseColor("#FF018786"));
textView.setTextColor(Color.parseColor("#FF018786"));
textView.setTextSize(1,20);
//Return View
}
return convertView;
}
#Override
public Object getChild(int groupPosition, int childPosition) {
return materiauItemList.get(materiau.get(groupPosition)).get(childPosition);
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
#Override
public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
view = inflater3.inflate(R.layout.adapter_item2, null);
//get information about item
MatosItem currentItem = (MatosItem) getChild(i,i1);
String itemName = currentItem.getName();
String itemNumber = currentItem.getNumber();
Boolean intemCheck = currentItem.getCheck();
String itemRef = currentItem.getRef();
String itemCodefourn = currentItem.getCodefourn();
//get item name view
TextView itemNameView = view.findViewById(R.id.name);
itemNameView.setText(itemName);
//get item number view
TextView itemNumberView = view.findViewById(R.id.item_number);
itemNumberView.setText(itemNumber);
//get item ref view
TextView itemRefView = view.findViewById(R.id.ref);
itemRefView.setText(itemRef);
//get item codefournisseur
TextView itemCodefournView = view.findViewById(R.id.codefourn);
itemCodefournView.setText(itemCodefourn);
LinearLayout layout = view.findViewById(R.id.compteur);
Button btnplus = view.findViewById(R.id.btnplus);
Button btnmoins = view.findViewById(R.id.btnmoins);
TextView text = view.findViewById(R.id.text);
if(intemCheck){
//checkBox.setChecked(true);
mdescription.add(itemName);
mid.add(itemNumber);
mref.add(itemRef);
mcodefourn.add(itemCodefourn);
}
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
layout.setVisibility(View.VISIBLE);
mdescription.add(""+itemName);
mid.add(""+itemNumber);
mref.add(itemRef);
mcodefourn.add(itemCodefourn);
//checkBox.setChecked(true);
//Toast.makeText(viewGroup.getContext(),itemName , Toast.LENGTH_SHORT).show();
}
});
btnplus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
c=c+1;
text.setText(""+c);
mdescription.add(""+itemName);
mid.add(""+itemNumber);
mref.add(itemRef);
mcodefourn.add(itemCodefourn);
}
});
btnmoins.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
c=c-1;
text.setText(""+c);
mdescription.remove(""+itemName);
mid.remove(""+itemNumber);
mref.remove(itemRef);
mcodefourn.remove(itemCodefourn);
}
});
return view;
}
#Override
public int getChildrenCount(int groupPosition) {
return materiauItemList.get(materiau.get(groupPosition)).size();
}
#Override
public boolean hasStableIds() {
return true;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
level 1-2 and 3 work fine and i can scroll
level 4 is not displayed completely and I can't scroll
Thank you for your help
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);
I have been working on this all day, much of my code is from the sample code google put out in its sample API project. I have an expandablelistview menu, and I want one of my child classes to have a lot of text. But if I add a lot of text, the text goes off screen. It's tough to describe. Here is a screenshot:
Notice how the entry in the first child under the first parent goes off screen. I need view to automatically expand around the child's text so that you can see all of it.
Here is my code:
public class Assignment extends ExpandableListActivity {
ExpandableListAdapter mAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set up our adapter
mAdapter = new MyExpandableListAdapter();
setListAdapter(mAdapter);
registerForContextMenu(getExpandableListView());
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
menu.setHeaderTitle("Sample menu");
menu.add(0, 0, 0, R.string.expandable_list_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;
}
/**
* A simple adapter which maintains an ArrayList of photo resource Ids.
* Each photo is displayed as an image. This adapter supports clearing the
* list of photos and adding a new photo.
*
*/
public class Assignment extends ExpandableListActivity {
ExpandableListAdapter mAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set up our adapter
mAdapter = new MyExpandableListAdapter();
setListAdapter(mAdapter);
registerForContextMenu(getExpandableListView());
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
menu.setHeaderTitle("Sample menu");
menu.add(0, 0, 0, R.string.expandable_list_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;
}
/**
* A simple adapter which maintains an ArrayList of photo resource Ids.
* Each photo is displayed as an image. This adapter supports clearing the
* list of photos and adding a new photo.
*
*/
public class MyExpandableListAdapter extends BaseExpandableListAdapter {
// Sample data set. children[i] contains the children (String[]) for groups[i].
private String[] groups = { "P", "D", "N"};
private String[][] children = {
{ "Arnoadsfadsfadsfaaaaaaaaaaaaaaaaaaaaaaaa" +
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaadsafdasfadsf" +
"adsfdasfdsafadsfld", "Barry", "Chuck", "David" },
{ "Ace", "Bandit", "Cha-Cha", "Deuce" },
{ "Fluffy", "Snuggles" },
{ "Goldy", "Bubbles" }
};
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(Assignment.this);
textView.setLayoutParams(lp);
// Center the text vertically
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
// Set the text starting position
textView.setPadding(36, 0, 0, 0);
return textView;
}
public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
View convertView, ViewGroup parent) {
TextView textView = getGenericView();
textView.setTextSize(18);
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;
}
//Set design for Parent Lists
public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
ViewGroup parent) {
TextView textView = getGenericView();
textView.setTextSize(30);
textView.setPadding(50, 0, 0, 0);
textView.setText(getGroup(groupPosition).toString());
return textView;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
public boolean hasStableIds() {
return true;
}
}
}
Notice One of my childs names is "Arnoadsfadsfadsfaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaadsafdasfadsf"
And in the screenshot, this goes off screen. What I would like to happen, is the child layout to automatically expand to encompass all the text. Not sure how to do that. Any help would be greatly appreciated. Thanks!
I feel dumb but just solved this, under my method getGenericView(), width was set to match_parent but height was not.
It was this:
public TextView getGenericView() {
// Layout parameters for the ExpandableListView
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, 64);
But it needed to be this
public TextView getGenericView() {
// Layout parameters for the ExpandableListView
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
i am getting a null pointer exception
public class ApisimpleActivity extends ExpandableListActivity {
ExpandableListAdapter mAdapter;
Button btn;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set up our adapter
setContentView(R.layout.main);
btn=(Button)findViewById(R.id.button);
btn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
int i=getExpandableListView().getChildCount();
int k=0;
//Toast.makeText(ApisimpleActivity.this, "hello"+i, Toast.LENGTH_LONG).show();
for(int j=0;j<i;j++)
{
View v=(View)getExpandableListView().getChildAt(i);
if(v==null)
//if(v.findViewById(R.id.chk)==null)
k++;
}
Toast.makeText(ApisimpleActivity.this, k+"",Toast.LENGTH_LONG).show();
} });
mAdapter = new MyExpandableListAdapter();
setListAdapter(mAdapter);
registerForContextMenu(getExpandableListView());
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
menu.setHeaderTitle("Sample menu");
menu.add(0, 0, 0, "Hello World");
}
#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;
}
/**
* A simple adapter which maintains an ArrayList of photo resource Ids.
* Each photo is displayed as an image. This adapter supports clearing the
* list of photos and adding a new photo.
*
*/
public class MyExpandableListAdapter extends BaseExpandableListAdapter {
// Sample data set. children[i] contains the children (String[]) for groups[i].
private String[] groups = { "People Names", "Dog Names", "Cat Names", "Fish Names" };
private String[][] children = {
{ "Arnold", "Barry", "Chuck", "David" },
{ "Ace", "Bandit", "Cha-Cha", "Deuce" },
{ "Fluffy", "Snuggles" },
{ "Goldy", "Bubbles" }
};
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.FILL_PARENT, 64);
TextView textView = new TextView(ApisimpleActivity.this);
textView.setLayoutParams(lp);
// Center the text vertically
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
// Set the text starting position
textView.setPadding(36, 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) {
View v;
v=convertView;
if(v==null)
{LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.group, null);
}
return v;}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
public boolean hasStableIds() {
return true;
}
}
}
on btn.setOnClickListener when i call the function
getExpandableListView().getChildAt(i) all the views are null.can any one please help why all the view are showing null i need to get the view and check on each weather checkbox is checked or not.please some one help
i think you want getExpandableListView().getChildAt(j) (j instead of i, for j is the size of the list, therefor not in it)
Edit: not really important here, but using k+"" to print an integer is slow. Use String.valueOf(k) instead
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.