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
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
I am using an expandible view where parent is a text view and children are edittext views. I want to show different hints for different edit texts. I tried to do it programmatically but no success at all. Here is my code.
ContactFragment.java
public class ContactFragment extends Fragment {
private View mView;
Context mContext;
EditText edittext;
ExpandableListAdapter listAdapter;
ExpandableListView elv;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = getActivity();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mView = inflater.inflate(R.layout.fragment_contact_details, container, false);
ExpandableListView elv = (ExpandableListView) mView.findViewById(R.id.list);
prepareListData();
listAdapter = new ExpandableListAdapter(mContext, listDataHeader,listDataChild);
elv.setAdapter(listAdapter);
elv.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
#Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
// Toast.makeText(getApplicationContext(),
// "Group Clicked " + listDataHeader.get(groupPosition),
// Toast.LENGTH_SHORT).show();
return false;
}
});
// Listview Group expanded listener
elv.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
#Override
public void onGroupExpand(int groupPosition) {
Toast.makeText(mContext,
listDataHeader.get(groupPosition) + " Expanded",
Toast.LENGTH_SHORT).show();
}
});
// Listview Group collasped listener
elv.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
#Override
public void onGroupCollapse(int groupPosition) {
Toast.makeText(mContext,
listDataHeader.get(groupPosition) + " Collapsed",
Toast.LENGTH_SHORT).show();
}
});
// Listview on child click listener
elv.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
Toast.makeText(
mContext,
listDataHeader.get(groupPosition)
+ " : "
+ listDataChild.get(
listDataHeader.get(groupPosition)).get(
childPosition), Toast.LENGTH_SHORT)
.show();
return false;
}
});
return mView;
}
/*
* Preparing the list data
*/
private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
// Adding child data
listDataHeader.add("Contact Details");
// Adding child data
List<String> basic = new ArrayList<String>();
basic.add(" ");
basic.add(" ");
basic.add(" ");
basic.add(" ");
basic.add(" ");
listDataChild.put(listDataHeader.get(0), basic); // Header, Child data
}
}
ExpandableListAdapter.java
public class ExpandableListAdapter extends BaseExpandableListAdapter implements TextWatcher{
private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;
private ArrayList<EditText> editTextList = new ArrayList<EditText>();
int i=1;
String name,purpose;
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.fragment_contact_details_child, null);
}
EditText editetext = (EditText) convertView .findViewById(R.id.item);
while(i<6)
{
switch (i)
{
case 1:editetext.setHint("Contact Person");
break;
case 2:editetext.setHint("Contact Person Photo");
break;
case 3:editetext.setHint("Designation/Job");
break;
case 4:editetext.setHint("CEO Name");
break;
case 5:editetext.setHint("Company Name");
break;
}
i++;
}
editetext.addTextChangedListener(this);
editTextList.add(editetext);
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.fragment_contact_details_parent, null);
}
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.textViewParent);
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;
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
name=editTextList.get(0).getText().toString();
purpose=editTextList.get(1).getText().toString();
}
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
System.out.println("Name is"+name);
System.out.println("purpose is"+purpose);
}
}
In your ExpandableListAdapter class under getChildView it looks like you are telling the app to loop through all of the hints and it finishes on "Company Name"
maybe change
while(i<6)
{
switch (i)
{
case 1:editetext.setHint("Contact Person");
break;
case 2:editetext.setHint("Contact Person Photo");
break;
case 3:editetext.setHint("Designation/Job");
break;
case 4:editetext.setHint("CEO Name");
break;
case 5:editetext.setHint("Company Name");
break;
}
i++;
}
to
switch (childPosition)
{
case 1:editetext.setHint("Contact Person");
break;
case 2:editetext.setHint("Contact Person Photo");
break;
case 3:editetext.setHint("Designation/Job");
break;
case 4:editetext.setHint("CEO Name");
break;
case 5:editetext.setHint("Company Name");
break;
}
actual i have better solution why dont u simply use xml code to put hint in your edit text.It will be easy for u and it is not mendetory to code it programmatically.hope u get it
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 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 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);