您的位置:首页 > 移动开发 > Android开发

Android中的ListView初步接触(一)

2015-12-12 11:16 591 查看
Android中ListView是我们可以让我们以列表视图的方式来呈现数据和信息,是经常被使用到的一种控件。ListView的使用又可以分为三大块:ListView控件,Adapter适配器以及要加载呈现的数据。简单的使用ListView可以通过以下步骤。

在xml布局文件中定义一个ListView控件。

在Java代码中获取该ListView控件,并为其创建一个Adapter对象(可以是ArrayAdapter,SimpleAdapter等系统定义好的Adapter也可以是自定义的Adapter)。

将要加载的数据放入Adapter中,并将Adapter绑定到已获取的ListView上。

xml布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<ListView
android:id="@+id/lv_main"
android:layout_width="match_parent"
android:layout_height="match_parent">

</ListView>
</LinearLayout>


Java代码

public class MainActivity extends Activity {
private ListView listView;
private ArrayAdapter arrayAdapter;
private List<String> dataList;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i("MainActivity", "onCreate");
setContentView(R.layout.activity_main);

//获取布局文件中的控件
listView = (ListView) findViewById(R.id.lv_main);

dataList = new ArrayList<String>();
//把数据加载到List中去,实际操作一般都是放入请求到的数据
for (int i = 0; i < 10; i++) {
dataList.add("这是第" + i +"个item");
}

//生成一个ArrayAdapter对象,为其设置Item的布局和要加载的数据
arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, dataList);
//把Adapter绑定到已获取的ListView上
listView.setAdapter(arrayAdapter);

}

}


至此完成了一个ListView的简单使用过程。



注:

其他Adapter的使用和此例类似,只是所使用的Adapter有所差别,以SimpleAdapter为例。

使用SimpleAdapter的Java代码

public class MainActivity extends Activity {
private ListView listView;
private SimpleAdapter simpleAdapter;
private List<Map<String,Object>> dataList;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i("MainActivity", "onCreate");
setContentView(R.layout.activity_main);

//模拟获取到的数据
int[] imageIds = new int[]{R.drawable.ic_launcher , R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher};
String[] names = new String[]{"第一个", "第二个", "第三个", "第四个"};

//获取布局文件中的控件
listView = (ListView) findViewById(R.id.lv_main);

dataList = new ArrayList<Map<String, Object>>() {
};
//先将数据以键值对放入HashMap中,再将HashMap放入List中
for (int i = 0; i < 4; i++) {
Map<String,Object> listItem = new HashMap<String,Object>();
listItem.put("Photo", imageIds[i]);
listItem.put("Name", names[i]);
dataList.add(listItem);
}
//生成一个SimpleAdapter对象,为其设置要加载的数据,Item的布局,数据链表里的键,数据链表中键所对应的值要放到的控件ID
//参数依次为:上下文对象,要加载的数据List,用于承载的Item布局,数据list中的键顺序,Item布局中对应于list键的控件id。
simpleAdapter = new SimpleAdapter(this, dataList, R.layout.simple_list_item, new String[]{"Photo", "Name"}, new int[]{R.id.item_image, R.id.item_tv});
//把Adapter绑定到已获取的ListView上
listView.setAdapter(simpleAdapter);
}

}


simple_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="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/item_image"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:padding="5dp"
android:layout_margin="10dp" />
<TextView
android:id="@+id/item_tv"
android:gravity="center"
android:textSize="50px"
android:padding="5dp"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>




这样,我们就创建了一个自己定义的ListView,可以根据需要在simple_list_item.xml布局文件中加入需要的控件,并在Java代码中进行数据的显示。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  listview android 控件