Android-异步加载

这篇博客详细介绍了如何在Android应用中实现异步加载ListView数据。首先,创建了item_layout.xml布局文件用于显示列表项,接着在activity_main.xml中添加ListView。接着,定义了NewsBean.java来封装数据,并在MainActivity.java中实现数据的异步获取和ListView的填充,确保UI的流畅性。

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

创建一个item_layout.xml布局

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

    <LinearLayout
        android:id="@+id/iv_icon"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <ImageView
            android:layout_width="64dp"
            android:layout_height="64dp"
            android:src="@mipmap/ic_launcher"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingLeft="4dp"
            android:orientation="vertical">

            <TextView
                android:id="@+id/tv_title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:textSize="15sp"
                android:maxLines="1"
                android:text="Title"/>
        <TextView
                android:id="@+id/tv_content"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="10sp"
                android:maxLines="3"
                android:text="Content"/>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

效果图:
这里写图片描述

然后在主界面activity_main.xml中创建一个ListView

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

    <ListView
        android:id="@+id/lv_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></ListView>
</RelativeLayout>

布局完成了!然后就是要完成获取数据

创建一个NewsBean.java

public class NewsBean {

    public String newsIconUrl;
    public String newsTitle;
    public String newsContent;

}

在主函数MainActivity.java

package com.example.huang.myapplication;

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

//创建mListView获取ListView布局
    private ListView mListView;
//网址
    private static String URL = "http://www.imooc.com/api/teacher?type=4&num=30";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mListView = (ListView) findViewById(R.id.lv_main);

        new NewsAsyncTask().execute(URL);
    }


    private List<NewsBean> getJsonData(String url) {

        List<NewsBean> newsBeanList = new ArrayList<>();
        //获取网页返回的字符串
        String jsonString = null;
        try {
            //调用readStream方法读取网页url的信息返回到jsonString
            jsonString = readStream(new URL(url).openStream());
            Log.d("hj",jsonString);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return newsBeanList;
    }

    //读取网页返回信息
    private String readStream(InputStream is) {
        InputStreamReader isr;
        String result = "";
        try {
            String line = "";
            //字节流转化为字符流
            isr = new InputStreamReader(is, "utf-8");
            //字符流利用BufferedReader读取出来
            BufferedReader br = new BufferedReader(isr);

            //如果不为内容空,读取到result中
            while ((line=br.readLine()) != null) {
                result += line;
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return result;

    }

    //AsyncTask<传入一个String网址,不需要记录中间过程Void,返回NewsBean的集合>
    class NewsAsyncTask extends AsyncTask<String, Void, List<NewsBean>> {
        @Override
        protected List<NewsBean> doInBackground(String... strings) {
            //strings就一个,传进来的网址
            return getJsonData(strings[0]);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值