c测试

本文深入探讨了在Android应用开发中如何配置ListView的适配器,并详细讲解了如何通过MyAdapter类实现列表项的数据绑定。同时,文章还介绍了如何使用MyAsyncTask类创建异步任务来执行网络操作,获取数据并更新UI,确保应用程序的响应性和效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

测试1 配置适配器

package com.example.app2;

import android.content.Context;
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.bumptech.glide.Glide;

import java.util.List;

class MyAdapter extends BaseAdapter {

    private List<NewsBean.TopStoriesBean> lists;
    private Context context;

    //构造传参
    public MyAdapter(List<NewsBean.TopStoriesBean> lists, Context context) {
        this.lists = lists;
        this.context = context;
    }

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

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //封装对象
        ViewHolder holder = null;

        //判断是否复用条目
        if(convertView == null){//非复用
            //创建对象
            holder = new ViewHolder();
            //设置自定义布局
            convertView = LayoutInflater.from(context).inflate(R.layout.layout_items,null);
            //获取布局中的对象
            holder.textView_title = convertView.findViewById(R.id.text_item);
            holder.imageView_pic = convertView.findViewById(R.id.image_item);
            //添加标记
            convertView.setTag(holder);

        }else {//复用item
            //获取标记  (强转)
            holder = (ViewHolder) convertView.getTag();
        }
        //向控件中添加内容
        holder.textView_title.setText(lists.get(position).getTitle());
        //判断图片是否为空
        if(lists.get(position).getImage().isEmpty() || lists.get(position).getImage() == null){//图片为空
            //添加本地图片
            holder.imageView_pic.setImageResource(R.mipmap.ic_launcher);
        }else {//图片不为空
            //使用Gilde添加图片
            Glide.with(context).load(lists.get(position).getImage()).into(holder.imageView_pic);
        }
        //返回convertView
        return convertView;
    }



    //对条目布局控件的封装
    private class ViewHolder {

        private TextView textView_title;
        private ImageView imageView_pic;


    }
}

测试2 创建异步任务

package com.example.app2;

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

public class MyAsyncTask extends AsyncTask<String,Void, List<NewsBean.TopStoriesBean>> {

    private static final String TAG = "MyAsyncTask";

    //构造传参
    private List<NewsBean.TopStoriesBean> lists;
    private Context context;
    private MyAdapter adapter;
    private ProgressBar progressBar ;

    public MyAsyncTask(List<NewsBean.TopStoriesBean> lists, Context context, MyAdapter adapter, ProgressBar progressBar) {
        this.lists = lists;
        this.context = context;
        this.adapter = adapter;
        this.progressBar = progressBar;
    }

    @Override
    protected List<NewsBean.TopStoriesBean> doInBackground(String... strings) {

        //执行网络操作 获取json
        String json = Http_GetJdonFromNet.getJsonFromUrl(strings[0]);
        //使用原生解析
        try {
            JSONObject jsonObject = new JSONObject(json);
            List<NewsBean.TopStoriesBean> top_stories1 = JSON.parseObject(json, NewsBean.class).getTop_stories();
            JSONArray top_stories = jsonObject.getJSONArray("top_stories");



            Log.i(TAG, "doInBackground: "+top_stories1);

            //判断top_stories
            if(top_stories1 == null){
                //日志输出
                Log.i("TAG", "doInBackground:数据获取失败");
            }else {
                return top_stories1;
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(List<NewsBean.TopStoriesBean> topStoriesBeans) {
        super.onPostExecute(topStoriesBeans);
        progressBar.setVisibility(View.VISIBLE);


        //判断
        if(topStoriesBeans == null){
            //吐司失败
            Toast.makeText(context, "json数据加载失败", Toast.LENGTH_SHORT).show();
        }else {
            //加入集合
            lists.addAll(topStoriesBeans);
            //适配器刷新
            adapter.notifyDataSetChanged();
        }

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        progressBar.setVisibility(View.GONE);

    }
}

开启网络操作

package com.example.app2;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class Http_GetJdonFromNet {

    public static String getJsonFromUrl(String path){
        InputStream is = null;
        ByteArrayOutputStream baos = null;

        //开始进行网络操作
        try {
            //添加网址
            URL url = new URL(path);
            //获取连接
            HttpURLConnection connection = (HttpURLConnection)url.openConnection();
            //设置连接方式
            connection.setRequestMethod("GET");
            //设置连接超时和读取超时
            connection.setReadTimeout(3000);
            connection.setConnectTimeout(3000);
            //连接服务端
            connection.connect();

            //判断是否连接成功
            if(connection.getResponseCode() == 200){

                //设置流对象
                is = connection.getInputStream();
                baos = new ByteArrayOutputStream();

                //设置一个数字
                int len = 0;
                //创建一个字节数组
                byte[] bys = new byte[1024];

                //进行循环读取
                while ((len = is.read(bys)) != -1){
                    baos.write(bys,0,len);
                    //刷新
                    baos.flush();
                }

                //类型转换 获取json字符串
                String json = baos.toString();
                //返回字符串
                return json;

            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            //关闭流对象
            if( is != null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if( baos != null){
                try {
                    baos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值