代码可以正常运行,没有任何错误或警告,但可扩展列表未显示,这是代码
public class MainActivity extends Activity implements ExpandableListView.OnChildClickListener{
ArrayList childitems;
ArrayList groupitems;
ExpandableListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
list=(ExpandableListView)findViewById(R.id.list);
list.setOnChildClickListener(this);
addGroupItems();
addChildItems();
LayoutInflater inflater=(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ExpandableListAdapterImpl adapter=new ExpandableListAdapterImpl(inflater, this, groupitems, childitems);
list.setAdapter(adapter);
super.onCreate(savedInstanceState);
}
private void addChildItems() {
ArrayList childlist;
try {
childitems=new ArrayList();
childlist=new ArrayList();
childlist.add("Pragnani");
childlist.add("Kinnera");
childlist.add("Good");
childitems.add(childlist);
childlist=new ArrayList();
childlist.add("Bannu");
childlist.add("Kinnera");
childlist.add("Good");
childitems.add(childlist);
} catch (Exception e) {
e.printStackTrace();
}
}
private void addGroupItems() {
try {
groupitems=new ArrayList();
groupitems.add("My Details 1");
groupitems.add("My Details 2");
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
return false;
}
}
这是activity_main.xml
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/list"
>
可扩展列表视图适配器实现
public class ExpandableListAdapterImpl extends BaseExpandableListAdapter
{
LayoutInflater inflatter;
Context context;
ArrayList groupitems;
ArrayList childitems;
ExpandableListAdapterImpl(LayoutInflater inflater,Activity activity,ArrayList groupitems,ArrayList childitems)
{
super();
this.inflatter=inflater;
context=activity;
this.groupitems=groupitems;
this.childitems=childitems;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
ArrayList childtemp=(ArrayList) childitems.get(groupPosition);
try {
if(convertView==null)
{
convertView=inflatter.inflate(R.layout.childrow, null);
}
Button button=(Button)convertView.findViewById(R.id.button1);
button.setText(childtemp.get(groupPosition).toString());
} catch (Exception e) {
e.printStackTrace();
}
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return 0;
}
@Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return null;
}
@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return 0;
}
@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
try {
if(convertView==null)
{
convertView=inflatter.inflate(R.layout.grouprow, null);
}
CheckedTextView view=(CheckedTextView) convertView.findViewById(R.id.textView1);
view.setText(groupitems.get(groupPosition).toString());
view.setChecked(isExpanded);
} catch (Exception e) {
e.printStackTrace();
}
return convertView;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return false;
}
}
这是grouprow.xml
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_marginLeft="5dp"
android:drawableRight="@drawable/plusminus"
android:gravity="center_vertical"
android:padding="10dp"
android:text="@string/hello_world"
android:textColor="#FFFFFF"
android:textSelectHandleLeft="@string/hello_world"
android:textSize="14sp"
android:textStyle="bold" />
childrow.xml
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
android:id="@+id/button1"
android:background="@drawable/playersdetails"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
我想知道我想念的地方…在此先感谢