main4.xml
<?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" > <ListView android:id="@+id/listView4_1_1" android:layout_width="match_parent" android:layout_height="wrap_content" ></ListView> </LinearLayout>
main4_item.xml
<?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="wrap_content" android:orientation="horizontal" android:gravity="center" > <ImageView android:id="@+id/imageView4_1_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/textView4_1_1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="textView" /> </LinearLayout>
MainActivity4
package com.example.listview; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.R.anim; import android.R.drawable; import android.app.Activity; import android.os.Bundle; import android.widget.ListView; import android.widget.SimpleAdapter; public class MainActivity4 extends Activity{ private ListView listView; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.main4); listView=(ListView) findViewById(R.id.listView4_1_1); //一个列表项的内容 Map<String, Object> map=new HashMap<String, Object>(); map.put("image", R.drawable.ic_launcher); map.put("name", "张三"); Map<String, Object> map2=new HashMap<String, Object>(); map2.put("image", R.drawable.ic_launcher); map2.put("name", "王五"); List<Map<String, Object>> list=new ArrayList<Map<String,Object>>(); list.add(map); list.add(map2); SimpleAdapter simpleAdapter=new SimpleAdapter(this, list, R.layout.main4_item, new String [] {"image","name"}, new int [] {R.id.imageView4_1_1,R.id.textView4_1_1}); listView.setAdapter(simpleAdapter); } }
亦可以自定义Adapter
mian5.xml
<?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" > <ListView android:id="@+id/listView5_1_1" android:layout_width="match_parent" android:layout_height="wrap_content" ></ListView> </LinearLayout>
main5_item.xml
<?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="wrap_content" android:orientation="horizontal" android:gravity="center" > <ImageView android:id="@+id/imageView5_1_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/a" /> <TextView android:id="@+id/textView5_1_1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="textView" /> </LinearLayout>
MainActivity5
package com.example.listview; import android.app.Activity; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; public class MainActivity5 extends Activity { private ListView listView; //定义的数据 private int [] images={R.drawable.ic_launcher,R.drawable.ic_launcher}; private String [] names={"xiaobai","xiaoming"}; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.main5); listView=(ListView) findViewById(R.id.listView5_1_1); listView.setAdapter(new MyAdapter()); } //自定义适配器 class MyAdapter extends BaseAdapter{ //获取列表项总数 @Override public int getCount() { // TODO Auto-generated method stub return names.length; } //获取每一个列表项(根据下标获取) @Override public Object getItem(int position) { // TODO Auto-generated method stub return names[position]; } //获取每一个列表项的id @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } // @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub //获取布局填充器 LayoutInflater layoutInflater=getLayoutInflater(); //获取main5_item的布局模板 View view=layoutInflater.inflate(R.layout.main5_item, null); //从得到的view查找相应view ImageView imageView=(ImageView) view.findViewById(R.id.imageView5_1_1); TextView textView=(TextView) view.findViewById(R.id.textView5_1_1); imageView.setImageResource(images[position]); textView.setText(names[position]); return view; } } }
这种方式会多次调用getView()方法
本文出自 “matengbing” 博客,请务必保留此出处http://matengbing.blog.51cto.com/11395502/1882233
原文地址:http://matengbing.blog.51cto.com/11395502/1882233