代码正常工作,没有任何错误或警告,但可扩展列表未显示,这是代码

public class MainActivity extends Activity implements  ExpandableListView.OnChildClickListener{
    ArrayList<List> 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<List>();
            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
  <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:context=".MainActivity" >

        <ExpandableListView
             android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/list"
             >

        </ExpandableListView>

    </RelativeLayout>

可扩展 ListView 适配器实现
public class ExpandableListAdapterImpl extends BaseExpandableListAdapter
{
    LayoutInflater inflatter;
    Context context;
    ArrayList groupitems;
    ArrayList<List> childitems;

    ExpandableListAdapterImpl(LayoutInflater inflater,Activity activity,ArrayList groupitems,ArrayList<List> 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
<CheckedTextView xmlns:tools="http://schemas.android.com/tools"
    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" />

子行.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:background="@drawable/playersdetails"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="" />

</LinearLayout>

我想知道我在哪里失踪...提前致谢

最佳答案

是的…… Pragnani 我做到了…………

修改 ExpandableListAdapterImpl 中的代码:
child 数

 @Override
public int getChildrenCount(int groupPosition) {
    return ((ArrayList<String>) childitems.get(groupPosition)).size();
}

对于组计数:
@Override
public int getGroupCount() {
    // TODO Auto-generated method stub
    return groupitems.size();
}

那肯定会奏效......

关于android - 未显示可扩展 ListView ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14373811/

10-12 02:14