Android ListView 列表视图
列表显示的三个元素
ListView : 用来显示列表的View
Adapter : 适配器 用来把数据映射到ListView 上的中介
Data: 数据 将被映射的字符串,图片或者基本组件等资源
根据列表的适配器类型 可分为三种: ArrayAdapter, SimpleAdapter ,SimpleCursorAdapter .
ArrayAdapter 最简单 ,只能显示一行字
SimpleAdapter 扩充性好,可以自定义各种效果 ,
SimpleCursorAdapter 可以认为是对数据库的简单结合,可以把数据库的内容以列表的形式显示出来.
ArrayAdapter适配器 简单实例:
package com.app.listviewtest; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; public class MainActivity extends Activity { //创建ListView 对象 private ListView mListView ; //添加数据 List<Object> data; private List getData(){ data = new ArrayList<Object>(); data.add("Java"); data.add("android"); data.add("c"); data.add("c++"); return data; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mListView = new ListView(this);//获得组件 //设置适配器 mListView.setAdapter(new ArrayAdapter<Object>(this,android.R.layout.simple_expandable_list_item_1,getData())); //设置布局 显示listView setContentView(mListView); mListView.setOnItemClickListener( new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { String text =data.get(position).toString(); switch (position) { case 0 : Toast.makeText(getApplicationContext(),text+"被点击" , 0).show(); break; case 1 : Toast.makeText(getApplicationContext(),text+"被点击" , 0).show(); break; case 2 : Toast.makeText(getApplicationContext(),text+"被点击" , 0).show(); break; case 3 : Toast.makeText(getApplicationContext(),text+"被点击" , 0).show(); break; default : break; } } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
SimpleAdapter 的简单使用:
布局文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <ImageView android:id="@+id/ImageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/TextView1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout>
Java文件:
package com.app.listviewtest; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.ListActivity; import android.os.Bundle; import android.view.Window; import android.widget.SimpleAdapter; public class simpleAdapterActivity extends ListActivity { //数据源 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("ImageView1", R.drawable.ic_launcher); map.put("TextView1", "图片一"); list.add(map); map = new HashMap<String, Object>(); map.put("ImageView1", R.drawable.ic_launcher); map.put("TextView1", "图片二"); list.add(map); map = new HashMap<String, Object>(); map.put("ImageView1", R.drawable.ic_launcher); map.put("TextView1", "图片3"); list.add(map); map = new HashMap<String, Object>(); map.put("ImageView1", R.drawable.ic_launcher); map.put("TextView1", "图片4"); list.add(map); return list; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //隐藏标题 requestWindowFeature(Window.FEATURE_NO_TITLE); //设置 适配器 (上下文,数据,模板的资源ID,组件对应的资源,xml布局文件中组件的id) //public SimpleAdapter(Context context, List<? extends Map<String, ?>> data,int resource, String[] from, int[] to) {} SimpleAdapter adapter = new SimpleAdapter(getApplicationContext(), getData(), R.layout.simpleadapter_activity,new String[]{"ImageView1","TextView1"}, new int[] {R.id.ImageView1,R.id.TextView1}); setListAdapter(adapter); } }