rikao3将数据图片展示到listview上

来源:互联网 发布:人工智能手机芯片 编辑:程序博客网 时间:2024/06/14 16:37

//activity_main

<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">


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


//MainActivity

public class MainActivity extends Activity {


    private ListView lv;
    private static final String JSON_URL = "https://www.toutiao.com/api/pc/focus/";
    private HttpURLConnection connection;
    private URL url;
    String data = "";
    private List<Feed> feed;
    private Handler handler = new Handler(){


public void handleMessage(android.os.Message msg) {
    if (msg.what == 0) {
Gson gson = new Gson();
UserBean fromJson = gson.fromJson(data, UserBean.class);
feed = fromJson.getData().getPc_feed_focus();
Myadapter myadapter = new Myadapter(feed, MainActivity.this);
lv.setAdapter(myadapter);
}
    };
    };
    
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        lv = (ListView) findViewById(R.id.listview);
        
        new Thread(){
        public void run() {
        try {
url = new URL(JSON_URL);
connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
int responseCode = connection.getResponseCode();
if (responseCode == 200) {
InputStream inputStream = connection.getInputStream();
byte[] b = new byte[1024];
int len = 0;
while((len = inputStream.read(b))!=-1){
String str = new String(b, 0, len);
data += str;
}
Log.i("TAG", data);
handler.sendMessage(handler.obtainMessage(0,data));
}
        } catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
        };
        }.start();
    }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}


//UserBean

