网络请求Adapter添加数据

一般在开发中我们都需要在listview中添加数据显示在界面上

1.首先我们会在布局中写一个listview

 1 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2              xmlns:tools="http://schemas.android.com/tools"
 3              android:layout_width="match_parent"
 4              android:layout_height="match_parent"
 5              android:background="#EFEFF4"
 6              android:clipChildren="false"
 7     >
 8 
 9     <ListView
10             android:id="@+id/id_rada_list"
11             android:layout_width="match_parent"
12             android:layout_height="match_parent"
13             android:layout_marginTop="10dp"
14             android:divider="#00000000"/>
15 
16 
17 </FrameLayout>

2.初始化listview的布局控件

ListView rada_list = findViewById(R.id.id_rada_list);

 3.网络请求数据在listview中加载adapter

  rada_list.setAdapter(new MyListAdapter());

 4.关键步骤adapter添加数据

   //listview 设置适配器

    class MyListAdapter extends BaseAdapter {

        private DisplayImageOptions options;

        public MyListAdapter() {
            options = new DisplayImageOptions.Builder()
                    .showImageOnLoading(R.drawable.logo)//加载中图片的设置
                    .showImageForEmptyUri(R.drawable.logo)//url为null的情况下
                    .showImageOnFail(R.drawable.logo)//加载图片失败的时候的设置
                    .cacheInMemory(true)//是否缓存在内存
                    .cacheOnDisk(true)//是否缓存在硬盘
                    .considerExifParams(true)//exif:一张图片的附加信息  位置信息、时间信息、角度
                    .displayer(new FadeInBitmapDisplayer(500))//设置展示器
                    .build();
        }

        @Override
        public int getCount() {
            return redEnvelope.size();
        }

        @Override
        public Object getItem(int position) {
            return redEnvelope.get(position);
        }

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

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView == null) {
                convertView = View.inflate(getApplication(), R.layout.item_radar, null);
                holder = new ViewHolder();
                holder.ivImageview = convertView.findViewById(R.id.iv_rade_image);
                holder.tvName = convertView.findViewById(R.id.tv_name);
                holder.tvMonkey = convertView.findViewById(R.id.tv_money);
                holder.tvNuber = convertView.findViewById(R.id.tv_nuber);
                holder.btCome = convertView.findViewById(R.id.bt_come);
                convertView.setTag(holder);
            } else {

                holder = (ViewHolder) convertView.getTag();

            }
            holder.tvName.setText(redEnvelope.get(position).getNickname() + "");
            DecimalFormat format = new DecimalFormat("#.##");
            double aAmount = redEnvelope.get(position).getActualAmount();
            holder.tvMonkey.setText(format.format(aAmount));
            holder.tvNuber.setText(redEnvelope.get(position).getNumber() + "");
            String photoUrl = redEnvelope.get(position).getPhotoUrl();

            //  String url = GlobalConstants.URL_PREFIX + news.get(position).listimage;
            ImageLoader.getInstance().displayImage(photoUrl, holder.ivImageview, options);
            holder.btCome.setOnClickListener(new View.OnClickListener() {


                @Override
                public void onClick(View v) {//点击红包雷达 去抢红包
                    if(redNum == 0){
                        DialogBuilder.messageDialog(RadarActivity.this).setMessage("您的抢红包次数已使用完啦!!!").show();
                        return;
                    }
                    nickname = redEnvelope.get(position).getNickname();
                    photoUrl1 = redEnvelope.get(position).getPhotoUrl();
                    actualAmount = redEnvelope.get(position).getActualAmount();
                    id = redEnvelope.get(position).getId();
                    number = redEnvelope.get(position).getNumber();
                    pos = position;
                    toValidate();

                }
            });


            return convertView;
        }
    }

    //非静态内部类会默认拥有外部类的实例对象
    static class ViewHolder {
        public ImageView ivImageview;
        public TextView  tvName;
        public TextView  tvMonkey;
        public TextView  tvNuber;
        public Button    btCome;


    }

 6.adtpter布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:app="http://schemas.android.com/apk/res-auto"
              xmlns:tools="http://schemas.android.com/tools"
              android:layout_width="match_parent"
              android:layout_height="match_parent"

              android:fitsSystemWindows="true"
              android:orientation="vertical">



    <LinearLayout
        android:id="@+id/ll_pay_wx"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:layout_marginLeft="16dp"
        android:background="#FFFFFF"
        android:layout_marginRight="13dp"
        android:gravity="center_vertical">

        <com.netease.nim.avchatkit.common.imageview.CircleImageView
            android:id="@+id/iv_rade_image"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_marginEnd="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginStart="8dp"
            app:civ_border_color="#fffed470"
            app:civ_border_width="2dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="@+id/constraintLayout"
            app:srcCompat="@drawable/logo" />



        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="17dp"
            android:orientation="vertical">

            <TextView
                android:id="@+id/tv_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="2dp"
                android:text="红聊科技
"               android:textColor="#000000"
                android:textSize="15sp"/>

            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">


            <TextView
                android:id="@+id/tv_read_yuer"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#666666"
                android:textSize="13sp"
                android:text="剩余"/>

            <TextView

                android:id="@+id/tv_money"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                 android:layout_toRightOf="@+id/tv_read_yuer"
                android:text="1000"
                android:textSize="13sp"
                android:textColor="#ff533c"/>

                <TextView

                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_toRightOf="@+id/tv_money"
                    android:text="元"
                    android:textSize="13sp"
                    android:textColor="#666666"/>

            <TextView
                android:id="@+id/tv_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="90dp"
                 android:textSize="13sp"
                android:textColor="#666666"
                android:text="剩余"/>

            <TextView
                android:id="@+id/tv_nuber"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/tv_view"
                android:text="10"
                android:textSize="13sp"
                android:textColor="#666666"/>

                <TextView

                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_toRightOf="@+id/tv_nuber"
                    android:text="个红包"
                    android:textSize="13sp"
                    android:textColor="#666666"/>


            </RelativeLayout>

            <Button
                android:id="@+id/bt_come"
                android:layout_width="73dp"
                android:layout_height="27dp"
                android:layout_marginLeft="170dp"
                android:layout_marginTop="-22dp"
                android:textColor="#504f4f"
                style="?android:attr/borderlessButtonStyle"
                android:background="@drawable/shape_backgroundd"
                android:text="去抢"/>

        </LinearLayout>




    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dip"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="13dp"
        android:background="#E0E0E0"/>


</LinearLayout>
原文地址:https://www.cnblogs.com/monkey0928/p/9864139.html