首页 技术 正文
技术 2022年11月10日
0 收藏 446 点赞 4,698 浏览 3710 个字

ListView类作为在Android开发中经常会使用到的组件,作为新手,还是感到这一块变化形式还是很多的,需要慢慢学习。现在这里大概总结一下。

基于数组的ListView:使用android:entries属性可以指定列表项数组,这种方式最简洁方便,但内容只能是文本,可定制的内容少之又少。

使用ArrayAdapter创建ListView:也仅限于将数组或集合里的元素包装成列表项,比直接使用数组好在,可以指定列表项的列表项组件。例如:使用TextView作为列表项组件,不过也只能使用TextView。

例子:

1.列表项布局文件arraylist_item.xml:是的,没错,布局文件只包含一个TextView,这也是行的。

<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/text1"/>

2.主界面布局文件activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.zjlyyq.test.MainActivity"> <ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/listView" />
</LinearLayout>

3.MainActivity:

package com.example.zjlyyq.test;import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;public class MainActivity extends AppCompatActivity {
ListView listView;
String[] names = new String[]{"晃过天空","芝加哥公牛队","但使东山谢安石","颠三倒四","临时监护人"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView)findViewById(R.id.listView);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.arraylist_item,names);
listView.setAdapter(adapter);
}
}

ListView总结

使用SimpleAdapter创建ListView:SimpleAdapter可以满足更强的定制,每个列表项对应一个布局文件,将一个Map里的元素对应在布局文件里的各组件,例如:

1.列表项布局文件simpleadapter_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="match_parent">
<ImageView
android:layout_weight="0.15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/header"/>
<LinearLayout
android:layout_weight="0.85"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/username"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/talk"/>
</LinearLayout>
</LinearLayout>

2.MainActivity:

package com.example.zjlyyq.test;import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;public class MainActivity extends AppCompatActivity {
ListView listView;
String[] names = new String[]{"晃过天空","芝加哥公牛队","但使东山谢安石","颠三倒四","临时监护人"};
int[] headers = new int[]{R.drawable.plane,R.drawable.plane,R.drawable.plane,R.drawable.plane,R.drawable.plane};
String[] talks = new String[]{"晚上吃了吗?","吃了","你呢?","还没?","哦哦"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView)findViewById(R.id.listView);
List<Map<String,Object>> itemslist = new ArrayList<Map<String,Object>>();
for(int i = 0;i < 5;i ++){
Map<String,Object> evetyItem = new HashMap<String,Object>();
evetyItem.put("header",headers[i]);
evetyItem.put("name",names[i]);
evetyItem.put("talk",talks[i]);
itemslist.add(evetyItem);
}
//创建一个SimpleAdapter
SimpleAdapter simpleAdapter = new SimpleAdapter(this,itemslist,R.layout.simpleadapter_item,
new String[]{"header","name","talk"},
new int[]{R.id.header,R.id.username,R.id.talk}); listView.setAdapter(simpleAdapter);
}
}

ListView总结

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,086
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,561
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,410
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,183
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,820
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,903