无法看到的ListView [英] Not able to see ListView

查看:133
本文介绍了无法看到的ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有看到在ListView中我从网络服务中的所有数据的任何数据,但我没有看到这里面的ListView。
我得到的流量,我理解它从来没有进去getView()方法
请给我解释一下什么是错将在code和建议我最好的之一。

 公共类WeatherReport延伸活动{    ListView控件的ListView;
    GPS_Location gpsObj;
    BaseAdapter aAdapter;
    双经纬度;
    位图图像;    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.weather_report);
        ListView控件=(ListView控件)findViewById(R.id.listView);
        gpsObj =新GPS_Location(本);
        纬度= gpsObj.getLatitude();
        经度= gpsObj.getLongitude();
        的System.out.println(内部的onCreate());
        字符串URL =htt​​p://api.openweathermap.org/data/2.5/forecast/daily?lat=+纬度+&放大器;经度=+经度+&放大器; CNT = 14安培;模式= json的;
        新的getJSON()执行(URL);
        // listView.setAdapter(新BAdapter(本));
    }    私有类的getJSON扩展的AsyncTask<字符串,字符串,字符串> {
        字符串的cityName;
        串经纬度;
        ArrayList的< SingleRow>清单;
        ArrayList的<串GT;湿度;
        ArrayList的<串GT;速度;
        ArrayList的<串GT;天气;
        ArrayList的<串GT; tempMin;
        ArrayList的<串GT; tempMax;
        ArrayList的<串GT;描述;
        ArrayList的<&位图GT;图标;        @覆盖
        保护字符串doInBackground(字符串... PARAMS){
            字符串URL =参数[0];
            湿度=新的ArrayList<串GT;();
            速度=新的ArrayList<串GT;();
            天气=新的ArrayList<串GT;();
            tempMin =新的ArrayList<串GT;();
            tempMax =新的ArrayList<串GT;();
            图标=新的ArrayList<&位图GT;();
            说明=新的ArrayList<串GT;();
            字符串数据;
            尝试{
                HttpClient的hClient =新DefaultHttpClient();
                HTTPGET hGet =新HTTPGET(URL);
                ResponseHandler所<串GT; rHan​​dler =新BasicResponseHandler();
                数据= hClient.execute(hGet,rHandler);
                的System.out.println(内部doInBackground数据+数据);
                JSONObject的jObj =新的JSONObject(数据);
                的System.out.println(内部背景jObj+ jObj);
                的JSONObject的JSONObject = jObj.getJSONObject(城市);
                的cityName = jsonObject.getString(名称);
                JSONObject的jObjCoOrd = jsonObject.getJSONObject(坐标);
                纬度= jObjCoOrd.getString(纬度);
                的System.out.println(内部纬度+纬度);
                经度= jObjCoOrd.getString(LON);
                的System.out.println(内部经度+经度);                JSONArray jsonArray = jObj.getJSONArray(名单);
                的System.out.println(内部jsonObjList+ JSONArray),其中;
                的for(int i = 0; I< jsonArray.length();我++){
                    的JSONObject对象= jsonArray.getJSONObject(I)                    字符串humidityString = object.getString(湿度);
                    humidity.add(humidityString);
                    字符串speedString = object.getString(速度);
                    的System.out.println(内部speedString+ speedString);
                    speed.add(speedString);                    JSONArray weatherArray = object.getJSONArray(天气);
                    的JSONObject weatherObj = weatherArray.getJSONObject(0);                    字符串iconString = weatherObj.getString(图标);
                    最后弦乐iconURL =htt​​p://openweathermap.org/img/w/+ iconString +的.png;
                    图像=的getImage(iconURL);
                    icon.add(图片);
                    的System.out.println(内部图标+图标);
                    字符串descriptionString = weatherObj.getString(说明);
                    的System.out.println(内部descriptionString+ descriptionString);
                    description.add(descriptionString);                    JSONObject的TEMPOBJ = object.getJSONObject(TEMP);                    字符串minTemp = tempObj.getString(分);
                    的System.out.println(内部minTemp+ minTemp);
                    tempMin.add(minTemp);                    字符串maxTemp = tempObj.getString(最大);
                    的System.out.println(内部maxTemp+ maxTemp);
                    tempMax.add(maxTemp);
                }                aAdapter =新ListViewCustomAdapter(WeatherReport.this,
                        纬度,经度的cityName,湿度,速度,图标,说明tempMin,tempMax);
                listView.setAdapter(aAdapter);            }赶上(例外五){
                Log.w(错误,e.getMessage());
            }
            返回null;
        }        私人位图的getImage(字符串iconURL){
            在的InputStream = NULL;
            BMP位图= NULL;
            INT响应code = -1;
            尝试{                网址URL =新的URL(iconURL); //HTTP://192.xx.xx.xx/mypath/img1.jpg
                HttpURLConnection的CON =(HttpURLConnection类)url.openConnection();
                con.setDoInput(真);
                con.connect();
                响应code = con.getResponse code();
                如果(响应code == HttpURLConnection.HTTP_OK){
                    //下载
                    在= con.getInputStream();
                    BMP = BitmapFactory.de codeStream(中);
                    附寄();
                }            }赶上(例外前){
                Log.e(异常,ex.toString());
            }
            返回BMP;
        }        私有类ListViewCustomAdapter延伸BaseAdapter {
            上下文语境;
            公共LayoutInflater吹气;
            字符串的纬度,经度的cityName;
            的String []湿度,速度,内容描述,minTemp,maxTemp;
            ArrayList的<&位图GT;位图;
            公共ListViewCustomAdapter(上下文的背景下,字符串纬度,字符串经度,字符串的cityName,ArrayList的<弦乐>湿度,ArrayList的<弦乐>的速度,ArrayList的<位图>的形象,ArrayList的<弦乐>描述,ArrayList的<弦乐> tempMin,ArrayList的<弦乐> tempMax ){
                this.context =背景;
                this.Latitude =纬度;
                this.Longitude =经度;
                this.cityName =的cityName;
                this.bitmap =图标;
                this.Humidity = humidity.toArray(新的String [humidity.size()]);
                this.Speed​​ = speed.toArray(新的String [speed.size()]);
                this.Description1 = description.toArray(新的String [description.size()]);
                this.minTemp = tempMin.toArray(新的String [tempMin.size()]);
                this.maxTemp = tempMax.toArray(新的String [tempMax.size()]);
                this.inflater =(LayoutInflater)上下文
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                的System.out.println(内部ListViewCustomAdapter);
            }            @覆盖
            公众诠释的getCount(){
                返回0;
            }            @覆盖
            公共对象的getItem(int i)以{
                返回我;
            }            @覆盖
            众长getItemId(int i)以{
                返回我;
            }            私有类持有人{
                TextView的城市,MinTemp,MaxTemp,说明,速度,经纬度,湿度;
                ImageView的云;
            }            @覆盖
            公共查看getView(INT位置,查看convertView,父母的ViewGroup){
                持有人持有人= NULL;
                查看查看= convertView;
                的System.out.println(内部getView);
                如果(查看== NULL){
                    的System.out.println(内部如果getView);
                    持有人=新的持有人();
                    convertView = inflater.inflate(R.layout.weather_report_single_item,NULL);
                    holder.cloud =(ImageView的)findViewById(R.id.cloud);
                    holder.City =(的TextView)findViewById(R.id.cityName);
                    holder.MinTemp =(的TextView)findViewById(R.id.minTemp);
                    holder.MaxTemp =(的TextView)findViewById(R.id.maxTemp);
                    holder.Description =(的TextView)findViewById(R.id.weather);
                    holder.Speed​​ =(的TextView)findViewById(R.id.speed);
                    holder.Latitude =(的TextView)findViewById(R.id.latitude);
                    holder.Longitude =(的TextView)findViewById(R.id.longitude);
                    holder.Humidity =(的TextView)findViewById(R.id.humidity);                    convertView.setTag(保持器);
                }其他{
                    的System.out.println(内部其他getView);
                    支架=(座)convertView.getTag();
                    holder.City.setText(的cityName);
                    holder.Latitude.setText(纬度);
                    holder.Longitude.setText(经度);
                    holder.MaxTemp.setText(maxTemp [位置]);
                    holder.MinTemp.setText(minTemp [位置]);
                    holder.Speed​​.setText(速度[位置]);
                    holder.Description.setText(说明1 [位置]);
                    holder.cloud.setImageBitmap(bitmap.get(位置));
                }                返回convertView;
            }
        }
    }
}类SingleRow {
    串minTemp,maxTemp,速度,湿度;
    位图IMG;    SingleRow(字符串minTemp,字符串maxTemp,字符串的速度,字符串湿度,位图图片){        this.maxTemp = maxTemp;
        this.minTemp = minTemp;
        this.speed =速度;
        this.humidity =湿度;
        this.img = IMG;
    }}


