通过ListActivity使用ListView布局方法
2014-03-05 12:55
337 查看
先简单的介绍一下ListActivity
ListActivity是一个专门显示ListView的Activity类,它内置了ListView对象,只要我们设置了数据源,就会自动地显示出来。ListActivity和普通的Activity没有太大的差别,不同就是对显示ListView做了许多优化,方面显示而已。
我们知道
列表的显示需要三个元素:
1.ListVeiw 用来展示列表的View。
2.适配器 用来把数据映射到ListView上的中介。
3.数据 具体的将被映射的字符串,图片,或者基本组件。
根据列表的适配器类型,列表分为三种,ArrayAdapter,SimpleAdapter和SimpleCursorAdapter
simpleAdapter的扩展性最好,可以定义各种各样的布局出来,可以放上ImageView(图片),还可以放上Button(按钮),CheckBox(复选框)等等。
下面的代码都直接继承了ListActivity,采用simpleAdapter
先上两个图:
![](http://hi.csdn.net/attachment/201110/30/0_1319963136C3Yz.gif)
![](http://hi.csdn.net/attachment/201110/30/0_1319963167N8Te.gif)
上代码
MoreActivity.java
______________________________________
[java] view plaincopy
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
______________________________________________
[html] view plaincopy
<?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>
参考文章:
/article/5166743.html
ListActivity是一个专门显示ListView的Activity类,它内置了ListView对象,只要我们设置了数据源,就会自动地显示出来。ListActivity和普通的Activity没有太大的差别,不同就是对显示ListView做了许多优化,方面显示而已。
我们知道
列表的显示需要三个元素:
1.ListVeiw 用来展示列表的View。
2.适配器 用来把数据映射到ListView上的中介。
3.数据 具体的将被映射的字符串,图片,或者基本组件。
根据列表的适配器类型,列表分为三种,ArrayAdapter,SimpleAdapter和SimpleCursorAdapter
simpleAdapter的扩展性最好,可以定义各种各样的布局出来,可以放上ImageView(图片),还可以放上Button(按钮),CheckBox(复选框)等等。
下面的代码都直接继承了ListActivity,采用simpleAdapter
先上两个图:
![](http://hi.csdn.net/attachment/201110/30/0_1319963136C3Yz.gif)
![](http://hi.csdn.net/attachment/201110/30/0_1319963167N8Te.gif)
上代码
MoreActivity.java
______________________________________
[java] view plaincopy
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
______________________________________________
[html] view plaincopy
<?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>
参考文章:
/article/5166743.html
相关文章推荐
- 通过ListActivity使用ListView布局方法
- android ListView布局之一(继承listActivity、使用arrayAdapter)
- android ListView布局之三(使用自定义的Adapter绑定数据,通过contextView.setTag绑定数据)有按钮的ListView
- android ListView布局之一(继承listActivity、使用arrayAdapter)
- Activity在GridView和ListView之间切换使用一个布局实现的方法
- ListView 使用多布局的方法和简单的原理分析
- android ListView布局之一(继承listActivity、使用arrayAdapter)
- ListView使用详解,listActivity使用方法
- ListView使用详解,listActivity使用方法
- android ListView布局之三(使用自定义的Adapter绑定数据,通过contextView.setTag绑定数据)有按钮的ListView
- android studio开发教程 ListActivity的2种使用方法:系统默认布局和自己定义布局
- 屏幕旋转之后,ListView表格表头位置错乱 与onConfigurationChanged方法的使用【安卓布局】
- android ListView布局之三(使用自定义的Adapter绑定数据,通过contextView.setTag绑定数据)有按钮的ListView
- ListView异步加载图片是非常实用的方法,凡是是要通过网络获取图片资源一般使用这种方法比较好,用户体验好,下面就说实现方法,先贴上主方法的代码:
- listview,gridview里面通过一个item控制所有item的使用方法
- android ListView使用的三种方法汇总(通过继承BaseAdapter,使用SimpleAdapter和SimpleCursorAdapter)
- listView使用simpleAdapter通过notifyDataSetChanged()方法刷新数据问题
- Android系列之网络(一)----使用HttpClient发送HTTP请求(通过get方法获取数据)
- WinForm 之Control.Invoke 和Control.BeginInvoke 方法的使用 Control 不能在创建它的 Thread 之外被调用。但可以通过 invoke 来保证 C