zoukankan      html  css  js  c++  java
  • BaseAdapter教程(1) 最简单地使用BaseAdapter

    Adapter就是适配器,而设计模式里也有Adapter Pattern。

    而BaseAdapter就是设计模式里的思维,把一些不相关的东西放进去,经过适配器,最终都会出产同一样的东西。

    就像BaseAdapter,就算放不同的XML和Data进去,最终出现都是ListView的Cell。

    1. BaseAdaper代码,完全没有特别的地方,不解述了。

    package com.paradise.electronic.eparadise2.adapter;
    
    import android.content.Context;
    import android.graphics.Bitmap;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.ImageView;
    import android.widget.TextView;
    
    import com.paradise.electronic.eparadise2.R;
    import com.paradise.electronic.eparadise2.dto.PostDTO;
    
    import java.util.List;
    
    /**
     * Created by Administrator on 17-11-12.
     */
    public class PostAdapter extends BaseAdapter {
    
        // data
        private Context context;
        private LayoutInflater inflter;
        private List<PostDTO> postDTOs;
        private List<Bitmap> bitMaps;
    
        // UI
        private TextView tvPostTitle;
        private TextView tvPostDate;
        private ImageView imgAbstractImg;
    
        public PostAdapter(Context context, List<PostDTO> postDTOs, List<Bitmap> bitMaps) {
            super();
            this.postDTOs = postDTOs;
            this.bitMaps = bitMaps;
            this.context = context;
            inflter = (LayoutInflater.from(context));
        }
    
        @Override
        public int getCount() {
            return postDTOs.size();
        }
    
        @Override
        public Object getItem(int position) {
            return null;
        }
    
        @Override
        public long getItemId(int position) {
            return position;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
    
            // 初始化convertView
            convertView = inflter.inflate(R.layout.lv_home_post, null);
    
            // 初始化UI
            tvPostTitle = (TextView) convertView.findViewById(R.id.tvPostTitle);
            tvPostDate = (TextView) convertView.findViewById(R.id.tvPostDate);
            imgAbstractImg = (ImageView) convertView.findViewById(R.id.imgAbstractImg);
    
            // Set date into tv
            tvPostTitle.setText(postDTOs.get(position).getTitle());
            tvPostDate.setText(postDTOs.get(position).getDate());
            imgAbstractImg.setImageBitmap(bitMaps.get(position));
    
            return convertView;
        }
    
    }

    2. 初始化BaseAdaper,然后让ListView调用BaseAdaper就可以。

        private String strJsPost;
        private void initData() {
            // set view unclickable first
            rlFragmentHome.setClickable(false);
    
            // Network thread
            new Thread(new Runnable() {
                @Override
                public void run() {
                    // get <JSON>post from network
                    strJsPost = okhttp.getResponse(Configuration.getHotPost);
                    postHotDTOs = gsonTools.getPost(strJsPost);
    
                    // get post image URL from network, than convect the file to bitmap
                    bitHotMaps = new ArrayList<Bitmap>();
                    for (int i = 0; i < postHotDTOs.size(); i++ ) {
                        Bitmap temp = bitMap.returnBitMap(postHotDTOs.get(i).getAbstractImg());
                        bitHotMaps.add(temp);
                    }
    
                    // UI thread
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            // init adapter
                            postAdapter = new PostAdapter(getActivity(), postHotDTOs, bitHotMaps);
                            lvHomePostItem.setAdapter(postAdapter);
    
                            // hide loading textview
                            tvHomeLoading.setText("");
                            tvHomeLoading.getLayoutParams().height = 0;
    
                            // set view unclickable while finishing
                            rlFragmentHome.setClickable(true);
                        }
                    });
                }
            }).start();
        }
  • 相关阅读:
    增量+全量备份SVN服务器
    日常小命令集锦
    filebeat输出到kafka
    在Logstash的配置文件中对日志事件进行区分
    NFS服务器简易安装
    记录一次MySQL数据库CPU负载异常高的问题
    使用Spring的jdbcTemplate进一步简化JDBC操作
    Stream 和 byte[] 之间的转换
    C# 文件转byte数组,byte数组再转换文件
    groupbox里面添加Form
  • 原文地址:https://www.cnblogs.com/chenkuang/p/8127748.html
Copyright © 2011-2022 走看看