解决方案

给出这将是显示的记录长度..给数组/集合长度的get计数法

检查低于code的适配器。补充意见还其中u犯错和建议,还

将异步任务onPostExecute方法列表视图适配器和所有。

EDITED
检查我HVE标有建议与 #### SUGGESTION

 公共类WeatherReport延伸活动{    ListView控件的ListView;
    // GPS_Location gpsObj;
    // BaseAdapter aAdapter;
    双经纬度;
    位图图像;    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.listview_);
        ListView控件=(ListView控件)findViewById(R.id.listdata);
        // gpsObj =新GPS_Location(本);
        / *
         *为了测试我GVE静态纬度长期价值,因为我天玑的hve您
         * GPS_Location类
         * /
        纬度= 23;
        经度= 73;
        的System.out.println(内部的onCreate());
        字符串URL =htt​​p://api.openweathermap.org/data/2.5/forecast/daily?lat=
                +纬度+&放大器;经度=+经度+&放大器; CNT = 14安培;模式= json的;
        新的getJSON()执行(URL);
        // listView.setAdapter(新BAdapter(本));
    }    私有类的getJSON扩展的AsyncTask<字符串,字符串,字符串> {
        字符串的cityName;
        串经纬度;
        ArrayList的< SingleRow>清单;
        ArrayList的<串GT;湿度;
        ArrayList的<串GT;速度;
        ArrayList的<串GT;天气;
        ArrayList的<串GT; tempMin;
        ArrayList的<串GT; tempMax;
        ArrayList的<串GT;描述;
        ArrayList的<串GT;图标;        @覆盖
        保护无效onPostExecute(字符串结果){
            // TODO自动生成方法存根
            super.onPostExecute(结果);
            listView.setAdapter(新ListViewCustomAdapter(WeatherReport.this,
                    纬度,经度的cityName,湿度,速度,图标,
                    描述,tempMin,tempMax));
        }        @覆盖
        保护字符串doInBackground(字符串... PARAMS){
            字符串URL =参数[0];
            湿度=新的ArrayList<串GT;();
            速度=新的ArrayList<串GT;();
            天气=新的ArrayList<串GT;();
            tempMin =新的ArrayList<串GT;();
            tempMax =新的ArrayList<串GT;();
            图标=新的ArrayList<串GT;();
            说明=新的ArrayList<串GT;();
            字符串数据;
            尝试{
                HttpClient的hClient =新DefaultHttpClient();
                HTTPGET hGet =新HTTPGET(URL);
                ResponseHandler所<串GT; rHan​​dler =新BasicResponseHandler();
                数据= hClient.execute(hGet,rHandler);
                的System.out.println(内部doInBackground数据+数据);
                JSONObject的jObj =新的JSONObject(数据);
                的System.out.println(内部背景jObj+ jObj);
                的JSONObject的JSONObject = jObj.getJSONObject(城市);
                的cityName = jsonObject.getString(名称);
                JSONObject的jObjCoOrd = jsonObject.getJSONObject(坐标);
                纬度= jObjCoOrd.getString(纬度);
                的System.out.println(内部纬度+纬度);
                经度= jObjCoOrd.getString(LON);
                的System.out.println(内部经度+经度);                JSONArray jsonArray = jObj.getJSONArray(名单);
                的System.out.println(内部jsonObjList+ JSONArray),其中;
                的for(int i = 0; I< jsonArray.length();我++){
                    的JSONObject对象= jsonArray.getJSONObject(I)                    字符串humidityString = object.getString(湿度);
                    humidity.add(humidityString);
                    字符串speedString = object.getString(速度);
                    的System.out.println(内部speedString+ speedString);
                    speed.add(speedString);                    JSONArray weatherArray = object.getJSONArray(天气);
                    的JSONObject weatherObj = weatherArray.getJSONObject(0);                    字符串iconString = weatherObj.getString(图标);
                    最后弦乐iconURL =htt​​p://openweathermap.org/img/w/
                            + iconString +巴纽;
                    // #### SUGGESTION                    //不要下载图像或文件这样的
                    //显示图像使用ImageLoader的延迟加载库
                    //可用                    // https://github.com/nostra13/Android-Universal-Image-Loader/tree/master/sample                    //评论图片下载相关的线之下,一旦
                    //检查                    //图像=的getImage(iconURL);
                    // icon.add(图片);                    //我HVE这里做其他的变化是converty位阵列
                    //列表的字符串,的hve图片网址无效位
                    icon.add(iconURL);
                    的System.out.println(内部图标+图标);
                    字符串descriptionString = weatherObj
                            .getString(说明);
                    的System.out.println(内部descriptionString
                            + descriptionString);
                    description.add(descriptionString);                    JSONObject的TEMPOBJ = object.getJSONObject(TEMP);                    字符串minTemp = tempObj.getString(分);
                    的System.out.println(内部minTemp+ minTemp);
                    tempMin.add(minTemp);                    字符串maxTemp = tempObj.getString(最大);
                    的System.out.println(内部maxTemp+ maxTemp);
                    tempMax.add(maxTemp);
                }                //如果妳做任何用户界面相关的变化在这里下面的异常会来
                //因此需要在移动onPostExecute UI变化                // android.view.ViewRootImpl $ CalledFromWrongThreadException:
                //只有创建一个视图可以分层原来的线程
                //触摸自己的看法。                // listView.setAdapter(新ListViewCustomAdapter(
                // WeatherReport.this,纬度,经度的cityName,
                //湿度,速度,图标,说明tempMin,tempMax));            }赶上(例外五){
                Log.w(错误,e.getMessage());
            }
            返回null;
        }        私人位图的getImage(字符串iconURL){
            在的InputStream = NULL;
            BMP位图= NULL;
            INT响应code = -1;
            尝试{                网址URL =新的URL(iconURL); //HTTP://192.xx.xx.xx/mypath/img1.jpg
                HttpURLConnection的CON =(HttpURLConnection类)网址
                        .openConnection();
                con.setDoInput(真);
                con.connect();
                响应code = con.getResponse code();
                如果(响应code == HttpURLConnection.HTTP_OK){
                    // 下载
                    在= con.getInputStream();
                    BMP = BitmapFactory.de codeStream(中);
                    附寄();
                }            }赶上(例外前){
                Log.e(异常,ex.toString());
            }            返回BMP;
        }        私有类ListViewCustomAdapter延伸BaseAdapter {
            上下文语境;
            公共LayoutInflater吹气;
            字符串的纬度,经度的cityName;
            的String []湿度,速度,内容描述,minTemp,maxTemp;
            ArrayList的<串GT;位图;
            INT totalDisplayDatasize = 0;            公共ListViewCustomAdapter(上下文的背景下,字符串纬度,
                    经度字符串,字符串的cityName,
                    ArrayList的<串GT;湿度的ArrayList<串GT;速度,
                    ArrayList的<串GT;图像,ArrayList的<串GT;描述,
                    ArrayList的<串GT; tempMin,ArrayList的<串GT; tempMax){
                this.context =背景;
                this.Latitude =纬度;
                this.Longitude =经度;
                this.cityName =的cityName;
                this.bitmap =图标;
                this.Humidity = humidity.toArray(新的String [humidity.size()]);
                this.Speed​​ = speed.toArray(新的String [speed.size()]);
                this.Description1 = description.toArray(新的String [简介
                        。尺寸()​​]);
                this.minTemp = tempMin.toArray(新的String [tempMin.size()]);
                this.maxTemp = tempMax.toArray(新的String [tempMax.size()]);
                // /建议:没有必要建立LayoutInflater实例
                //构造
                // this.inflater =(LayoutInflater)上下文
                // .getSystemService(Context.LAYOUT_INFLATER_SERVICE);                / **
                 *用于计数设置总数据长度
                 * * /
                如果(this.Humidity!= NULL)
                    totalDisplayDatasize = this.Humidity.length;
                的System.out.println(内部ListViewCustomAdapter);
            }            @覆盖
            公众诠释的getCount(){
                //这可能是其中一个原因,不显示listview.set
                //为计数总数据长度
                返回totalDisplayDatasize;
            }            @覆盖
            公共对象的getItem(int i)以{
                返回我;
            }            @覆盖
            众长getItemId(int i)以{
                返回我;
            }            私有类持有人{
                TextView的城市,MinTemp,MaxTemp,说明,速度,纬度,
                        经度,湿度;
                ImageView的云;
            }            @覆盖
            公共查看getView(INT位置,查看convertView,父母的ViewGroup){
                持有人持有人= NULL;
                查看查看= convertView;
                的System.out.println(内部getView);
                如果(查看== NULL){
                    的System.out.println(内部如果getView);
                    持有人=新的持有人();
                    // /没有必要创建LayoutInflater实例
                    //构造                    convertView = LayoutInflater.from(this.context).inflate(
                            R.layout.list_row_data,NULL);
                    //从convertview.findviewbyid,而不是创建实例
                    //直接使用findViewById
                    // holder.cloud =(ImageView的)convertView
                    // .findViewById(R.id.cloud);
                    // holder.City =(TextView中)convertView
                    // .findViewById(R.id.cityName);
                    holder.MinTemp =(TextView中)convertView
                            .findViewById(R.id.minTemp);
                    holder.MaxTemp =(TextView中)convertView
                            .findViewById(R.id.maxTemp);
                    holder.Description =(TextView中)convertView
                            .findViewById(R.id.weather);
                    holder.Speed​​ =(TextView中)convertView
                            .findViewById(R.id.speed);
                    holder.Latitude =(TextView中)convertView
                            .findViewById(R.id.latitude);
                    holder.Longitude =(TextView中)convertView
                            .findViewById(R.id.longitude);                    convertView.setTag(保持器);
                }其他{
                    的System.out.println(内部其他getView);
                    支架=(座)convertView.getTag();                } //这里的每一行设置的值                // holder.City.setText(的cityName);
                holder.Latitude.setText(纬度:+纬度);
                holder.Longitude.setText(龙+经度);
                holder.MaxTemp.setText(maxTemp:+ maxTemp [位置]);
                holder.MinTemp.setText(minTemp:+ minTemp [位置]);
                holder.Speed​​.setText(速度:+速度[位置]);
                holder.Description.setText(描述:
                        +说明1 [位置]);
                // #### SUGGESTION                //要显示的图像使用ImageLoader的,将下载的图像中
                //背景并显示
                // https://github.com/nostra13/Android-Universal-Image-Loader/tree/master/sample                // holder.cloud.setImageBitmap(bitmap.get(位置));
                返回convertView;
            }
        }
    }
}类SingleRow {
    串minTemp,maxTemp,速度,湿度;
    位图IMG;    SingleRow(字符串minTemp,字符串maxTemp,字符串的速度,字符串湿度,
            位图图片){        this.maxTemp = maxTemp;
        this.minTemp = minTemp;
        this.speed =速度;
        this.humidity =湿度;
        this.img = IMG;
    }}

