设计一个ExpandableListView实现两级列表伸缩,代码报错找不到原因,求大神看看

澄鱼 发布于 2015/06/27 16:18
阅读 128
收藏 0
 
package com.example.expandlistviewadapter;
import android.os.Bundle;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.widget.ExpandableListView;

public class MainActivity extends Activity {

	private String[] childate;
	private String[][] groupdate;
	private expendablelistviewadapter	exdapter;
	private ExpandableListView list;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		LayoutInflater inflater = LayoutInflater.from(this);
		list=(ExpandableListView) findViewById(R.id.expandableListView1);
		//初始化数据
	String	groupdate[]={"LOL","剑灵","天涯明月刀"};
	String  childate[][]={ {"寒冰射手","德玛西亚","赵信","卡特琳娜","阿狸","曙光女神"},
							{"秦义绝","天下四杰","武神","鬼剑"},
							{"天香","太白","真武","丐帮"}
						};
		exdapter=new expendablelistviewadapter(groupdate, childate, null);
		list.setAdapter(exdapter);
		
	}

}



package com.example.expandlistviewadapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class expendablelistviewadapter extends BaseExpandableListAdapter {
	private  String[]  groupdate;
	private  String[][] childate;
	private  Context  m_context;
  public expendablelistviewadapter(String[]  groupdate,String[][] childate,Context  m_context) {
	  	this.groupdate=groupdate;
	  	this.childate=childate;
	  	this.m_context=m_context;
  
  }
	
	@Override
	public Object getChild(int grouposition, int childposition) {
		return childate[grouposition][childposition];
	}

	@Override
	public long getChildId(int grouposition, int childposition) {
		// TODO Auto-generated method stub
		return childposition;
	}

	@Override
	public View getChildView(int grouposition, int childposition, boolean isLastChild, View convertView,
			ViewGroup parent) {
		LinearLayout ll=new LinearLayout(m_context); 
    
        TextView textView=new TextView(m_context);
        textView.setText(childate[grouposition][childposition]); 
        ll.addView(textView); 
        return ll; 
	}

	@Override
	public int getChildrenCount(int grouposition) {
		// TODO Auto-generated method stub
		return childate[grouposition].length;
	}

	@Override
	public Object getGroup(int  grouposition) {
		// TODO Auto-generated method stub
		return groupdate[grouposition];
	}

	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		return groupdate.length;
	}

	@Override
	public long getGroupId(int grouposition) {
		// TODO Auto-generated method stub
		return grouposition;
	}

	@Override
	public View getGroupView(int grouposition, boolean isExpanded, View convertView, ViewGroup parent) {
		LinearLayout llg=new LinearLayout(m_context); 
   
        TextView textView=new TextView(m_context);
        textView.setText(groupdate[grouposition]); 
        llg.addView(textView);
		
		return llg;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean isChildSelectable(int grouposition, int childposition) {
		// TODO Auto-generated method stub
		return true;
	}

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <ExpandableListView
        android:id="@+id/expandableListView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ExpandableListView>
   
</LinearLayout>



上面第一个是主程序代码,第二个是ExpandableListView自定义的适配器,第三个是布局文件。

本人初学者 很多不懂请大神帮忙一下



加载中
返回顶部
顶部