您的位置:首页 > 移动开发 > Android开发

android ExpandableListView ExpandableListAdapter使用

2011-11-22 10:04 399 查看
目前大家都在使用ExpandableListView完成一些特效,目前大多数程序在显示都没有问题,但是在处理数据的时候出现问题,本人使用自定义的显示模式及数据,单介绍这个空间的使用。

首先看主xml文件:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout

xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation = "vertical"

android:layout_width="match_parent"

android:layout_height="match_parent">

<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="expandlistview content test" />

<ExpandableListView android:layout_height="match_parent" android:layout_width="match_parent" android:id="@+id/uselistExpLV" android:drawSelectorOnTop = "false" />//ExpandableListView 控件

<TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="No Data" />//ExpandableListView 没有数据时显示

</LinearLayout>

//用于显示二级选项的xml,即child显示需要用的xml布局

<?xml version="1.0" encoding="utf-8"?>

<TextView xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width = "fill_parent"

android:layout_height = "fill_parent"

android:id ="@+id/uselistChild"

android:text = "No Data"

android:textSize = "15sp"

android:paddingLeft = "45px"

android:paddingTop = "10px"

android:paddingRight = "30px"

android:paddingBottom = "10px"

/>

这里需要注意,我并没有使用布局文件,如果使用了布局文件,那么在程序中需要注意获取对象的时候,确认是否是布局对象,还是显示对象

//用于显示分组选项的xml,即group显示需要用的xml布局

<?xml version="1.0" encoding="utf-8"?>

<TextView xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width = "fill_parent"

android:layout_height = "fill_parent"

android:id ="@+id/uselistGroup"

android:text = "No Data"

android:textSize = "25sp"

android:paddingLeft = "45px"

android:paddingTop = "10px"

android:paddingRight = "30px"

android:paddingBottom = "10px"

/>

最后是主程序java文件:

package com.eoeAndroid;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.widget.ExpandableListView;

import android.widget.SimpleExpandableListAdapter;

import android.widget.TextView;

public class UseListControl extends Activity {

private ExpandableListView expListView;

public void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.uselistmain);

expListView = (ExpandableListView)findViewById(R.id.uselistExpLV);

List<Map<String,String>> groups = new ArrayList<Map<String,String>>();

Map<String,String> group1 = new HashMap<String,String>();

group1.put("group", "music");

Map<String,String> group2 = new HashMap<String,String>();

group2.put("group", "lrc");

groups.add(group1);

groups.add(group2);

List<Map<String,String>> child1 = new ArrayList<Map<String,String>>();

Map<String,String> childdata1 = new HashMap<String,String>();

childdata1.put("child", "青花瓷");

Map<String,String> childdata2 = new HashMap<String,String>();

childdata2.put("child", "东风破");

child1.add(childdata1);

child1.add(childdata2);

List<Map<String,String>> child2 = new ArrayList<Map<String,String>>();

Map<String,String> childdata3 = new HashMap<String,String>();

childdata3.put("child", "青花瓷.lrc");

Map<String,String> childdata4 = new HashMap<String,String>();

childdata4.put("child", "东风破.lrc");

child2.add(childdata3);

child2.add(childdata4);

List<List<Map<String,String>>> childs = new ArrayList<List<Map<String,String>>>();

childs.add(child1);

childs.add(child2);

SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(UseListControl.this, groups, R.layout.uselistgroups,new String[]{"group"} , new int[]{R.id.uselistGroup}, childs,R.layout.uselistchilds, new String[]{"child"},new int[]{R.id.uselistChild} );

expListView.setAdapter(adapter);

expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {

@Override

public boolean onChildClick(ExpandableListView parent, View v,

int groupPosition, int childPosition, long id) {

// TODO Auto-generated method stub

Log.v("UseListControl", "onChildClick"+((TextView)parent.getChildAt(childPosition)).getText().toString());

Bundle bundle = new Bundle();

bundle.putString("useforitem",((TextView)v).getText().toString());//给bundle 写入数据

Intent mIntent = new Intent();

mIntent.putExtras(bundle);

UseListControl.this.setResult(RESULT_OK, mIntent);

UseListControl.this.finish();

return true;

}

});

}

}

((TextView)v).getText().toString());这句使用的时候需要注意,v的获取,需要确定这个是布局对象还是显示对象,本人xml并没有使用布局,所以获取的是textview对象,但是如果使用了布局,则会获取到布局文件,所以需要根据布局对象findviewbyid获取对应的元素。

这种方式则可以确定使用的根据自己定义格式,使用布局将这些元素find查询到。

其余解释同别的一样,不做详细分析
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