适配器3

package com.example.wang.testapp2;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
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 TestActivity9 extends AppCompatActivity {

    ListView lv_3;
    List<MyClass> lm;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test9);

        lv_3=(ListView)findViewById(R.id.lv_3);

        //数据集合
        lm=new ArrayList<>();

        MyClass myClass = new MyClass(R.drawable.f1,"美食1","美食1的介绍");

        lm.add(myClass);

        lm.add(new MyClass(R.drawable.f2,"美食2","美食2的介绍"));
        lm.add(new MyClass(R.drawable.f3,"美食3","美食3的介绍"));
        lm.add(new MyClass(R.drawable.f4,"美食4","美食4的介绍"));
        lm.add(new MyClass(R.drawable.f5,"美食5","美食5的介绍"));
        lm.add(new MyClass(R.drawable.f6,"美食6","美食6的介绍"));
        lm.add(new MyClass(R.drawable.f7,"美食7","美食7的介绍"));
        lm.add(new MyClass(R.drawable.f8,"美食8","美食8的介绍"));
        lm.add(new MyClass(R.drawable.f9,"美食9","美食9的介绍"));
        lm.add(new MyClass(R.drawable.f10,"美食10","美食10的介绍"));

        MyBaseAdapter myBaseAdapter=new MyBaseAdapter();

        lv_3.setAdapter(myBaseAdapter);
    }

    //装载数据的类 实体类  POJO类
    //一般只包含属性和属性的getter和setter方法
    class MyClass
    {
        //img
        private int img;
        //name
        private String name;
        //content
        private  String content;

        public int getImg() {
            return img;
        }

        public void setImg(int img) {
            this.img = img;
        }

        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;
        }

        //默认构造方法
        public MyClass()
        {

        }
        //直接初始化属性的构造方法
        public MyClass(int img,String name,String content)
        {
            this.img=img;
            this.name=name;
            this.content=content;

        }
    }

    //内部类继承抽象类BaseAdapter实现4个方法
    class MyBaseAdapter extends BaseAdapter
    {
        @Override
        public int getCount() {
            return lm.size();
        }

        @Override
        public Object getItem(int position) {
            return lm.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        //返回适配完成的视图
        //convertView 可以复用的视图
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            //适配一条数据和View
            //1-得到数据
            MyClass myClass=lm.get(position);

            if (convertView==null)
            {

                Log.e("TAG","适配器 getView="+position);
                //2-得到View
                //加载layout文件
                convertView =View.inflate(TestActivity9.this, R.layout.simple_adpter, null);
            }

            //3-把数据放到View里

            ImageView imageView=(ImageView)convertView.findViewById(R.id.iv_2);
            imageView.setImageResource(myClass.getImg());

            TextView textView=(TextView)convertView.findViewById(R.id.tv_7);
            textView.setText(myClass.getName());


            TextView textView1=(TextView)convertView.findViewById(R.id.tv_8);
            textView1.setText(myClass.getContent());

            //4-返回数据
            return convertView;
        }
    }
}
java
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="com.example.wang.testapp2.TestActivity9">
    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/lv_3"></ListView>

</LinearLayout>
xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:src="@drawable/f1"
        android:id="@+id/iv_2"/>
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_marginLeft="20dp"
        android:gravity="center_vertical"
        android:layout_weight="1">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="名字=aaa"
            android:id="@+id/tv_7"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="内容=aaa"
            android:id="@+id/tv_8"/>

    </LinearLayout>

</LinearLayout>
View Code

posted @ 2016-05-19 19:55  1011042043  阅读(94)  评论(0编辑  收藏  举报