MainActivity.java
package com.example.listviewdemo4; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.widget.ListView; public class MainActivity extends Activity { ListView listview; List<Map<String, Object>> list; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listview = (ListView) findViewById(R.id.listview); // 获取数据 list = getData(); listview.setAdapter(new ListViewAdapter(list,MainActivity.this)); } private List<Map<String, Object>> getData() { // TODO Auto-generated method stub List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); Map<String, Object> map = new HashMap<String, Object>(); map.put("name", "图1"); map.put("pictrue", R.drawable.tu1); list.add(map); map = new HashMap<String, Object>(); map.put("name", "图2"); map.put("pictrue", R.drawable.tu2); list.add(map); map = new HashMap<String, Object>(); map.put("name", "图3"); map.put("pictrue", R.drawable.tu3); list.add(map); map = new HashMap<String, Object>(); map.put("name", "图4"); map.put("pictrue", R.drawable.tu4); list.add(map); return list; } }
ListViewAdapter.java
package com.example.listviewdemo4; import java.util.List; import java.util.Map; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; public class ListViewAdapter extends BaseAdapter{ List<Map<String, Object>> list; Context context; LayoutInflater layoutInflater; ListViewAdapter(List<Map<String, Object>> list,Context context){ this.list=list; this.context=context; //使用context上下文获取系统layout_inflater服务 layoutInflater=(LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE); } @Override public int getCount() { // TODO Auto-generated method stub return list.size(); } @Override public Object getItem(int arg0) { // TODO Auto-generated method stub return list.get(arg0); } @Override public long getItemId(int arg0) { // TODO Auto-generated method stub return arg0; } @Override public View getView(int position, View contentView, ViewGroup arg2) { //layoutInflater 传入list_item会生成一个view View view=layoutInflater.inflate(R.layout.list_item, null);
//在生成的view中寻找控件 TextView textview=(TextView)view.findViewById(R.id.textview);
//把控件设置内容 textview.setText((CharSequence) list.get(position).get("name")); ImageView imageView=(ImageView)view.findViewById(R.id.imageview);
//设置图片的资源 imageView.setImageResource((Integer) list.get(position).get("pictrue")); return view; } }
配置文件:
activity_main.xml:
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <ListView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/listview" ></ListView> </RelativeLayout>
listview_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="match_parent" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textview" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageview" /> </LinearLayout>