andorid       列表视图  ListView  之BaseAdapter-LMLPHP

.xml

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lv_2"> </ListView>

.java   //  BaseAdapter

package com.hanqi.application3;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
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; import java.util.ArrayList;
import java.util.List; public class UIActivity5 extends AppCompatActivity { //List<Map<String,Object>> lm;
List<Food> lf;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ui5); ListView lv_2 = (ListView)findViewById(R.id.lv_2); lf= new ArrayList<Food>(); lf.add(new Food(R.drawable.an1, "内容1","美食1"));
lf.add(new Food(R.drawable.an2, "内容2", "美食2"));
lf.add(new Food(R.drawable.an3, "内容3", "美食3"));
lf.add(new Food(R.drawable.an1, "内容4","美食4"));
lf.add(new Food(R.drawable.an1, "内容5","美食5"));
lf.add(new Food(R.drawable.an2, "内容6","美食6"));
lf.add(new Food(R.drawable.an3, "内容7","美食7"));
lf.add(new Food(R.drawable.an1, "内容8","美食8"));
lf.add(new Food(R.drawable.an1, "内容9","美食9"));
lf.add(new Food(R.drawable.an2, "内容10","美食10"));
lf.add(new Food(R.drawable.an3, "内容11","美食11"));
lf.add(new Food(R.drawable.an1, "内容12","美食12")); //准备数据源
// lm = new ArrayList<Map<String,Object>>();
//
// Map<String, Object> map=new HashMap<String, Object>();
// map.put("image",R.drawable.an1);
// map.put("name","美食图片01");
// map.put("content","看上去很好吃01");
// lm.add(map);
//
// map=new HashMap<String, Object>();
// map.put("image",R.drawable.an2);
// map.put("name","美食图片02");
// map.put("content","看上去很好吃02");
// lm.add(map);
//
// map=new HashMap<String, Object>();
// map.put("image",R.drawable.an3);
// map.put("name","美食图片03");
// map.put("content","看上去很好吃03");
// lm.add(map);
lv_2.setAdapter(new MyBaseAdapter()); } class MyBaseAdapter extends BaseAdapter
{
//获取要显示的列表的行数
@Override
public int getCount() {
return lf.size();
}
//获取对应下标行
@Override
public Object getItem(int position) {
return lf.get(position);
}
//获取对应下标行的id
@Override
public long getItemId(int position) {
return position;
}
//给视图适配数据 返回视图
//position 数据的下标
// convertView 可重复使用
//parent 父视图
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//如果 convertView为null 就加载layout文件
if(convertView == null) { //1.获取样式layout文件
LayoutInflater layoutInflater = getLayoutInflater();
convertView = layoutInflater.inflate(R.layout.layout_simple, null);
}
//2.数据和layout文件的视图组件进行绑定
//1)从数据源里获取一行数据
//Map<String, Object> map = lf.get(position);
Food food = lf.get(position);
//2)获取layout文件的视图组件
ImageView iv_2 = (ImageView)convertView.findViewById(R.id.iv_2);
TextView tv_1=(TextView)convertView.findViewById(R.id.tv_1);
TextView tv_2=(TextView)convertView.findViewById(R.id.tv_2);
//3)数据适配
// iv_2.setImageResource((int)map.get("image"));
// tv_1.setText(map.get("name").toString());
// tv_2.setText(map.get("content").toString());
iv_2.setImageResource(food.getImage());
tv_1.setText(food.getName());
tv_2.setText(food.getContent());
//3.返回视图
return convertView; }
}
}
layout_simple.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"> <ImageView
android:layout_width="70dp"
android:layout_height="70dp"
android:src="@drawable/an1"
android:id="@+id/iv_2"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center_vertical"
android:layout_marginLeft="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="美食图片"
android:id="@+id/tv_1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="很好吃的样子"
android:id="@+id/tv_2"/> </LinearLayout> </LinearLayout>
05-04 01:29