菜鸟学习android之ListView
注:本文只是一个学习笔记 用以记录自己学到哪了
①先上个简单点的ListView
public class ListViewActivity extends ListActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ArrayAdapter<String> adapter = new ArrayAdapter<String> (this,android.R.layout.simple_list_item_1,getData()); this.setListAdapter(adapter); } private String[] getData() { String[] data = {"中国","美国","俄罗斯","澳大利亚","加拿大"}; return data; } }下面看一下效果图:
publicArrayAdapter(Contextcontext, int textViewResourceId, T[] objects)
ArrayAdapter的构造函数的三个参数依次是this ,布局文件,数据源(在这里是String[])另外说一下那个布局文件:描述的是列表的每一行的布局,android.R.layout.simple_list_item_1是系统定义好的布局文件只显示一行文字
②下面利用SimpleAdapter 来说一下Listview 效仿了QQ的布局 先看一下效果:
本程序有ListViewActivity.java 和 my_list_item.xml主要文件组成
my_list_item.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/Image"
android:layout_width="50dip"
android:layout_height="50dip"
android:layout_margin="5dip"
android:src="@drawable/j1"
android:scaleType="centerInside"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/nickname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffff0000"
android:textSize="22sp"
android:textStyle="bold"
android:singleLine="true"/>
<TextView
android:id="@+id/sign"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffffff"
android:textSize="20sp"
android:singleLine="true"/>
</LinearLayout>
</LinearLayout>ListViewActivity.java 文件如下:
下面是onCreate()方法
List<Map<String, Object>> listData = this.getData();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] from = new String[]{"nickname","sign","image"};
int[] to = new int[]{R.id.nickname,R.id.sign,R.id.Image};
//为ListView设置适配器
SimpleAdapter adapter = new SimpleAdapter(this,listData,
R.layout.my_list_item, from,to);
this.setListAdapter(adapter);
}
PS:from,to要对应
获取数据即listview中文字,图片
private List<Map<String, Object>> getData()
{
List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
Map<String,Object> map = new HashMap<String,Object>();
map.put("nickname", "岁月神偷");
map.put("sign", "一切都会变好的!");
map.put("image", R.drawable.j1);
list.add(map);
map = new HashMap<String,Object>();
map.put("nickname", "被水淹死的鱼");
map.put("sign", "等待春天的到来");
map.put("image", R.drawable.j2);
list.add(map);
map = new HashMap<String,Object>();
map.put("nickname", "我相信");
map.put("sign", "只希望能手牵手在太阳下散步”....“最绝望的念想、最悲恸的守望”");
map.put("image", R.drawable.j3);
list.add(map);
map = new HashMap<String,Object>();
map.put("nickname", "龟龟");
map.put("sign", "当一切 ...已成惘然...");
map.put("image", R.drawable.j4);
list.add(map);
return list;
}
单击事件的处理
@Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
this.displayMessage(position);
super.onListItemClick(l, v, position, id);
}
public void displayMessage(int position)
{
String nickname = (String)listData.get(position).get("nickname");
String sign = (String)listData.get(position).get("sign");
Toast.makeText(this, nickname+": "+sign,Toast.LENGTH_SHORT).show();
}
这次关于学习ListVIew的笔记就这么多,以后随着深入Listview会扩充这次笔记的。
这次关于学习ListVIew的笔记就这么多,以后随着深入Listview会扩充这次笔记的。