public class User {


private String display_url;
private String group_id;
private String has_image;
private String has_video;
private String image_url;
private String media_url;
private String title;
public String getDisplay_url() {
return display_url;
}
public void setDisplay_url(String display_url) {
this.display_url = display_url;
}
public String getGroup_id() {
return group_id;
}
public void setGroup_id(String group_id) {
this.group_id = group_id;
}
public String getHas_image() {
return has_image;
}
public void setHas_image(String has_image) {
this.has_image = has_image;
}
public String getHas_video() {
return has_video;
}
public void setHas_video(String has_video) {
this.has_video = has_video;
}
public String getImage_url() {
return image_url;
}
public void setImage_url(String image_url) {
this.image_url = image_url;
}
public String getMedia_url() {
return media_url;
}
public void setMedia_url(String media_url) {
this.media_url = media_url;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public User(String display_url, String group_id, String has_image,
String has_video, String image_url, String media_url, String title) {
super();
this.display_url = display_url;
this.group_id = group_id;
this.has_image = has_image;
this.has_video = has_video;
this.image_url = image_url;
this.media_url = media_url;
this.title = title;
}

}


//UserClass

public class UserBean {


private String message;
private Data data;


public String getMessage() {
return message;
}




public void setMessage(String message) {
this.message = message;
}




public Data getData() {
return data;
}




public void setData(Data data) {
this.data = data;
}




class Data{
private List<Feed> pc_feed_focus;


public List<Feed> getPc_feed_focus() {
return pc_feed_focus;
}




public void setPc_feed_focus(List<Feed> pc_feed_focus) {
this.pc_feed_focus = pc_feed_focus;
}




class Feed{
private String display_url;
private String group_id;
private String has_image;
private String has_video;
private String image_url;
private String media_url;
private String title;
public String getDisplay_url() {
return display_url;
}
public void setDisplay_url(String display_url) {
this.display_url = display_url;
}
public String getGroup_id() {
return group_id;
}
public void setGroup_id(String group_id) {
this.group_id = group_id;
}
public String getHas_image() {
return has_image;
}
public void setHas_image(String has_image) {
this.has_image = has_image;
}
public String getHas_video() {
return has_video;
}
public void setHas_video(String has_video) {
this.has_video = has_video;
}
public String getImage_url() {
return image_url;
}
public void setImage_url(String image_url) {
this.image_url = image_url;
}
public String getMedia_url() {
return media_url;
}
public void setMedia_url(String media_url) {
this.media_url = media_url;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}

}
}
}


//将数据,图片获取

public class NetWorkUtils {

public Bitmap getBitmapByGet(String img_url){
Bitmap bm = null;
try {
URL url = new URL(img_url);//JSON_URL表示的是图片的url
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();//�õ�HttpsURLConnection��������
urlConn.setConnectTimeout(5000);//�����������ʱ��
urlConn.setReadTimeout(5000);//���ö�ȡʱ��
int responseCode = urlConn.getResponseCode();//�õ���Ӧ��
if(responseCode == 200){//200��ʾ��Ӧ�ɹ�
InputStream inputStream = urlConn.getInputStream();//������Ӧ�����������

bm = BitmapFactory.decodeStream(inputStream);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return bm;
}

public Bitmap getBitmapByHttpClientGet(String imgUrl){
Bitmap bm = null;
HttpClient httpClient = new DefaultHttpClient();//用来执行get请求的
HttpGet get = new HttpGet(imgUrl);
try {
HttpResponse res = httpClient.execute(get);//执行get请求
int rsCode = res.getStatusLine().getStatusCode();//得到响应的结果码
if(rsCode == 200){
HttpEntity entity = res.getEntity();//得到服务器响应的结果

bm = BitmapFactory.decodeStream(entity.getContent());
}

} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


return bm;
}


}


//适配器

public class Myadapter extends BaseAdapter{


private List<Feed> list;
private Context context;

public Myadapter(List<Feed> list, Context context) {
super();
this.list = list;
this.context = context;
}


@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}


@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}


@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final Viewholder viewholder;
if (convertView == null) {
convertView = View.inflate(context, R.layout.item, null);
viewholder = new Viewholder();
viewholder.text1 = (TextView) convertView.findViewById(R.id.text1);
viewholder.text2 = (TextView) convertView.findViewById(R.id.text2);
viewholder.text3 = (TextView) convertView.findViewById(R.id.text3);
viewholder.text4 = (TextView) convertView.findViewById(R.id.text4);
viewholder.text5 = (TextView) convertView.findViewById(R.id.text5);
viewholder.text6 = (TextView) convertView.findViewById(R.id.text6);
viewholder.text7 = (TextView) convertView.findViewById(R.id.text7);
viewholder.img = (ImageView) convertView.findViewById(R.id.img);
convertView.setTag(viewholder);
}else{
viewholder = (Viewholder) convertView.getTag();
}

viewholder.text1.setText(list.get(position).getDisplay_url());
viewholder.text2.setText(list.get(position).getGroup_id());
viewholder.text3.setText(list.get(position).getHas_image());
viewholder.text4.setText(list.get(position).getHas_video());
viewholder.text5.setText(list.get(position).getImage_url());
viewholder.text6.setText(list.get(position).getMedia_url());
viewholder.text7.setText(list.get(position).getTitle());
//给图片赋值
//得到图片完整路径
final String imgUrl = "http:"+list.get(position).getImage_url();
//启动异步任务
new AsyncTask<String, Integer, Bitmap>() {
@Override
protected Bitmap doInBackground(String... params) {
// TODO Auto-generated method stub
Bitmap bm = null;
// try {
// URL url = new URL(imgUrl);
// HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// connection.setConnectTimeout(5000);
// connection.setReadTimeout(5000);
// int responseCode = connection.getResponseCode();
// if (responseCode == 200) {
// InputStream inputStream = connection.getInputStream();//得到图片信息
// bm = BitmapFactory.decodeStream(inputStream);//把图片的数据流(输入流)转换为Bitmap对象
// }
//         } catch (Exception e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }

bm = new NetWorkUtils().getBitmapByGet(imgUrl);
return bm;//把bm传到onPostExecute方法中
}

protected void onPostExecute(Bitmap result) {
viewholder.img.setImageBitmap(result);
};
}.execute();

return convertView;
}


class Viewholder{
private TextView text1;
private TextView text2;
private TextView text3;
private TextView text4;
private TextView text5;
private TextView text6;
private TextView text7;
private ImageView img;
}

}


//适配器数据texe

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    


    <TextView 
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="text01"
        android:layout_margin="10dp"/>
    <TextView 
        android:id="@+id/text2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="text02"
        android:layout_margin="10dp"/>
    <TextView 
        android:id="@+id/text3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="text03"
        android:layout_margin="10dp"/>
    <TextView 
        android:id="@+id/text4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="text04"
        android:layout_margin="10dp"/>
    <TextView 
        android:id="@+id/text5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="text05"
        android:layout_margin="10dp"/>
    <TextView 
        android:id="@+id/text6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="text06"
        android:layout_margin="10dp"/>
    <TextView 
        android:id="@+id/text7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="text07"
        android:layout_margin="10dp"/>
    <ImageView 
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        />
   
</LinearLayout>


原创粉丝点击