现在的位置: 首页 > 综合 > 正文

Android开发之ListView详解(带按钮布局)【免费提供源码下载】

2012年09月12日 ⁄ 综合 ⁄ 共 8872字 ⁄ 字号 评论关闭

Android开发之ListView详解(带按钮布局)

        在android开发中,ListView是比较常用到的组件之一,它可以以列表的形式来显示内容,并且能够根据数据的长度自适应显示。我写了个小例子来描述ListView的使用方法,另发现网上的资料中,都没有按钮,我添加了按钮的布局:


我们在界面上自定义了几个按钮,分别要演示的内容就是按钮上面写的,我想这个都会把,不会的可以留言,我给发源码。


其中以ArrayAdapter最为简单,只能展示一行字。SimpleAdapter有最好的扩充性,可以自定义出各种效果。SimpleCursorAdapter最常用,它可以认为是SimpleAdapter对数据库的简单结合,可以方面的把数据库的内容以列表的形式展示出来(今天不做应用)。

1、我们从最简单的ListView开始:

点击第一个按钮会进入如下图的界面:

下面我们来看代码:
(代码解释注释已经写好了)

/**
 * @author dbkmeteor
 *
 */
public class ActivityList1 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("ArrayAdapter的练习");
//新建一个ListView,也就是新建了一个列表
ListView listView=new ListView(this);
/*
* 让listv和Adapter进行绑定
* 第一个参数是context
* 第二歌参数是在R文件定义的Layout,这里用系统的
* 第三个参数是一个数组,且里面的每一项的类型 没有限制
*/
listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, getData()));
//将listview直接显示
setContentView(listView);
}

private List<String> getData(){
List<String> data=new ArrayList<String>();
data.add("android移动开发");
data.add("JAVA的C/S开发");
data.add("JAVA的B/S开发");
data.add("PHP开发");
return data;
}
}


2、SimpleAdapter的练习,也就是点击第二个按钮,会看到如下的界面


simpleAdapter的扩展性最好,可以定义各种各样的布局出来,可以放上ImageView(图片),还可以放上Button(按钮),CheckBox(复选框)等等。下面的代码都直接继承了ListActivity,ListActivity和普通的Activity没有太大的差别,不同就是对显示ListView做了许多优化,方面显示而已
我们看下主要代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ListView listview = new ListView(this);
//生成一个SimpleAdapter实例
SimpleAdapter adapter=new SimpleAdapter(this, PrepareData(), R.layout.sample_list2
, new String[]{"姓名","性别"}
, new int[]{R.id.mview1,R.id.mview2});
listview.setAdapter(adapter);
setContentView(listview);

}

private  ArrayList<Map<String, Object>> PrepareData(){
ArrayList<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
Map<String, Object> item;
item=new HashMap<String, Object>();
item.put("姓名", "张三");
item.put("性别", "男");
data.add(item);
item=new HashMap<String, Object>();
item.put("姓名", "李四");
item.put("性别", "男");
data.add(item);
item=new HashMap<String, Object>();
item.put("姓名", "王五");
item.put("性别", "女");
data.add(item);
item=new HashMap<String, Object>();
item.put("姓名", "王五");
item.put("性别", "女");
data.add(item);
item=new HashMap<String, Object>();
item.put("姓名", "欧海涛");
item.put("性别", "男");
data.add(item);
return data;
}


3、添加一个返回按钮后,该在么布局呢?
看第三个按钮点击进去的图片:


不知道大家有没有注意到,我写的是绝对布局,为什么呢?
我们先来看下代码:
private Button btnm1=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
setContentView(layout);
//用LayoutInflater可以把一个XML布局文件变成一个view实例
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ListView listview = new ListView(this);
SimpleAdapter adapter=new SimpleAdapter(this, PrepareData(), R.layout.sample_list6
, new String[]{"姓名","性别"}
, new int[]{R.id.mview1,R.id.mview2});
listview.setAdapter(adapter);
//添加按钮的布局文件
RelativeLayout layoutRight=(RelativeLayout) inflater.inflate(R.layout.sample_button, null);
RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
//定义布局文件的宽和高
layout.addView(listview, 1000, 100);
layout.addView(layoutRight, 180, 330);
setContentView(layout, params);
btnm1=(Button)findViewById(R.id.btnm1);
btnm1.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
Intent intent=new Intent();
intent.setClass(ActivityList6.this, ActivityMain.class);
startActivity(intent);
}
});
}

private  ArrayList<Map<String, Object>> PrepareData(){
ArrayList<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
Map<String, Object> item;
item=new HashMap<String, Object>();
item.put("姓名", "张三");
item.put("性别", "男");
data.add(item);
item=new HashMap<String, Object>();
item.put("姓名", "李四");
item.put("性别", "男");
data.add(item);
item=new HashMap<String, Object>();
item.put("姓名", "王五");
item.put("性别", "女");
data.add(item);
item=new HashMap<String, Object>();
item.put("姓名", "王五");
item.put("性别", "女");
data.add(item);
item=new HashMap<String, Object>();
item.put("姓名", "欧海涛");
item.put("性别", "男");
data.add(item);
return data;
}
在代码中, layout.addView(listview, 1000, 100);
layout.addView(layoutRight, 180, 330);
这两句分别定义了两个布局文件在当前窗口的宽和高,
不知道大家有没有注意,代码中明明有5条数据,在界面上却只显示出3条来?为什么呢?
那么你试试按键盘上的下键试试?怎么样?
这是因为在layout.addView(listview, 1000, 100);这句代码中定义的高度不够,但是当你把高度改大的时候,在界面上的返回按钮就会不见了,因为窗口就有那么大,你把第一个布局设大了,那么第二个就会显示不完全啦!
这中布局也许在有些需求中会用到,所以就写出来了。

