ListView理解
ListView是一种用来显示多个可滑动项(Item)列表的ViewGroup
需要使用Adapter将集合数据和每一个Item所对应的布局动态适配到ListView中显示
显示列表: listView.setAdapter(adapter)
更新列表:adapter.notifyDataSetChanged()
———————————————————————————
ListView效果样例
Adapter
集合数据为List<String>或String[]
集合数据必须是List<Map<String,Object>>类型
集合数据可以是任意类型的集合List<Xxx>
集合数据是数据库查询结果集
————————————————————————————————————————————————————
ListView+ ArrayAdapter
context: 上下文件对象, 一般为Acivity对象
resource:Item的布局文件标识
objects: 需要显示的数据集合(Array或List)
————————————————————————————————————————————————————
ListView+ SimpleAdapter
context: 上下文件对象, 一般为Acivity对象
data: 需要显示的数据集合
resource:Item布局文件标识
from: map对象中的key的数组, 用于得到对应的value
to:Item布局文件中的子view的id的数组
————————————————————————————————————————————————————
ListView+ BaseAdapter
classMyBaseAdapterextends BaseAdapter{
@Override
public intgetCount() {
return0;
}
@Override
public Object getItem(int position) {
returnnull;
}
@Override
public long getItemId(int position) {
return0;
}
@Override
public View getView(int position,
ViewconvertView, ViewGroup parent) {
returnnull;
}
}
————————————————————————————————————————————————————
package com.example.test08_listvieew;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class CopyOfMainActivity_Simple extends Activity {
private ListView lv_main;
private List<ShoppInfo> data;//商品信息数据源
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv_main=(ListView) findViewById(R.id.lv_main);
//准备集合数据
data=new ArrayList<ShoppInfo>();
data.add(new ShoppInfo(R.drawable.f1,"美食名称--1","美食的内容---1"));
data.add(new ShoppInfo(R.drawable.f2,"美食名称--2","美食的内容---2"));
data.add(new ShoppInfo(R.drawable.f3,"美食名称--3","美食的内容---3"));
data.add(new ShoppInfo(R.drawable.f4,"美食名称--4","美食的内容---4"));
data.add(new ShoppInfo(R.drawable.f5,"美食名称--5","美食的内容---5"));
data.add(new ShoppInfo(R.drawable.f6,"美食名称--6","美食的内容---6"));
data.add(new ShoppInfo(R.drawable.f7,"美食名称--7","美食的内容---7"));
data.add(new ShoppInfo(R.drawable.f8,"美食名称--8","美食的内容---8"));
data.add(new ShoppInfo(R.drawable.f9,"美食名称--9","美食的内容---9"));
data.add(new ShoppInfo(R.drawable.f10,"美食名称--10","美食的内容---10"));
//准备适配器对象 BaseAdatper
MyAdapter adapter = new MyAdapter();
//
lv_main.setAdapter(adapter);
}
package com.example.test08_listvieew;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class MainActivity_Array extends Activity {
private ListView lv_main;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv_main=(ListView) findViewById(R.id.lv_main);
//准备集合
List<Map<String, Object>> data = new ArrayList<Map<String,Object>>();
HashMap<String, Object> mp = new HashMap<String, Object>();
mp.put("icon", R.drawable.f1);
mp.put("name", "美食---1");
mp.put("content", "内容--1");
data.add(mp);
mp=new HashMap<String, Object>();
mp.put("icon", R.drawable.f2);
mp.put("name", "美食---2");
mp.put("content", "内容--2");
data.add(mp);
mp=new HashMap<String, Object>();
mp.put("icon", R.drawable.f3);
mp.put("name", "美食---3");
mp.put("content", "内容--3");
data.add(mp);
mp=new HashMap<String, Object>();
mp.put("icon",R.drawable.f4);
mp.put("name", "美食---4");
mp.put("content", "内容--4");
data.add(mp);
mp=new HashMap<String, Object>();
mp.put("icon", R.drawable.f5);
mp.put("name", "美食---5");
mp.put("content", "内容--5");
data.add(mp);
mp=new HashMap<String, Object>();
mp.put("icon", R.drawable.f6);
mp.put("name", "美食---6");
mp.put("content", "内容--6");
data.add(mp);
mp=new HashMap<String, Object>();
mp.put("icon", R.drawable.f7);
mp.put("name", "美食---7");
mp.put("content", "内容--7");
data.add(mp);
mp=new HashMap<String, Object>();
mp.put("icon", R.drawable.f8);
mp.put("name", "美食---8");
mp.put("content", "内容--8");
data.add(mp);
mp=new HashMap<String, Object>();
mp.put("icon", R.drawable.f9);
mp.put("name", "美食---9");
mp.put("content", "内容--9");
data.add(mp);
mp=new HashMap<String, Object>();
mp.put("icon", R.drawable.f10);
mp.put("name", "美食---10");
mp.put("content", "内容--10");
data.add(mp);
//map对象的key数组 ,用于得到对应的value
String[] from={"icon","name","content"};
//item布局文件中的子View的id的数组
int[] to={R.id.iv_item_icon,R.id.iv_item_name,R.id.iv_item_content};
//准备SimpleAdapter对象
SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.item_simple_adapter, from, to);
//设置Adapter显示列表
lv_main.setAdapter(adapter);
}
}
@Override//返回集合的总数
public int getCount() {
return data.size();
}
@Override//返回指定下标对应的数据对象
public Object getItem(int position) {
return data.get(position);
}
@Override//返回每个条目的id 下标
public long getItemId(int position) {
return position;
}
@Override//返回指定下标所对应(Item)的视图对象
//position 下标 parent listView对象
//convertView 可复用Item的视图对象 前n+1个为null
public View getView(int position, View convertView, ViewGroup parent) {
ViewHodler holder=null;
//如果没有复用
if (convertView==null) {
//加载条目(Item)布局 得到视图(View)对象
convertView= View.inflate(CopyOfMainActivity_Simple.this,
R.layout.item_simple_adapter, null);
holder=new ViewHodler();
holder.inv = (ImageView) convertView.findViewById(R.id.iv_item_icon);
holder.nametext = (TextView) convertView.findViewById(R.id.iv_item_name);
holder.inv2 = (TextView) convertView.findViewById(R.id.iv_item_content);
//将holder 保存在convertView上
convertView.setTag(holder);
}else{
//复用时 取出holder
holder=(ViewHodler) convertView.getTag();
}
//2. 得到当前行的对象
ShoppInfo shoppInfo = data.get(position);
//3.给ViewHolder对象的数据设置数据
holder.inv.setImageResource(shoppInfo.getIcon());
holder.nametext.setText(shoppInfo.getName());
holder.inv2.setText(shoppInfo.getContent());
return convertView;
}//视图容器类
class ViewHodler{
public ImageView inv;
public TextView nametext;
public TextView inv2;
}
}
}
package com.example.test08_listvieew;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {
private ListView lv_main;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv_main=(ListView) findViewById(R.id.lv_main);
//准备集合
String[] data={"A","B","C","D","E","F","G","H","J","K","L","O"};
//准备适配器ArrayAdapter对象
ArrayAdapter<String> adapter=new ArrayAdapter<String>
(this, R.layout.item_array,data);
//设置adapter显示列表
lv_main.setAdapter(adapter);
}
}
<?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="horizontal" >
<ImageView
android:id="@+id/iv_icon"
android:layout_width="70dp"
android:layout_height="70dp"
android:src="@drawable/ic_launcher" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="70dp"
android:gravity="center_vertical"
android:orientation="vertical"
android:layout_marginLeft="10dp"
>
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="@+id/tv_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
</LinearLayout>
<?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="horizontal" >
<ImageView
android:id="@+id/iv_item_icon"
android:layout_width="70dp"
android:layout_height="70dp"
android:src="@drawable/f1" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="70dp"
android:orientation="vertical"
android:gravity="center_vertical"
android:layout_marginLeft="10dp"
>
<TextView
android:id="@+id/iv_item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
/>
<TextView
android:id="@+id/iv_item_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
</LinearLayout>
package com.example.test08_listvieew;
public class ShoppInfo {
private int icon;
private String name;
private String content;
public ShoppInfo() {
super();
}
public ShoppInfo(int icon, String name, String content) {
super();
this.icon = icon;
this.name = name;
this.content = content;
}
public int getIcon() {
return icon;
}
public void setIcon(int icon) {
this.icon = icon;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
@Override
public String toString() {
return "ShoppInfo [icon=" + icon + ", name=" + name + ", content="
+ content + "]";
}
}