Skip to content Skip to sidebar Skip to footer

Not Able To See Listview

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 explai

Solution 1:

EDITED

First write

      @Override
        publicintgetCount() {
            return Humidity.length();;
        }

instead of

       @Override
        publicintgetCount() {
            return0;
        }

And you are setting value to row view in getview()'s else section set values after if else

        @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);
               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;
        }

Solution 2:

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

enter image description here

publicclassWeatherReportextendsActivity {

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

    @OverrideprotectedvoidonCreate(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() ");
        StringUrl="http://api.openweathermap.org/data/2.5/forecast/daily?lat="
                + Latitude + "&lon=" + Longitude + "&cnt=14&mode=json";
        newgetJSON().execute(Url);
        // listView.setAdapter(new BAdapter(this));
    }

    privateclassgetJSONextendsAsyncTask<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;

        @OverrideprotectedvoidonPostExecute(String result) {
            // TODO Auto-generated method stubsuper.onPostExecute(result);
            listView.setAdapter(newListViewCustomAdapter(WeatherReport.this,
                    Latitude, Longitude, cityName, humidity, speed, icon,
                    description, tempMin, tempMax));
        }

        @Overrideprotected String doInBackground(String... params) {
            StringUrl= params[0];
            humidity = newArrayList<String>();
            speed = newArrayList<String>();
            weather = newArrayList<String>();
            tempMin = newArrayList<String>();
            tempMax = newArrayList<String>();
            icon = newArrayList<String>();
            description = newArrayList<String>();
            String data;
            try {
                HttpClienthClient=newDefaultHttpClient();
                HttpGethGet=newHttpGet(Url);
                ResponseHandler<String> rHandler = newBasicResponseHandler();
                data = hClient.execute(hGet, rHandler);
                System.out.println("Inside doInBackground  data " + data);
                JSONObjectjObj=newJSONObject(data);
                System.out.println("Inside Background jObj " + jObj);
                JSONObjectjsonObject= jObj.getJSONObject("city");
                cityName = jsonObject.getString("name");
                JSONObjectjObjCoOrd= jsonObject.getJSONObject("coord");
                Latitude = jObjCoOrd.getString("lat");
                System.out.println("Inside Latitude " + Latitude);
                Longitude = jObjCoOrd.getString("lon");
                System.out.println("Inside Longitude " + Longitude);

                JSONArrayjsonArray= jObj.getJSONArray("list");
                System.out.println("Inside jsonObjList " + jsonArray);
                for (inti=0; i < jsonArray.length(); i++) {
                    JSONObjectobject= jsonArray.getJSONObject(i);

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

                    JSONArrayweatherArray= object.getJSONArray("weather");
                    JSONObjectweatherObj= weatherArray.getJSONObject(0);

                    StringiconString= weatherObj.getString("icon");
                    finalStringiconURL="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);
                    StringdescriptionString= weatherObj
                            .getString("description");
                    System.out.println("Inside descriptionString "
                            + descriptionString);
                    description.add(descriptionString);

                    JSONObjecttempObj= object.getJSONObject("temp");

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

                    StringmaxTemp= 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());
            }
            returnnull;
        }

        private Bitmap getImage(String iconURL) {
            InputStreamin=null;
            Bitmapbmp=null;
            intresponseCode= -1;
            try {

                URLurl=newURL(iconURL);// "http://192.xx.xx.xx/mypath/img1.jpgHttpURLConnectioncon= (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;
        }

        privateclassListViewCustomAdapterextendsBaseAdapter {
            Context context;
            public LayoutInflater inflater;
            String Latitude, Longitude, cityName;
            String[] Humidity, Speed, Description1, minTemp, maxTemp;
            ArrayList<String> bitmap;
            inttotalDisplayDatasize=0;

            publicListViewCustomAdapter(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(newString[humidity.size()]);
                this.Speed = speed.toArray(newString[speed.size()]);
                this.Description1 = description.toArray(newString[description
                        .size()]);
                this.minTemp = tempMin.toArray(newString[tempMin.size()]);
                this.maxTemp = tempMax.toArray(newString[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 ");
            }

            @OverridepublicintgetCount() {
                // this could be one of the reason for not showing listview.set// total data length for countreturn totalDisplayDatasize;
            }

            @Overridepublic Object getItem(int i) {
                return i;
            }

            @OverridepubliclonggetItemId(int i) {
                return i;
            }

            privateclassHolder {
                TextView City, MinTemp, MaxTemp, Description, Speed, Latitude,
                        Longitude, Humidity;
                ImageView cloud;
            }

            @Overridepublic View getView(int position, View convertView, ViewGroup parent) {
                Holderholder=null;
                Viewview= convertView;
                System.out.println("Inside getView");
                if (view == null) {
                    System.out.println("Inside if getView");
                    holder = newHolder();
                    // / 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;
            }
        }
    }
}

classSingleRow {
    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"?><LinearLayoutxmlns: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" ><TextViewandroid:id="@+id/speed"android:layout_width="match_parent"android:layout_height="wrap_content"android:textColor="#ffff8800" /><TextViewandroid:id="@+id/weather"android:layout_width="match_parent"android:layout_height="wrap_content"android:textColor="#ffff8800" /><TextViewandroid:id="@+id/latitude"android:layout_width="match_parent"android:layout_height="wrap_content"android:textColor="#ffff8800" /><TextViewandroid:id="@+id/longitude"android:layout_width="match_parent"android:layout_height="wrap_content"android:textColor="#ffff8800" /><TextViewandroid:id="@+id/minTemp"android:layout_width="match_parent"android:layout_height="wrap_content"android:textColor="#ffff8800" /><TextViewandroid: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"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical" ><ListViewandroid:id="@+id/listdata"android:layout_width="fill_parent"android:layout_height="fill_parent" /></LinearLayout>

Solution 3:

Hi your getCount() in the base adaptor returns 0 change it to you total size of data you have like this

 @Override
        publicintgetCount() {
            return Humidity.length;
        }

and also change

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

from the doInBackground to onPostExecute methord in the Async Task Class

@OverrideprotectedvoidonPostExecute(String s) {
        super.onPostExecute(s);
aAdapter = newListViewCustomAdapter(WeatherReport.this,
                    Latitude, Longitude, cityName, humidity, speed, icon, description, tempMin, tempMax);
            listView.setAdapter(aAdapter);
    }

The complete Code Look Like This

publicclassWeatherReportextendsActivity {

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

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

privateclassgetJSONextendsAsyncTask<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;

    @Overrideprotected String doInBackground(String... params) {
        StringUrl= params[0];
        humidity = newArrayList<String>();
        speed = newArrayList<String>();
        weather = newArrayList<String>();
        tempMin = newArrayList<String>();
        tempMax = newArrayList<String>();
        icon = newArrayList<Bitmap>();
        description = newArrayList<String>();
        String data;
        try {
            HttpClienthClient=newDefaultHttpClient();
            HttpGethGet=newHttpGet(Url);
            ResponseHandler<String> rHandler = newBasicResponseHandler();
            data = hClient.execute(hGet, rHandler);
            System.out.println("Inside doInBackground  data " + data);
            JSONObjectjObj=newJSONObject(data);
            System.out.println("Inside Background jObj " + jObj);
            JSONObjectjsonObject= jObj.getJSONObject("city");
            cityName = jsonObject.getString("name");
            JSONObjectjObjCoOrd= jsonObject.getJSONObject("coord");
            Latitude = jObjCoOrd.getString("lat");
            System.out.println("Inside Latitude " + Latitude);
            Longitude = jObjCoOrd.getString("lon");
            System.out.println("Inside Longitude " + Longitude);

            JSONArrayjsonArray= jObj.getJSONArray("list");
            System.out.println("Inside jsonObjList " + jsonArray);
            for (inti=0; i < jsonArray.length(); i++) {
                JSONObjectobject= jsonArray.getJSONObject(i);

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

                JSONArrayweatherArray= object.getJSONArray("weather");
                JSONObjectweatherObj= weatherArray.getJSONObject(0);

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

                JSONObjecttempObj= object.getJSONObject("temp");

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

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

           //Removed Setting Adaptor from do in background to onPostExecute

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


    @OverrideprotectedvoidonPostExecute(String s) {
        super.onPostExecute(s);

        //The Last Step in Async Task Do set adaptor here

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

    private Bitmap getImage(String iconURL) {
        InputStreamin=null;
        Bitmapbmp=null;
        intresponseCode= -1;
        try {

            URLurl=newURL(iconURL);//"http://192.xx.xx.xx/mypath/img1.jpgHttpURLConnectioncon= (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;
    }

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


        publicListViewCustomAdapter(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(newString[humidity.size()]);
            this.Speed = speed.toArray(newString[speed.size()]);
            this.Description1 = description.toArray(newString[description.size()]);
            this.minTemp = tempMin.toArray(newString[tempMin.size()]);
            this.maxTemp = tempMax.toArray(newString[tempMax.size()]);
            this.inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            System.out.println("Inside ListViewCustomAdapter ");
        }

        @OverridepublicintgetCount() {

            //Prev you have been returning 0 its changed from 0 to Humidity.length so that base adaptor show the result as listviewreturn Humidity.length;
        }

        @Overridepublic Object getItem(int i) {
            return i;
        }

        @OverridepubliclonggetItemId(int i) {
            return i;
        }

        privateclassHolder {
            TextView City, MinTemp, MaxTemp, Description, Speed, Latitude, Longitude, Humidity;
            ImageView cloud;
        }

        @Overridepublic View getView(int position, View convertView, ViewGroup parent) {
            Holder holder= null;
            Viewview= convertView;
            System.out.println("Inside getView");
            if(view== null){
                System.out.println("Inside if getView");
                holder = newHolder();
                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;
        }
    }



classSingleRow {
    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;
    }

}

}

Solution 4:

Create and set adapter of your ListView in onPostExecute method of getJSON class.

getCount() - How many items are in the data set represented by this Adapter. You are returning 0 means you are setting the adapter's data as 0 so no row will be inflated.

In adapter change all those String array to ArrayList<String> besides that you need to change the constructor, getCount and getView portion

Declaration

ArrayList<Bitmap> bitmap, Humidity, Speed, Description1, minTemp, maxTemp;

constructor

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;
    this.Speed = speed;
    this.Description1 = description;
    this.minTemp = tempMin;
    this.maxTemp = tempMax;
    this.inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    System.out.println("Inside ListViewCustomAdapter ");
}

getCount portion

@Override
publicintgetCount() {
   return humidity.size();
}

getView portion

@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, parent, false);
        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.get(position));
   holder.MinTemp.setText(minTemp.get(position));
   holder.Speed.setText(Speed.get(position));
   holder.Description.setText(Description1.get(position));
   holder.cloud.setImageBitmap(bitmap.get(position));
   return convertView;
 }

Post a Comment for "Not Able To See Listview"