list_row_data.xml

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent机器人:layout_margin =5DP
    机器人:layout_height =WRAP_CONTENT
    机器人:背景=#FFFFFF
    机器人:方向=垂直>    <的TextView
        机器人:ID =@ + ID /速度
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:文字颜色=#ffff8800/>    <的TextView
        机器人:ID =@ + ID /全天候
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:文字颜色=#ffff8800/>    <的TextView
        机器人:ID =@ + ID /纬度
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:文字颜色=#ffff8800/>    <的TextView
        机器人:ID =@ + ID /经度
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:文字颜色=#ffff8800/>    <的TextView
        机器人:ID =@ + ID / minTemp
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:文字颜色=#ffff8800/>    <的TextView
        机器人:ID =@ + ID / maxTemp
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:文字颜色=#ffff8800/>< / LinearLayout中>

listview_.xml

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT
    机器人:方向=垂直>    < ListView控件
        机器人:ID =@ + ID /的ListData
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =FILL_PARENT/>< / LinearLayout中>

i didn't see any data in listView i get all data from web-service but i didn't see it inside ListView. I get flow and i understand it never go inside getView() method Please explain me what's wrong going in that code and suggest me best-one.

public class WeatherReport extends Activity {

    ListView listView;
    GPS_Location gpsObj;
    BaseAdapter aAdapter;
    double Latitude, Longitude;
    Bitmap image;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.weather_report);
        listView = (ListView) findViewById(R.id.listView);
        gpsObj = new GPS_Location(this);
        Latitude = gpsObj.getLatitude();
        Longitude = gpsObj.getLongitude();
        System.out.println("Inside onCreate() ");
        String Url = "http://api.openweathermap.org/data/2.5/forecast/daily?lat=" + Latitude + "&lon=" + Longitude + "&cnt=14&mode=json";
        new getJSON().execute(Url);
        //  listView.setAdapter(new BAdapter(this));
    }

    private class getJSON extends AsyncTask<String, String, String> {
        String cityName;
        String Latitude, Longitude;
        ArrayList<SingleRow> list;
        ArrayList<String> humidity;
        ArrayList<String> speed;
        ArrayList<String> weather;
        ArrayList<String> tempMin;
        ArrayList<String> tempMax;
        ArrayList<String> description;
        ArrayList<Bitmap> icon;

        @Override
        protected String doInBackground(String... params) {
            String Url = params[0];
            humidity = new ArrayList<String>();
            speed = new ArrayList<String>();
            weather = new ArrayList<String>();
            tempMin = new ArrayList<String>();
            tempMax = new ArrayList<String>();
            icon = new ArrayList<Bitmap>();
            description = new ArrayList<String>();
            String data;
            try {
                HttpClient hClient = new DefaultHttpClient();
                HttpGet hGet = new HttpGet(Url);
                ResponseHandler<String> rHandler = new BasicResponseHandler();
                data = hClient.execute(hGet, rHandler);
                System.out.println("Inside doInBackground  data " + data);
                JSONObject jObj = new JSONObject(data);
                System.out.println("Inside Background jObj " + jObj);
                JSONObject jsonObject = jObj.getJSONObject("city");
                cityName = jsonObject.getString("name");
                JSONObject jObjCoOrd = jsonObject.getJSONObject("coord");
                Latitude = jObjCoOrd.getString("lat");
                System.out.println("Inside Latitude " + Latitude);
                Longitude = jObjCoOrd.getString("lon");
                System.out.println("Inside Longitude " + Longitude);

                JSONArray jsonArray = jObj.getJSONArray("list");
                System.out.println("Inside jsonObjList " + jsonArray);
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject object = jsonArray.getJSONObject(i);

                    String humidityString = object.getString("humidity");
                    humidity.add(humidityString);
                    String speedString = object.getString("speed");
                    System.out.println("Inside speedString " + speedString);
                    speed.add(speedString);

                    JSONArray weatherArray = object.getJSONArray("weather");
                    JSONObject weatherObj = weatherArray.getJSONObject(0);

                    String iconString = weatherObj.getString("icon");
                    final String iconURL = "http://openweathermap.org/img/w/" + iconString + ".png";
                    image = getImage(iconURL);
                    icon.add(image);
                    System.out.println("Inside icon " + icon);
                    String descriptionString = weatherObj.getString("description");
                    System.out.println("Inside descriptionString " + descriptionString);
                    description.add(descriptionString);

                    JSONObject tempObj = object.getJSONObject("temp");

                    String minTemp = tempObj.getString("min");
                    System.out.println("Inside minTemp " + minTemp);
                    tempMin.add(minTemp);

                    String maxTemp = tempObj.getString("max");
                    System.out.println("Inside maxTemp " + maxTemp);
                    tempMax.add(maxTemp);
                }

                aAdapter = new ListViewCustomAdapter(WeatherReport.this,
                        Latitude, Longitude, cityName, humidity, speed, icon, description, tempMin, tempMax);
                listView.setAdapter(aAdapter);

            } catch (Exception e) {
                Log.w("Error", e.getMessage());
            }
            return null;
        }

        private Bitmap getImage(String iconURL) {
            InputStream in = null;
            Bitmap bmp = null;
            int responseCode = -1;
            try {

                URL url = new URL(iconURL);//"http://192.xx.xx.xx/mypath/img1.jpg
                HttpURLConnection con = (HttpURLConnection) url.openConnection();
                con.setDoInput(true);
                con.connect();
                responseCode = con.getResponseCode();
                if (responseCode == HttpURLConnection.HTTP_OK) {
                    //download
                    in = con.getInputStream();
                    bmp = BitmapFactory.decodeStream(in);
                    in.close();
                }

            } catch (Exception ex) {
                Log.e("Exception", ex.toString());
            }


            return bmp;
        }

        private class ListViewCustomAdapter extends BaseAdapter {
            Context context;
            public LayoutInflater inflater;
            String Latitude, Longitude, cityName;
            String[] Humidity, Speed, Description1, minTemp, maxTemp ;
            ArrayList<Bitmap> bitmap;


            public ListViewCustomAdapter(Context context, String latitude, String longitude, String cityName, ArrayList<String> humidity, ArrayList<String> speed, ArrayList<Bitmap> image, ArrayList<String> description, ArrayList<String> tempMin, ArrayList<String> tempMax) {
                this.context = context;
                this.Latitude = latitude;
                this.Longitude = longitude;
                this.cityName = cityName;
                this.bitmap = icon;
                this.Humidity = humidity.toArray(new String[humidity.size()]);
                this.Speed = speed.toArray(new String[speed.size()]);
                this.Description1 = description.toArray(new String[description.size()]);
                this.minTemp = tempMin.toArray(new String[tempMin.size()]);
                this.maxTemp = tempMax.toArray(new String[tempMax.size()]);
                this.inflater = (LayoutInflater) context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                System.out.println("Inside ListViewCustomAdapter ");
            }

            @Override
            public int getCount() {
                return 0;
            }

            @Override
            public Object getItem(int i) {
                return i;
            }

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

            private class Holder {
                TextView City, MinTemp, MaxTemp, Description, Speed, Latitude, Longitude, Humidity;
                ImageView cloud;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                Holder holder= null;
                View view = convertView;
                System.out.println("Inside getView");
                if(view== null){
                    System.out.println("Inside if getView");
                    holder = new Holder();
                    convertView = inflater.inflate(R.layout.weather_report_single_item, null);
                    holder.cloud = (ImageView) findViewById(R.id.cloud);
                    holder.City = (TextView) findViewById(R.id.cityName);
                    holder.MinTemp= (TextView) findViewById(R.id.minTemp);
                    holder.MaxTemp= (TextView) findViewById(R.id.maxTemp);
                    holder.Description=(TextView) findViewById(R.id.weather);
                    holder.Speed =(TextView) findViewById(R.id.speed);
                    holder.Latitude = (TextView) findViewById(R.id.latitude);
                    holder.Longitude= (TextView) findViewById(R.id.longitude);
                    holder.Humidity= (TextView) findViewById(R.id.humidity);

                    convertView.setTag(holder);
                }else{
                    System.out.println("Inside else getView");
                    holder = (Holder) convertView.getTag();
                    holder.City.setText(cityName);
                    holder.Latitude.setText(Latitude);
                    holder.Longitude.setText(Longitude);
                    holder.MaxTemp.setText(maxTemp[position]);
                    holder.MinTemp.setText(minTemp[position]);
                    holder.Speed.setText(Speed[position]);
                    holder.Description.setText(Description1[position]);
                    holder.cloud.setImageBitmap(bitmap.get(position));
                }

                return convertView;
            }
        }
    }
}

