ListView 是一种用于垂直显示的列表控件,类似于QQ好友列表的可以上下翻.
三要素:
1.ListView控件
2.适配器
3.数据
一.列表视图第一种创建方式:
单列数据列表,通过ArrayList动态数组创建列表的资源数据,ArrayAdapter创建适配器对象,setAdapter将适配器和数据绑定到一起实现列表视图.
fragment_main.xml文件:
<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">
<ListView
android:id="@+id/listView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FF00FFFF">
</ListView>
</LinearLayout>
MainActivity.java文件:
package com.example.listview;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends ActionBarActivity {
ArrayList<String> songs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
//获取ListView的id
ListView listview = (ListView) findViewById(R.id.listView1);
//创建资源数组
Songs();
//创建适配器对象
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
MainActivity.this, android.R.layout.
simple_list_item_checked, songs);
//将适配器和列表相关联
listview.setAdapter(adapter);
}
private void Songs() {
songs = new ArrayList<String>();
songs.add("望岳1");
songs.add("望岳2");
songs.add("望岳3");
songs.add("望岳4");
songs.add("望岳5");
songs.add("望岳6");
songs.add("望岳7");
songs.add("望岳8");
songs.add("望岳9");
}
}
二.列表视图第二种创建方式:
多列数据列表,fragment_main.xml布局文件内添加ListView控件,contacts.xml布局文件内放置一个ImageView和两个TextView控件组成列表视图的一行,MainActivity.java文件内
fragment_main.xml文件:
<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="horizontal" >
<ListView
android:id="@+id/list1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/ic_launcher" />
</LinearLayout>
<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="horizontal" >
<ListView
android:id="@+id/list1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/ic_launcher" />
</LinearLayout>
自定义的布局文件contacts.xml文件:
<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="horizontal" >
<ListView
android:id="@+id/list1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/ic_launcher" />
</LinearLayout>
<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="horizontal" >
<ListView
android:id="@+id/list1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/ic_launcher" />
</LinearLayout>
MainActivity.java文件:
package com.example.listview2;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class MainActivity extends ActionBarActivity {
public ListView listview;
int[] images = {R.drawable.a1, R.drawable.a2,R.drawable.a3,
R.drawable.a4,R.drawable.a5,};
String[] name = {"德玛德玛","迅捷提莫","九尾阿狸","激光大眼","炮台大头"};
String[] phone = {"111","222","333","444","555"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
listview = (ListView) findViewById(R.id.list1);
//构建数据集合映射
List<Map<String, Object>> data = new ArrayList<Map<String,Object>>();
for (int i = 0; i < images.length; i++){
Map<String, Object> map = new HashMap<String, Object>();
map.put("image", images[i]);
map.put("name", name[i]);
map.put("phone", phone[i]);
data.add(map);
}
//SimpleAdapter:扩展性很好的适配器,可以自定义各种想要的布局
//第一个参数:上下文参数,在我这是本Activity
//第二个参数:数据资源的集合
//第三个参数:自定义布局的XML文件id
//第四个参数:映射列表Map的键的值,是列表里列的名称
//第五个参数:和第四个参数对应,是列表里列的id
SimpleAdapter adapter = new SimpleAdapter(MainActivity.this, data,
R.layout.contacts, new String[]{"image","name","phone"},
new int[]{R.id.image1,R.id.text1,R.id.text2});
//绑定适配器和列表相关联
listview.setAdapter(adapter);
}
}
三.今天的问题
1.Cannot refer to a non-final variable button inside an inner class defined in a different method MainActivity.java
/ImageButton/src/com/example/imagebutton
line 26 Java Problem
我在一个类中创建了一个局部变量,但是在这个类下的匿名内部类中使用这个变量时系统报错,说是要加个final修饰才行.
2.setText()设置文本内容时错误
setText()方法是设置文本内容的,我在使用setText时只往里边传入一个int型参数时,编译不报错,运行时刚开始也可以运行,但是只要一涉及到设置的文本时,虚拟器直接弹出错误框退出.
我在一个类中创建了一个局部变量,但是在这个类下的匿名内部类中使用这个变量时系统报错,说是要加个final修饰才行.
2.setText()设置文本内容时错误
setText()方法是设置文本内容的,我在使用setText时只往里边传入一个int型参数时,编译不报错,运行时刚开始也可以运行,但是只要一涉及到设置的文本时,虚拟器直接弹出错误框退出.