码迷,mamicode.com
首页 > 其他好文 > 详细

BaseAdapter

时间:2016-05-21 11:32:32      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:

BaseAdapterActivity.java

技术分享
package com.example.lenovo.textapp4;

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 BaseAdapterActivity extends AppCompatActivity {

    ListView lv_1;
    List<Myclass> lm;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base_adapter);

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

        //数据集合

        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.f2,"美食2","美食2的介绍"));
        lm.add(new Myclass(R.drawable.f3,"美食3","美食3的介绍"));
        lm.add(new Myclass(R.drawable.f2,"美食2","美食2的介绍"));
        lm.add(new Myclass(R.drawable.f3,"美食3","美食3的介绍"));
        lm.add(new Myclass(R.drawable.f2,"美食2","美食2的介绍"));
        lm.add(new Myclass(R.drawable.f3,"美食3","美食3的介绍"));


        //绑定
        MyBaseAdapter myBaseAdapter=new MyBaseAdapter();
        lv_1.setAdapter(myBaseAdapter);
    }
    //装载数据的分类  实体类POJO类,只包含get set方法
    //一般只包含属性和属性的get set方法
    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);
                convertView=View.inflate(BaseAdapterActivity.this, R.layout.simpleadapter_activity, null);
                //2-得到视图View(Layout文件).加载Layout文件。返回View
                //View view=View.inflate(BaseAdapterActivity.this, R.layout.simpleadapter_activity, 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);
            textView.setText(myclass.getContent());
            //4-返回
            return convertView;
        }
    }
}
View Code

 

 

activity_base_adapter

技术分享
<?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.lenovo.textapp4.BaseAdapterActivity">
    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/lv_1">

    </ListView>

</LinearLayout>
View Code

 

BaseAdapter

标签:

原文地址:http://www.cnblogs.com/1ming/p/5514200.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!