1.垂直的形式列出需要显示的列表,显示系统设置项或直接使用ListView组件创建,另一种是让Activity继承ListActivity实现ListView是开发中使常见的一个控件,一般会对其进行封装.QQ列表等.
2.使用资源文件的listview.
2.1XMl文件(lsitview)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/texview"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ListView>
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
2.2布局文件(activity_res)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
2.2values/string下的资源
<resources>
<string name="app_name">android012</string>
<string-array name="a2">
<item>芷若</item>
<item>无忌</item>
<item>敏敏</item>
<item>珠儿</item>
<item>小昭</item>
<item>无忌</item>
</string-array>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
3 ListVActivity(主类调用所有类)
package com.example.android012;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class ListVActivity extends Activity implements View.OnClickListener {
private Button button01;
private Button button02;
private Button button03;
private Button button04;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lsitview);
button01=findViewById(R.id.button01);
button01.setOnClickListener(this);
button02=findViewById(R.id.button02);
button02.setOnClickListener(this);
button03=findViewById(R.id.button03);
button03.setOnClickListener(this);
button04=findViewById(R.id.button04);
button04.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button01:
//创建意图
Intent li=new Intent(this, ResActivity.class);
startActivity(li);
break;
case R.id.button02:
//创建意图
Intent li2=new Intent(this, ArrayAdapterActivity.class);
startActivity(li2);
break;
case R.id.button03:
//创建意图
Intent li3=new Intent(this, SimpleAdapterActivity.class);
startActivity(li3);
break;
}
}
}
4.1 ResActivity
package com.example.android012;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.icu.text.Transliterator;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class ResActivity extends Activity {
ListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_res);
list = findViewById(R.id.listview);
//定义适配器
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.a2, android.R.layout.simple_expandable_list_item_1);
//给LIST定义适配器
list.setAdapter(adapter);
//监听事件
// list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
// @SuppressLint("WrongConstant")
// @Override
// public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Toast.makeText(ResActivity.this,parent.getItemIdAtPosition(position),0).show();
// }
// });
}
//定义数组
public String[] getData() {
String[] str = new String[12];
for (int i = 0; i < str.length; i++) {
str[i] = "天下" + (i + 1);
}
return str;
}
}
运行实例
5.使用ArrayAdapterActivity的listView文件.
package com.example.android012;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class ArrayAdapterActivity extends Activity {
private ListView lsitview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_res);
lsitview=findViewById(R.id.listview);
//定义适配器
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,getData());
//给LSITVIEW定义适配器
lsitview.setAdapter(adapter);
}
//定义数组
public String[] getData(){
String [] str=new String[12];
for (int i=0;i<str.length;i++){
str[i]="天下"+(i+1);
}
return str;
}
}
5.1运行实例
6.使用simpleAdapterActivity
6.1创建布局(item.xml)
6.2SimpleAdapterActivity
package com.example.android012;
import android.app.Activity;
import android.os.Bundle;
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 SimpleAdapterActivity extends Activity {
private ListView listView;
//声明一个SimpleAdapter
SimpleAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_simple);
listView=findViewById(R.id.list2);
//
adapter=new SimpleAdapter(this,this.getData(),R.layout.item,new String[]{"img","title"},new int[]{R.id.img,R.id.title});//使用构造方法
//给listView设置
listView.setAdapter(adapter);
}
private List<Map<String,Object>> getData(){
List<Map<String,Object>> list=new ArrayList<>();
for (int i=0;i<10;i++){
Map<String,Object>map=new HashMap<String, Object>();
map.put("imag",R.drawable.ic_launcher_background);
map.put("title","新闻"+(i+1));
list.add(map);
}
return list;
}
}
7.自定义的使用Adapter的listview