通过ListActivity使用ListView布局方法

由于本人第一次使用该博客,发表文章使用的不是很熟练,所以文章格式及写作水平可能不是很好,希望各位见谅

先简单的介绍一下ListActivity

ListActivity是一个专门显示ListView的Activity类,它内置了ListView对象,只要我们设置了数据源,就会自动地显示出来。ListActivity和普通的Activity没有太大的差别,不同就是对显示ListView做了许多优化,方面显示而已。

我们知道

列表的显示需要三个元素:

1.ListVeiw 用来展示列表的View。

2.适配器 用来把数据映射到ListView上的中介。

3.数据    具体的将被映射的字符串,图片,或者基本组件。

根据列表的适配器类型,列表分为三种,ArrayAdapter,SimpleAdapter和SimpleCursorAdapter

simpleAdapter的扩展性最好,可以定义各种各样的布局出来,可以放上ImageView(图片),还可以放上Button(按钮),CheckBox(复选框)等等。

下面的代码都直接继承了ListActivity,采用simpleAdapter

先上两个图:



上代码

MoreActivity.java
______________________________________

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;
 
public class MoreActivity extends ListActivity {
 
private String discount_info;
private String recommend_shop;
private String about_us;
 
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
 
SimpleAdapter adapter = new SimpleAdapter(this,getData(),R.layout.more,
new String[]{"title","info","img"},
new int[]{R.id.title,R.id.info,R.id.img});
setListAdapter(adapter);
 
}
 
private List<HashMap> getData() {
List<HashMap> list = new ArrayList<HashMap>();
 
Map map = new HashMap();
 
discount_info=this.getString(R.string.discount_info);//相应字符串在String.xml中设置
recommend_shop=this.getString(R.string.recommend_shop);
about_us=this.getString(R.string.about_us);
 
map.put("title", discount_info);
map.put("info", "android 1");
map.put("img", R.drawable.i1);
list.add(map);
 
map = new HashMap();
map.put("title", recommend_shop);
map.put("info", "android 2");
map.put("img", R.drawable.i2);
list.add(map);
 
map = new HashMap();
map.put("title", about_us);
map.put("info", "android 3");
map.put("img", R.drawable.i3);
list.add(map);
 
return list;
}
 
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
if(0==id)
{
showInfo();//点击第一项执行函数,其它id可以自己设置执行函数
}
 
}
 
//定义启动Dialog
 
public void showInfo(){
new AlertDialog.Builder(this)
.setTitle("title")
.setMessage("bla..bla")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.show();
 
}
}

more.xml

______________________________________________

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
 android:orientation=”horizontal”
 android:layout_width=”fill_parent”
 android:layout_height=”fill_parent”>
 <ImageView
  android:id=”@+id/img”
  android:layout_width=”wrap_content”
  android:layout_height=”wrap_content”
  android:layout_margin=”5px”/>
 
 <LinearLayout
  android:orientation=”vertical”
  android:layout_width=”wrap_content”
  android:layout_height=”wrap_content”>
 
  <TextView
   android:id=”@+id/title”
   android:layout_width=”wrap_content”
   android:layout_height=”wrap_content”
   android:textColor=”#000000″
   android:textSize=”22px” />
  <TextView
   android:id=”@+id/info”
   android:layout_width=”wrap_content”
   android:layout_height=”wrap_content”
   android:textColor=”#000000″
   android:textSize=”13px” />
 
 </LinearLayout>
</LinearLayout>

参考文章:

http://www.cnblogs.com/allin/archive/2010/05/11/1732200.html


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值