Adapter for android listview---baseadapter

Basic articles:

Adapter is a design pattern, such as listview gridview recyclerview and so on in android all use this concept. It is to display different data sources in a list or grid. An adapter is needed, and the adapter displays different data sources into a list. The adapter actually acts as a bridge. Just like the usb interface, no matter your MP3 PM4, mobile phone, dvd and other products, you just need to implement this interface. The data and view can be used together.

1. Use the simple layout file of listview baseadapter

2. Write a class inheritance and baseadapter

3. Associating the adapter with the listview is ok

In order to illustrate the role of the adapter, I will write two kinds of adapters for the same listview later and see the different effects.

activity's layout file


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <!--既然要将listview 就不写那么多冗余的东西了 -->
    <ListView
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></ListView>
</LinearLayout>

Layout file for listview item

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <!--简单定义了listview 的item-->
    <TextView
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:gravity="center"
        />
</LinearLayout>

First give the wording of the adapter

package com.lefeng.lfvideo.myndk.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.lefeng.lfvideo.myndk.R;
import java.util.List;

/**
 * Created by CYL on 2016/3/26.
 * email:[email protected]
 */
public class MyAdapter1 extends BaseAdapter{
    //数据源 建议 包含一个对象  方便操作
    private List<String> dataSource;
    //初始化 listview 每个item 的布局文件
    private LayoutInflater inflater;

    /**
     * 构造器
     * @param dataSource 数据源
     * @param context     上下文
     */
    public MyAdapter1(List<String> dataSource, Context context) {
        this.dataSource = dataSource;
        this.inflater = LayoutInflater.from(context);
    }

    /**
     * 获取listview 总长度  也就是数据源的长度
     * @return
     */
    @Override
    public int getCount() {
        return dataSource.size();
    }

    /**
     * 得到数据源上的对象 点击item的时候 可以获取数据然后进行操作
     * @param i
     * @return
     */
    @Override
    public Object getItem(int i) {
        return dataSource.get(i);
    }

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

    /**
     * 为了缓存 listview 上的  view 如iamgeview textview button 等view
     * 因为 每次findviewbyid 是很浪费资源的
     * 对应listview 每个item的view
     * 这个到 android L 除了新得的组件recyclerview 已经把这个东西给简化了   有兴趣的可以去看看
     */
    private final class ViewHolder{
        public TextView tv;
    }
    /**
     * 这个方法比较重要  就是每次初始化 listview item 的 view
     * @param i           item 的位置 从0 开始
     * @param view        item 的view
     * @param viewGroup   父容器
     * @return
     */
    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder = null;
        if(view == null){
            viewHolder = new ViewHolder();
            view = inflater.inflate(R.layout.mylistview_item1, null);
            viewHolder.tv = (TextView) view.findViewById(R.id.tv);
            //将viewholder 缓存起来
            view.setTag(viewHolder);
        }
        else {
            //可以从holder 得到
            viewHolder = (ViewHolder) view.getTag();
        }
        viewHolder.tv.setText(dataSource.get(i));
        return view;
    }
}
The comment is almost done, no more verbosity.

activity code

package com.lefeng.lfvideo.myndk;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;

import com.lefeng.lfvideo.myndk.adapter.MyAdapter1;

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

/**
 * Created by CYL on 2016/3/26.
 * email:[email protected]
 */
public class MyListViewActivity extends AppCompatActivity{
    private ListView lv;
    private List<String> dataSource;
    private MyAdapter1 adapter1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置布局文件
        setContentView(R.layout.mylistview_layout);
        //实例化listview
        lv = (ListView) findViewById(R.id.lv);
        //初始化数据源   可以从手机本地获取,可以从网络获取 等
        dataSource = new ArrayList<>();
        for (int i = 0;i<100;i++){
            dataSource.add(getRandWorld());
        }
        adapter1 = new MyAdapter1(dataSource,this);
        lv.setAdapter(adapter1);
    }

    /**
     * 随机产生 5位字符
     * @return
     */
    private String getRandWorld(){
        String world = "";
        for (int i = 0; i < 5; i++){
            int val = (int) (Math.random()*26+1)+'a';
            world += (char)val;
        }
        return  world;
    }

}


Screenshot of running results




In order to illustrate the role of the adapter, the code of the activity on the implementation of an adapter does not change and is used directly


The change is added to the layout file of the item of the listview, and the corresponding adapter of the imageview is not given out. Basically, it is to obtain the view and set the view value. If the data source changes, it is to store a copy of the picture of each item, so that you can use the id or the network. The url depends on the demand  


given item's layout file

<?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="match_parent">
    <!--简单定义了listview 的item-->
    <ImageView
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_margin="10dp"
        android:src="@drawable/logo"/>
    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:gravity="center"
        android:layout_gravity="center_vertical"
        />
</LinearLayout>
After changing the layout, change the data source adapter to see the effect

It can be seen that different results can be displayed through different data sources. . . .


Guess you like

Origin blog.csdn.net/tiandiyinghun/article/details/50985305