ListView适配器

本文中,listview有两种布局,但是因为这两种布局比较简单,就没有用XListView,其中listView的条目适配中还牵扯到了json串

lv_item的xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_marginLeft="10dp"
    >
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="25dp"
    android:orientation="horizontal"
    android:gravity="center_vertical"
    >
    <ImageView
        android:id="@+id/lv_headerimg"
        android:layout_width="25dp"
        android:layout_height="match_parent"
        android:src="@mipmap/ic_launcher"
        />
    <TextView
        android:id="@+id/lv_itemauthor"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="某人的帖子"
        />
</LinearLayout>
    <ImageView
        android:id="@+id/lv_itembigimg"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:src="@mipmap/ic_launcher"
        android:visibility="gone"
        />
    <TextView
        android:id="@+id/lv_itemtitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="标题"
        android:textSize="20dp"
        />
    <TextView
        android:id="@+id/lv_itemcontent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="内容"
        android:textSize="15dp"
        />
</LinearLayout>
主Activity中,onCreate()中,获得lv的适配数据 getLvData();

 private void getLvData() {
        HttpUtils httpUtils=new HttpUtils();
        httpUtils.send(HttpRequest.HttpMethod.GET, path, new RequestCallBack<String>() {

            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                //获得json串
                String json = responseInfo.result;
                Message msg=Message.obtain();
                msg.what=2;
                Gson gson=new Gson();
               LvBean lvData=gson.fromJson(json,LvBean.class);
                lvDataList = lvData.items;
                handler.sendMessage(msg);
            }

            @Override
            public void onFailure(HttpException e, String s) {

            }
        });
    }
在这里请求数据用的是HttpUtils,用studio时用这个工具类,需要在build.gradlez中配置

android{
    useLibrary 'org.apache.http.legacy'
}

 //Handler
    Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 1:
                    currentItem++;
                    vp.setCurrentItem(currentItem);
                    break;
                case 2:
                   //适配器
                    lv.setAdapter(new BaseAdapter() {
                        @Override
                        public int getCount() {
                            return lvDataList.size();
                        }

                        @Override
                        public Object getItem(int i) {
                            return lvDataList.get(i);
                        }

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

                        @Override
                        public View getView(int i, View view, ViewGroup viewGroup) {
                            ViewHolder vh;
                            if(view==null){
                                vh=new ViewHolder();
                                view=View.inflate(MainActivity.this,R.layout.lv_item,null);
                                vh.headerImg=(ImageView)view.findViewById(R.id.lv_headerimg);
                                vh.author=(TextView)view.findViewById(R.id.lv_itemauthor);
                                vh.bigImg=(ImageView)view.findViewById(R.id.lv_itembigimg);
                                vh.title=(TextView)view.findViewById(R.id.lv_itemtitle);
                                vh.content=(TextView)view.findViewById(R.id.lv_itemcontent);
                                view.setTag(vh);
                            }else{
                                vh= (ViewHolder) view.getTag();
                            }
                            //给控件设置值
                            ImageLoader imageLoader=ImageLoader.getInstance();
                            imageLoader.displayImage("http://img.dxycdn.com/avatars/120/"+lvDataList.get(i).infoAvatar,vh.headerImg);
                            //又得到一串json串,同样转成对象
                            String content = lvDataList.get(i).content;
                            Gson gson=new Gson();
                            ContentBean contentBean=gson.fromJson(content,ContentBean.class);
                            vh.author.setText(contentBean.name+"通讯员");
                            if(TextUtils.isEmpty(contentBean.url)){
                                vh.bigImg.setVisibility(View.GONE);
                            }else{
                                vh.bigImg.setVisibility(View.VISIBLE);
                                ImageLoader imageLoader1=ImageLoader.getInstance();
                                imageLoader1.displayImage("http://res.dxycdn.com/upload"+contentBean.url,vh.bigImg);
                            }
                            vh.title.setText(contentBean.subject);
                            vh.content.setText(contentBean.body);

                            return view;
                        }

                        class ViewHolder{
                            ImageView headerImg;
                            TextView author;
                            ImageView bigImg;
                            TextView title;
                            TextView content;
                        }
                    });

                    break;
            }
        }
    };
listView中每一个子条目代表一个view,适配每一个条目都会去走一遍getView()方法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值