ExpandableList View not using ExpandableListActivity
Because I don't have enough reputation points to comment on previous questions yet, I had to create a new one. I needed an expandable list view that didn't take up the entire activity so I used this example to do it without ExpandableListActivity:
ExpandableList View don't expand
My slightly modified code:
public class Main extends Activity {
ExpandableListView lv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.expandable_list);
lv = (ExpandableListView) this.findViewById(R.id.expandableListView1);
MyExpandableListAdapter expandableAdapter = new MyExpandableListAdapter();
lv.setAdapter(expandableAdapter);
}
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.MATCH_PARENT, 64);
TextView textView = new TextView(Main.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) {
TextView textView = getGenericView();
textView.setText(getGroup(groupPosition).toString());
return textView;
}
public boolean hasStableIds() {
return true;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
开发者_JAVA百科return true;
}
}
}
This works fine, but to make it more robust, I wanted to separate the textViews created in code to xml files (This is a good idea right?) This is where I run into some FC issues.
group_row.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/row_name"
android:paddingLeft="50px"
android:textSize="20px"
android:textStyle="normal"
android:layout_width="320px"
android:layout_height="wrap_content"/>
</LinearLayout>
And I changed this in the java file above:
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
TextView parentRow = (TextView) findViewById(R.id.row_name);
parentRow.setText(getGroup(groupPosition).toString());
return parentRow;
}
I also tried removing the LinearLayout wrapper but that didn't fix anything. Can anyone tell me why my xml view isn't working? Thanks.
EDIT: Thanks Flo, new code that is working using the tutorial:
group_row.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/group_name"
android:paddingLeft="50dp"
android:textSize="30sp"
android:textStyle="normal"
android:layout_height="wrap_content"/>
Main:
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
View parentView = convertView;
if (parentView == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
parentView = vi.inflate(R.layout.group_row, null);
}
TextView parentText = (TextView) parentView.findViewById(R.id.group_name);
if (parentText != null) {
parentText.setText(getGroup(groupPosition).toString());
}
return parentText;
}
Your implementation of the getGroupView() method is wrong. At the moment your searching in the activity's layout for a element with the element R.id.row_name
which doesn't exist. I guess the line parentRow.setText(getGroup(groupPosition).toString());
throws an exception as parentRow
is null.
The correct implementation of the method with a custom row layout from a xml file is to inflate this layout. Check out this tutorial. It's for normal ListViews and adapter but the concept of inflating the row layouts it the same.
精彩评论