class SingleRow {
    String minTemp, maxTemp, speed, humidity;
    Bitmap img;

    SingleRow(String minTemp, String maxTemp, String speed, String humidity, Bitmap img) {

        this.maxTemp = maxTemp;
        this.minTemp = minTemp;
        this.speed = speed;
        this.humidity = humidity;
        this.img = img;
    }

}

解决方案

give length of the records which is going to be display .. give array/collection length in get count method

Check the below code for adapter . Added comment also where u made mistakes and suggestions also

Set the listview adapter and all in async task onPostExecute method.

EDITED Check the suggestions i hve marked with with #### SUGGESTION

public class WeatherReport extends Activity {

    ListView listView;
    // GPS_Location gpsObj;
    // BaseAdapter aAdapter;
    double Latitude, Longitude;
    Bitmap image;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listview_);
        listView = (ListView) findViewById(R.id.listdata);
        // gpsObj = new GPS_Location(this);
        /*
         * For testing i gve static lat long values as i dnt hve your
         * GPS_Location class
         */
        Latitude = 23;
        Longitude = 73;
        System.out.println("Inside onCreate() ");
        String Url = "http://api.openweathermap.org/data/2.5/forecast/daily?lat="
                + Latitude + "&lon=" + Longitude + "&cnt=14&mode=json";
        new getJSON().execute(Url);
        // listView.setAdapter(new BAdapter(this));
    }

    private class getJSON extends AsyncTask<String, String, String> {
        String cityName;
        String Latitude, Longitude;
        ArrayList<SingleRow> list;
        ArrayList<String> humidity;
        ArrayList<String> speed;
        ArrayList<String> weather;
        ArrayList<String> tempMin;
        ArrayList<String> tempMax;
        ArrayList<String> description;
        ArrayList<String> icon;

        @Override
        protected void onPostExecute(String result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            listView.setAdapter(new ListViewCustomAdapter(WeatherReport.this,
                    Latitude, Longitude, cityName, humidity, speed, icon,
                    description, tempMin, tempMax));
        }

        @Override
        protected String doInBackground(String... params) {
            String Url = params[0];
            humidity = new ArrayList<String>();
            speed = new ArrayList<String>();
            weather = new ArrayList<String>();
            tempMin = new ArrayList<String>();
            tempMax = new ArrayList<String>();
            icon = new ArrayList<String>();
            description = new ArrayList<String>();
            String data;
            try {
                HttpClient hClient = new DefaultHttpClient();
                HttpGet hGet = new HttpGet(Url);
                ResponseHandler<String> rHandler = new BasicResponseHandler();
                data = hClient.execute(hGet, rHandler);
                System.out.println("Inside doInBackground  data " + data);
                JSONObject jObj = new JSONObject(data);
                System.out.println("Inside Background jObj " + jObj);
                JSONObject jsonObject = jObj.getJSONObject("city");
                cityName = jsonObject.getString("name");
                JSONObject jObjCoOrd = jsonObject.getJSONObject("coord");
                Latitude = jObjCoOrd.getString("lat");
                System.out.println("Inside Latitude " + Latitude);
                Longitude = jObjCoOrd.getString("lon");
                System.out.println("Inside Longitude " + Longitude);

                JSONArray jsonArray = jObj.getJSONArray("list");
                System.out.println("Inside jsonObjList " + jsonArray);
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject object = jsonArray.getJSONObject(i);

                    String humidityString = object.getString("humidity");
                    humidity.add(humidityString);
                    String speedString = object.getString("speed");
                    System.out.println("Inside speedString " + speedString);
                    speed.add(speedString);

                    JSONArray weatherArray = object.getJSONArray("weather");
                    JSONObject weatherObj = weatherArray.getJSONObject(0);

                    String iconString = weatherObj.getString("icon");
                    final String iconURL = "http://openweathermap.org/img/w/"
                            + iconString + ".png";
                    // #### SUGGESTION

                    // Do not Download images or files like this
                    // to display images use imageloader lazy loading library is
                    // available

                    // https://github.com/nostra13/Android-Universal-Image-Loader/tree/master/sample

                    // Comment image downloading related below line and once
                    // check

                    // image = getImage(iconURL);
                    // icon.add(image);

                    // here other change i hve made is converty bitmap array
                    // list to String which will hve images url not bitmap
                    icon.add(iconURL);
                    System.out.println("Inside icon " + icon);
                    String descriptionString = weatherObj
                            .getString("description");
                    System.out.println("Inside descriptionString "
                            + descriptionString);
                    description.add(descriptionString);

                    JSONObject tempObj = object.getJSONObject("temp");

                    String minTemp = tempObj.getString("min");
                    System.out.println("Inside minTemp " + minTemp);
                    tempMin.add(minTemp);

                    String maxTemp = tempObj.getString("max");
                    System.out.println("Inside maxTemp " + maxTemp);
                    tempMax.add(maxTemp);
                }

                // If u do any ui related changes here below exception will come
                // so need to move ui changes in onPostExecute

                // android.view.ViewRootImpl$CalledFromWrongThreadException:
                // Only the original thread that created a view hierarchy can
                // touch its views.

                // listView.setAdapter(new ListViewCustomAdapter(
                // WeatherReport.this, Latitude, Longitude, cityName,
                // humidity, speed, icon, description, tempMin, tempMax));

            } catch (Exception e) {
                Log.w("Error", e.getMessage());
            }
            return null;
        }

        private Bitmap getImage(String iconURL) {
            InputStream in = null;
            Bitmap bmp = null;
            int responseCode = -1;
            try {

                URL url = new URL(iconURL);// "http://192.xx.xx.xx/mypath/img1.jpg
                HttpURLConnection con = (HttpURLConnection) url
                        .openConnection();
                con.setDoInput(true);
                con.connect();
                responseCode = con.getResponseCode();
                if (responseCode == HttpURLConnection.HTTP_OK) {
                    // download
                    in = con.getInputStream();
                    bmp = BitmapFactory.decodeStream(in);
                    in.close();
                }

            } catch (Exception ex) {
                Log.e("Exception", ex.toString());
            }

            return bmp;
        }

        private class ListViewCustomAdapter extends BaseAdapter {
            Context context;
            public LayoutInflater inflater;
            String Latitude, Longitude, cityName;
            String[] Humidity, Speed, Description1, minTemp, maxTemp;
            ArrayList<String> bitmap;
            int totalDisplayDatasize = 0;

            public ListViewCustomAdapter(Context context, String latitude,
                    String longitude, String cityName,
                    ArrayList<String> humidity, ArrayList<String> speed,
                    ArrayList<String> image, ArrayList<String> description,
                    ArrayList<String> tempMin, ArrayList<String> tempMax) {
                this.context = context;
                this.Latitude = latitude;
                this.Longitude = longitude;
                this.cityName = cityName;
                this.bitmap = icon;
                this.Humidity = humidity.toArray(new String[humidity.size()]);
                this.Speed = speed.toArray(new String[speed.size()]);
                this.Description1 = description.toArray(new String[description
                        .size()]);
                this.minTemp = tempMin.toArray(new String[tempMin.size()]);
                this.maxTemp = tempMax.toArray(new String[tempMax.size()]);
                // /suggestion : No need to create LayoutInflater instance in
                // constructor
                // this.inflater = (LayoutInflater) context
                // .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

                /**
                 * set total data length for count
                 * */
                if (this.Humidity != null)
                    totalDisplayDatasize = this.Humidity.length;
                System.out.println("Inside ListViewCustomAdapter ");
            }

            @Override
            public int getCount() {
                // this could be one of the reason for not showing listview.set
                // total data length for count
                return totalDisplayDatasize;
            }

            @Override
            public Object getItem(int i) {
                return i;
            }

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

            private class Holder {
                TextView City, MinTemp, MaxTemp, Description, Speed, Latitude,
                        Longitude, Humidity;
                ImageView cloud;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                Holder holder = null;
                View view = convertView;
                System.out.println("Inside getView");
                if (view == null) {
                    System.out.println("Inside if getView");
                    holder = new Holder();
                    // / No need to create LayoutInflater instance in
                    // constructor

                    convertView = LayoutInflater.from(this.context).inflate(
                            R.layout.list_row_data, null);
                    // Create instance from convertview.findviewbyid instead of
                    // directly using findViewById
                    // holder.cloud = (ImageView) convertView
                    // .findViewById(R.id.cloud);
                    // holder.City = (TextView) convertView
                    // .findViewById(R.id.cityName);
                    holder.MinTemp = (TextView) convertView
                            .findViewById(R.id.minTemp);
                    holder.MaxTemp = (TextView) convertView
                            .findViewById(R.id.maxTemp);
                    holder.Description = (TextView) convertView
                            .findViewById(R.id.weather);
                    holder.Speed = (TextView) convertView
                            .findViewById(R.id.speed);
                    holder.Latitude = (TextView) convertView
                            .findViewById(R.id.latitude);
                    holder.Longitude = (TextView) convertView
                            .findViewById(R.id.longitude);

                    convertView.setTag(holder);
                } else {
                    System.out.println("Inside else getView");
                    holder = (Holder) convertView.getTag();

                }// set values here for each row

                // holder.City.setText(cityName);
                holder.Latitude.setText("Lat : " + Latitude);
                holder.Longitude.setText("Long : " + Longitude);
                holder.MaxTemp.setText("maxTemp : " + maxTemp[position]);
                holder.MinTemp.setText("minTemp : " + minTemp[position]);
                holder.Speed.setText("Speed : " + Speed[position]);
                holder.Description.setText("Description : "
                        + Description1[position]);
                // #### SUGGESTION

                // TO display image use ImageLoader which will download image in
                // background and will display
                // https://github.com/nostra13/Android-Universal-Image-Loader/tree/master/sample

                // holder.cloud.setImageBitmap(bitmap.get(position));
                return convertView;
            }
        }
    }
}

class SingleRow {
    String minTemp, maxTemp, speed, humidity;
    Bitmap img;

    SingleRow(String minTemp, String maxTemp, String speed, String humidity,
            Bitmap img) {

        this.maxTemp = maxTemp;
        this.minTemp = minTemp;
        this.speed = speed;
        this.humidity = humidity;
        this.img = img;
    }

}

list_row_data.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_margin="5dp"
    android:layout_height="wrap_content"
    android:background="#ffffff"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/speed"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#ffff8800" />

    <TextView
        android:id="@+id/weather"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#ffff8800" />

    <TextView
        android:id="@+id/latitude"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#ffff8800" />

    <TextView
        android:id="@+id/longitude"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#ffff8800" />

    <TextView
        android:id="@+id/minTemp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#ffff8800" />

    <TextView
        android:id="@+id/maxTemp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#ffff8800" />

</LinearLayout>

listview_.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="wrap_content"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/listdata"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</LinearLayout>

这篇关于无法看到的ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