码迷,mamicode.com
首页 > 其他好文 > 详细

AsyncTask异步;ListView列表展示数据

时间:2017-12-27 15:38:28      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:UI   buffered   save   getview   int   internet   数据   ase   exce   

AsyncTask异步ListView列表展示数据

libs文件夹下加入jar包(导包): gson 

 

=======================================

 

添加权限:

 

网络请求权限:INTERNET
<uses-permission android:name="android.permission.INTERNET"/>
网络判断权限:ACCESS_NETWORK_STATE
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

 

===========================================================

 

布局:

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

 

json解析类:https://api.tianapi.com/wxnew/?key=32b83b189e5e5e520db1612a83d82033&num=10
============================================================
主要方法:
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import com.google.gson.Gson;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ListView listview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listview = findViewById(R.id.list_view);
MyTask myTask = new MyTask();
myTask.execute("https://api.tianapi.com/wxnew/?key=32b83b189e5e5e520db1612a83d82033&num=10");
}
class MyTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... strings) {
StringBuilder builder = new StringBuilder();
try {
URL url = new URL(strings[0]);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setConnectTimeout(5000);
urlConnection.setReadTimeout(5000);
urlConnection.setRequestMethod("GET");
if (urlConnection.getResponseCode() == 200) {
InputStream inputStream = urlConnection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String str;
while ((str = reader.readLine()) != null) {
builder.append(str);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return builder.toString();
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Gson gson = new Gson();
Result result = gson.fromJson(s, Result.class);
List<Result.NewslistBean> data = result.getNewslist();
MyAdapter myAdapter = new MyAdapter(data, MainActivity.this);
listview.setAdapter(myAdapter);
}
}
}

=========================================================
MyAdapter 适配器方法:
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
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 java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
class MyAdapter extends BaseAdapter {
private List<Result.NewslistBean> data;
private Context context;
public MyAdapter(List<Result.NewslistBean> data, Context context) {
this.data = data;
this.context = context;
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int i) {
return data.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder holder;
if (view == null) {
view= LayoutInflater.from(context).inflate(R.layout.item,null);
holder = new ViewHolder();
holder.name=view.findViewById(R.id.name);
holder.img=view.findViewById(R.id.img);
view.setTag(holder);
}else {
holder = (ViewHolder) view.getTag();
}
holder.name.setText(data.get(i).getDescription());
new MyTask(holder.img).execute(data.get(i).getPicUrl());
return view;
}
class ViewHolder {
public TextView name;
public ImageView img;
}
class MyTask extends AsyncTask<String, Void, Bitmap> {
private ImageView img;
public MyTask(ImageView img) {
this.img = img;
}
@Override
protected Bitmap doInBackground(String... strings) {
Bitmap bitmap = null;
try {
URL url = new URL(strings[0]);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setConnectTimeout(5000);
urlConnection.setReadTimeout(5000);
urlConnection.setRequestMethod("GET");
if (urlConnection.getResponseCode() == 200) {
InputStream inputStream = urlConnection.getInputStream();
bitmap = BitmapFactory.decodeStream(inputStream);
}
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
img.setImageBitmap(bitmap);
}
}
}
-------------------------------------------
R.layout.item
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/img"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="10dp"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="16dp" />
<TextView
android:id="@+id/name"
android:layout_width="200dp"
android:layout_height="50dp"
android:layout_alignTop="@+id/img"
android:layout_toEndOf="@+id/img"
android:layout_toRightOf="@+id/img"
tools:layout_editor_absoluteX="80dp"
tools:layout_editor_absoluteY="16dp" />
</RelativeLayout>
==================================================================
 

AsyncTask异步;ListView列表展示数据

标签:UI   buffered   save   getview   int   internet   数据   ase   exce   

原文地址:https://www.cnblogs.com/shenhuiran/p/8125669.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!