• 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();
        }
  • 相关阅读:
    C#根据IP地址和子网掩码计算广播地址
    关于ZIP大文件压缩
    JVM内存的设置
    Eclipse下内存溢出错误(OutOfMemoryError)
    实体化视图的使用心得(转)
    Java 调用cmd.exe命令
    select 操作选中添加、删除操作Javascript
    quartz定时任务时间设置
    手动安装 MyEclipse6.5 FindBugs
    App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure.-解决办法
  • 原文地址:https://www.cnblogs.com/chenkuang/p/8127748.html
Copyright © 2020-2023  润新知