«

BaseAdapter初体验---(mooc)

时间:2024-3-2 18:45     作者:韩俊     分类: Android


数据适配器-BaseAdapter

什么是数据适配器?


-数据适配器的使用:
1.降低程序耦合性
2.容易扩展

ListView的显示与缓存机制


-需要才显示,显示完就回收到缓存

eclipse工程简图

Bean.java

package com.example.baseadapter;

public class Bean {
    private int pic;
    private String title;
    private String content;

    public Bean(int pic, String title, String content) {
        super();
        this.pic = pic;
        this.title = title;
        this.content = content;
    }

    public int getPic() {
        return pic;
    }

    public void setPic(int pic) {
        this.pic = pic;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

}

MainActivity.java

package com.example.baseadapter;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;

public class MainActivity extends Activity {

    private ListView listview;
    private List<Bean> listbean;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listview = (ListView) findViewById(R.id.lv);
        listbean = new ArrayList<Bean>();

        for (int i = 0; i < 20; i++) {
            listbean.add(new Bean(R.drawable.ic_launcher,"title"+i,"content"+i));
        }
        listview.setAdapter(new Myadapter(this,listbean));
    }

}

Myadapter.java

package com.example.baseadapter;

import java.util.List;

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 Myadapter extends BaseAdapter {

    private Context context;
    private List<Bean> listbean;
    private LayoutInflater minflater;

    public Myadapter(Context context, List<Bean> listbean) {
        super();
        this.context = context;
        this.listbean = listbean;
        minflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return listbean.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return listbean.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        //This is the arttistic mode 
        ViewHolder viewHolder;
        if(convertView == null){
            viewHolder = new ViewHolder();
            convertView = minflater.inflate(R.layout.item, null);
            viewHolder.pic = (ImageView) convertView.findViewById(R.id.pic);
            viewHolder.title = (TextView) convertView.findViewById(R.id.title);
            viewHolder.content = (TextView) convertView.findViewById(R.id.content);
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) convertView.getTag();
        }
        Bean bean = listbean.get(position);
        viewHolder.pic.setImageResource(bean.getPic());
        viewHolder.title.setText(bean.getTitle());
        viewHolder.content.setText(bean.getContent());
        return convertView;
        }

        class ViewHolder {
            public ImageView pic;
            public TextView title;
            public TextView content;
        }
}

activity_main.xml

<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:orientation="vertical"
    tools:context="com.example.baseadapter.MainActivity" >

    <ListView
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        </ListView>
</LinearLayout>

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="horizontal" >

    <ImageView
        android:id="@+id/pic"
        android:layout_width="60dp"
        android:layout_height="60dp" 
        android:src="@drawable/ic_launcher"
        />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:orientation="vertical"
        >
        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="title"
            android:textSize="30sp"
            />
        <TextView
            android:id="@+id/content"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="content"
            android:textSize="15sp"
            />
      </LinearLayout>

</LinearLayout>

总结:

ViewHolder优化BaseAdapter思路:

创建Bean对象,用于封装数据
在构造方法中初始化用于映射的数据List
创建ViewHolder类,创建布局映射关系
判断converView,为空则创建,并设置tag,否则通过tag来取出ViewHolder
给ViewHolder中的空间设置数据

备注:

@Override
    public View getView(int position, View convertView, ViewGroup parent) {

        //This is the stupid mode
        View view = minflater.inflate(R.layout.item, null);
        ImageView pic = (ImageView) view.findViewById(R.id.pic);
        TextView title = (TextView) view.findViewById(R.id.title);
        TextView content = (TextView) view.findViewById(R.id.content);
        Bean bean = listbean.get(position);
        pic.setImageResource(bean.getPic());
        title.setText(bean.getTitle());
        content.setText(bean.getContent());
        return view;

        //This is the normal mode
        //判断缓冲区中是否有view 
        if (convertView == null) {
            convertView = minflater.inflate(R.layout.item, null);
        }
        ImageView pic = (ImageView) convertView.findViewById(R.id.pic);
        TextView title = (TextView) convertView.findViewById(R.id.title);
        TextView content = (TextView) convertView.findViewById(R.id.content);
        Bean bean = listbean.get(position);
        pic.setImageResource(bean.getPic());
        title.setText(bean.getTitle());
        content.setText(bean.getContent());
        return convertView;
}

标签: android

热门推荐