4、下面看第四个按钮的图


这个在按钮上已经说明了,是相对布局的,先看代码,:
private Button btnm1=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RelativeLayout layout=new RelativeLayout(this);
setContentView(layout);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ListView listview = new ListView(this);
SimpleAdapter adapter=new SimpleAdapter(this, PrepareData(), R.layout.sample_list5
, new String[]{"姓名","性别"}
, new int[]{R.id.mview1,R.id.mview2});
listview.setAdapter(adapter);
RelativeLayout layoutRight=(RelativeLayout) inflater.inflate(R.layout.sample_button, null);
RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
layout.addView(listview, params);
layout.addView(layoutRight, params);
setContentView(layout, params);
btnm1=(Button)findViewById(R.id.btnm1);
btnm1.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
Intent intent=new Intent();
intent.setClass(ActivityList5.this, ActivityMain.class);
startActivity(intent);
}
});
}

private  ArrayList<Map<String, Object>> PrepareData(){
ArrayList<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
Map<String, Object> item;
item=new HashMap<String, Object>();
item.put("姓名", "张三");
item.put("性别", "男");
data.add(item);
item=new HashMap<String, Object>();
item.put("姓名", "李四");
item.put("性别", "男");
data.add(item);
item=new HashMap<String, Object>();
item.put("姓名", "王五");
item.put("性别", "女");
data.add(item);
item=new HashMap<String, Object>();
item.put("姓名", "王五");
item.put("性别", "女");
data.add(item);
item=new HashMap<String, Object>();
item.put("姓名", "欧海涛");
item.put("性别", "男");
data.add(item);
return data;
}
和上面的没什么大的区别,就是用了RelativeLayout来布局,也就是相对布局。个人觉得这种应用可能会长用到。

5、第五个按钮是单击事件
也就是我们要单击列表中的数据,会触发单击事件来做一些事,如下图


代码如下;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("单击事件的练习");
ListView listview = new ListView(this);
SimpleAdapter adapter=new SimpleAdapter(this, PrepareData(), R.layout.sample_list3
, new String[]{"姓名","性别"}
, new int[]{R.id.view1,R.id.view2});
listview.setAdapter(adapter);
setContentView(listview);
//定义单击事件
listview.setOnItemClickListener(listener);

}
//ListView中的 数据
private  ArrayList<Map<String, Object>> PrepareData(){
ArrayList<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
Map<String, Object> item;
item=new HashMap<String, Object>();
item.put("姓名", "张三");
item.put("性别", "男");
data.add(item);
item=new HashMap<String, Object>();
item.put("姓名", "李四");
item.put("性别", "男");
data.add(item);
item=new HashMap<String, Object>();
item.put("姓名", "王五");
item.put("性别", "女");
data.add(item);
return data;
}
//定义单击事件
OnItemClickListener listener=new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
setTitle(parent.getItemAtPosition(position).toString());
showInfo();
}
};
//ListView的弹出提示
public void showInfo(){
new AlertDialog.Builder(this)
.setTitle("我的ListView")
.setMessage("介绍。。。")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {

}
})
.show();
}
我加了一个弹出窗口 进去,也把弹出窗口和listv结合起来了,相信不难理解吧。

6、选择事件如下图


代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("选择事件的练习");
ListView  listView=new ListView(this);
SimpleAdapter adapter=new SimpleAdapter(this, getData()
, R.layout.sample_list4
, new String[]{"view1","view2","img"}
, new int[]{R.id.view1,R.id.view2,R.id.img});
listView.setAdapter(adapter);
setContentView(listView);
listView.setOnItemSelectedListener(listener);
}

private ArrayList<Map<String, Object>> getData(){
ArrayList<Map<String, Object>> data=new ArrayList<Map<String,Object>>();
Map<String, Object> item;
item=new HashMap<String, Object>();
item.put("view1", "张三");
item.put("view2", "男");
item.put("img", R.drawable.i1);
data.add(item);
item=new HashMap<String, Object>();
item.put("view1", "李四");
item.put("view2", "男");
item.put("img", R.drawable.i2);
data.add(item);
item=new HashMap<String, Object>();
item.put("view1", "王兰");
item.put("view2", "女");
item.put("img", R.drawable.i3);
data.add(item);
return data;
}
OnItemSelectedListener listener=new OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(ActivityList4.this, parent.getItemAtPosition(position).toString(), Toast.LENGTH_LONG).show();
}

@Override
public void onNothingSelected(AdapterView<?> parent) {
setTitle("");
}
};
这个在模拟起测试的时候需要按键盘上的上下键来选择没条数据,就会在界面上有相应的提示。
大家可以试试改动下第三第四个按钮了面的东西,比如多写几条数据,试试,会有意想不到的效果哦,分享一下你的成果,大家一起讨论,学的才有乐趣嘛。
其实也不怎么的难,只要你肯下功夫,就能自己研究出来。

抱歉!评论已关闭.