历时若干天的框架搭建完成。开始转入内部学习:
ListView之前一直以为他的作用就和布局类似,不过其中只能存放一些类似于char或者int 的纯文本。今天又长姿势了。
ListView是一个列表框架,一般使用的时候需要和适配器相搭配。而常见的适配器有三种:ArrayAdapter,SimpleAdapter和SimpleCursorAdapter。
当然作为小白的我来说这些东西就只能是跟着大神的脚步慢慢爬,看了一下午的listView,大致的用法看明白了。
ArrayAdapter是Androidstudio最基本的,大神告诉我这是安卓自带的(莫非其他两种不是??)。他只能显示纯文本,显示一页的列表。
ArrayAdapter的构造需要三个参数,依次为this,布局文件(注意这里的布局文件描述的是列表的每一行的布局,android.R.layout.simple_list_item_1是系统定义好的布局文件只显示一行文字,数据源(一个List集合)。同时用setAdapter()完成适配的最后工作。
(借用一下百度得到的解释)
SimpleAdapter是档次比较高端的,可以映射imagmentView,TextView,甚至Button;撒一段代码:
public
class
MyListView3
extends
ListActivity {
// private List<String> data = new ArrayList<String>();
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
SimpleAdapter adapter =
new
SimpleAdapter(
this
,getData(),R.layout.vlist,
new
String[]{
"title"
,
"info"
,
"img"
},
new
int
[]{R.id.title,R.id.info,R.id.img});
setListAdapter(adapter);
}
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(
"title"
,
"G1"
);
map.put(
"info"
,
"google 1"
);
map.put(
"img"
, R.drawable.i1);
list.add(map);
map =
new
HashMap<String, Object>();
map.put(
"title"
,
"G2"
);
map.put(
"info"
,
"google 2"
);
map.put(
"img"
, R.drawable.i2);
list.add(map);
map =
new
HashMap<String, Object>();
map.put(
"title"
,
"G3"
);
map.put(
"info"
,
"google 3"
);
map.put(
"img"
, R.drawable.i3);
list.add(map);
return
list;
}
}
<?
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="#FFFFFFFF"
android:textSize="22px" />
<
TextView
android:id="@+id/info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFFFF"
android:textSize="13px" />
</
LinearLayout
>
</
LinearLayout
>
我不是代码的创造者,我只是代码的搬运工。。