• 手机版

    扫码体验手机版

  • 微信公众号

    扫码关注公众号

国内首家协议开发

软芯音视解码保护平台

在线
客服

发布
需求

在线
聊天

天盟
APP

天盟APP下载

关注
微信

微信扫一扫访问
顶部

为什么listview没显示数据

异步加载package com.tianmg.AsynTask;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.MalformedURLException;import java.net.URI;import java.net.URL;import java.util.ArrayList;import java.util.List;import org.json.JSONArray;import org.json.JSONException;import org.json.JSONObject;import com.example.asyntask.R;import android.app.Activity;import android.os.AsyncTask;import android.os.Bundle;import android.util.Log;import android.widget.ListView;public class AsyntaskOfListView extends Activity {    ListView listview;    static String url="http://www.tianmg.com/api/teacher?type=4&num=30";    @Override    protected void onCreate(Bundle savedInstanceState) {        // TODO Auto-generated method stub        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_listview);        listview=(ListView) findViewById(R.id.listview);        AnysTask task=new AnysTask();        task.execute(url);            }public List getJsongetJson(String url){    List newsBeanList=new ArrayList();    try {        String jdata=readStream(new URL(url).openStream());        JSONObject jsonObject;        newsBean bean;        try {            jsonObject=new JSONObject(jdata);            JSONArray jsonArray=jsonObject.getJSONArray("data");            for (int i = 0; i < jsonArray.length(); i++) {                bean=new newsBean();                jsonObject=jsonArray.getJSONObject(i);                bean.image=jsonObject.getString("picSmall");                bean.title=jsonObject.getString("name");                bean.content=jsonObject.getString("description");                newsBeanList.add(bean);            }        } catch (JSONException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    } catch (MalformedURLException e) {        // TODO Auto-generated catch block        e.printStackTrace();    } catch (IOException e) {        // TODO Auto-generated catch block        e.printStackTrace();    }    return newsBeanList;    }public String readStream(InputStream is){    String result = null;    try {        String line="";        InputStreamReader isr=new InputStreamReader(is, "utf-8");        BufferedReader br=new BufferedReader(isr);        while((line=br.readLine())!=null){            result+=line;        }            } catch (UnsupportedEncodingException e) {        // TODO Auto-generated catch block        e.printStackTrace();    } catch (IOException e) {        // TODO Auto-generated catch block        e.printStackTrace();    }    return result;    }    class AnysTask extends AsyncTask{    @Override    protected List doInBackground(String... params) {        // TODO Auto-generated method stub                return getJsongetJson(params[0]);    }    @Override    protected void onPostExecute(List result) {        // TODO Auto-generated method stub        super.onPostExecute(result);        NewsAdapter adapter=new NewsAdapter(result,AsyntaskOfListView.this);        listview.setAdapter(adapter);    }    }}适配器package com.tianmg.AsynTask;//适配器import java.util.List;import com.example.asyntask.R;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;public class NewsAdapter extends BaseAdapter{    private List newsbeanlist;    private LayoutInflater inflater;    public NewsAdapter(List newsbeanlist1,Context context){        this.newsbeanlist=newsbeanlist1;        inflater=LayoutInflater.from(context);            }    @Override    public int getCount() {        // TODO Auto-generated method stub        return newsbeanlist.size();    }    @Override    public Object getItem(int arg0) {        // TODO Auto-generated method stub        return newsbeanlist.get(arg0);    }    @Override    public long getItemId(int arg0) {        // TODO Auto-generated method stub        return arg0;    }    @Override    public View getView(int arg0, View convertview, ViewGroup arg2) {        // TODO Auto-generated method stub        ViewHolder viewHolder=null;        if(convertview==null){            viewHolder=new ViewHolder();            convertview=inflater.inflate(R.layout.item, null);            viewHolder.image=(ImageView) convertview.findViewById(R.id.imageview);            viewHolder.title=(TextView) convertview.findViewById(R.id.title);            viewHolder.content=(TextView) convertview.findViewById(R.id.content);            convertview.setTag(viewHolder);        }        else{            viewHolder=(ViewHolder) convertview.getTag();        }        viewHolder.image.setImageResource(R.drawable.ic_launcher);        viewHolder.title.setText(newsbeanlist.get(arg0).title);        viewHolder.content.setText(newsbeanlist.get(arg0).content);        return convertview;    }    class ViewHolder{        public TextView title;        public TextView content;        public ImageView image;    }}定义的泛型package com.tianmg.AsynTask;public class newsBean {    public String image;    public String title;    public String content;}

免责声明:本内容仅代表回答会员见解不代表天盟观点,请谨慎对待。

版权声明:作者保留权利,不代表天盟立场。

使用道具 举报

全部参与1

readStream 这个方法里的
  String result = null; 改成String result = "";
就可以了。获开的数据时候,null也会被加进去。

使用道具 举报

发新帖

发布任务需求已有1031167位用户正在使用天盟网服务

发布分类: *
任务预算: *
需求内容: *
手机号码: *
任务商家报价为
  • 预算价 :
  • 成交价 :
  • 完工期 :
  • 质保期 :

* 最终任务项目以服务商报价、双方协商为准!