theGreenCabbage
theGreenCabbage

Reputation: 4845

Lack of anything shown on Android Activity

I have the following layouts XML called list_item_layout.xml:

<ImageView
    android:layout_width="80dp"
    android:layout_height="40dp"
    android:id="@+id/imageView" />

<TextView
    android:layout_width="100dp"
    android:layout_height="@dimen/String_height"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Text"
    android:id="@+id/textView"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/imageView"
    android:layout_toEndOf="@+id/imageView"
    android:layout_alignBottom="@+id/imageView" />

<TextView
    android:layout_width="60dp"
    android:layout_height="@dimen/String_height"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Text"
    android:id="@+id/textView2"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/textView" />

<TextView
    android:layout_width="60dp"
    android:layout_height="@dimen/String_height"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Text"
    android:id="@+id/textView3"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/textView2" />

<TextView
    android:layout_width="100dp"
    android:layout_height="@dimen/String_height"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Text"
    android:id="@+id/textView4"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/textView3"
    android:layout_toEndOf="@+id/textView3" />

This is my AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.drexel.jacky.weather" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>


    <uses-permission android:name="android.permission.INTERNET" />
</manifest>

In the GUI the design is like this:

enter image description here

This is the Java code that gets the data from Weather Underground (this is in a doInBackground thread for networking):

        String key= "someKey";

        String sURL = "http://api.wunderground.com/api/someKey/geolookup/q/autoip.json";

        // Connect to the URL
        URL url = new URL(sURL);
        HttpURLConnection request = (HttpURLConnection) url.openConnection();
        request.connect();


        // Convert to a JSON object to print data
        JsonParser jp = new JsonParser();
        JsonElement root = jp.parse(new InputStreamReader((InputStream) request.getContent()));
        JsonObject rootobj = root.getAsJsonObject(); // may be Json Array if it's an array, or other type if a primitive



        // Get some data elements and print them)
        String city = rootobj.get("location").getAsJsonObject().get("city").getAsString();
        String sURL1 = "http://api.wunderground.com/api/" + key + "/hourly/q/PA/"+city+ ".json";
        URL url1 = new URL(sURL1);
        HttpURLConnection request1 = (HttpURLConnection) url1.openConnection();
        request1.connect();
        JsonParser jp1 = new JsonParser();
        JsonElement root1 = jp1.parse(new InputStreamReader((InputStream) request1.getContent()));
        JsonObject rootobj1 = root1.getAsJsonObject(); // may be Json Array if it's an array, or other type if a primitive

        String hour;
        String date;
        String humidity;
        String image_url;
        int l = rootobj1.get("hourly_forecast").getAsJsonArray().size();
        for (int i = 0; i < l; i++){
            hour = rootobj1.get("hourly_forecast").getAsJsonArray().get(i).getAsJsonObject().get("FCTTIME").getAsJsonObject().get("hour").getAsString();
            date = rootobj1.get("hourly_forecast").getAsJsonArray().get(i).getAsJsonObject().get("FCTTIME").getAsJsonObject().get("weekday_name").getAsString();
            humidity = rootobj1.get("hourly_forecast").getAsJsonArray().get(i).getAsJsonObject().get("temp").getAsJsonObject().get("english").getAsString();
            image_url = rootobj1.get("hourly_forecast").getAsJsonArray().get(i).getAsJsonObject().get("icon_url").getAsString();
            Weather weather = new Weather(city,hour,date,humidity,image_url);
            data.add(i, weather);

            Log.d("Hour: ", hour);
            Log.d("Date: ", date);

            //Insert data into the database
            Sql_Open_Helper sql_open_helper = new Sql_Open_Helper(this.context);
            SQLiteDatabase db = sql_open_helper.getWritableDatabase();
            if(this.c) {
                sql_open_helper.remake(db);
                this.c = false;
            }

            ContentValues values = new ContentValues();
            values.put("Temp", humidity);
            values.put("Hour", hour);
            values.put("Date", date);
            values.put("City", city);

            // Inserting Row
            db.insert("Weather_Table", null, values);
            db.close(); // Closing database connection
        }

I have been trying to get strings to show up on my Note 3 (it's a 1080x1920 resolution) using the following lay out, but with absolutely no progress made.

When I compile the app and load it using Android Studio, the activity is simply blank.

I tried a bunch of things. I made sure that the JSON parsing library is indeed returning the data such as hour, image URL, temperature, etc, and it does work (I created that library as a command line application first).

However, the data is just not showing up. The weird thing is, I do not even see any errors at all when I compile.

Could someone with much more Android experience than me help me debug this?

EDIT:

The following is my onCreate:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //Create weather list for handling database
    Sql_Open_Helper sql_open_helper = new Sql_Open_Helper(this);
    SQLiteDatabase db = sql_open_helper.getReadableDatabase();
    int hour = sql_open_helper.getHour(db); //Geting the last hour


    //Geting current hour for checking with in the last hour
    int current_hour = 14;
    //DateFormat Hhour = new SimpleDateFormat("kk:00 a");
    //int current_hour = Integer.parseInt(Hhour.format(Calendar.getInstance().getTime()).split(":",2)[0]);


    //Compare the last hour with the current hour
    if(current_hour <= hour) {
        ArrayList<Weather> weather_list= sql_open_helper.addWeatherToList(db);
        MyArrayAdapter myArrayAdapter = new MyArrayAdapter(this, weather_list,false);
        ListView l = (ListView) findViewById(R.id.listView);
        l.setAdapter(myArrayAdapter);
    }


    else {
        Bg_task bg_task = new Bg_task(this);
        MyArrayAdapter myArrayAdapter = new MyArrayAdapter(this, bg_task.getData(),true);
        ListView l = (ListView) findViewById(R.id.listView);
        l.setAdapter(myArrayAdapter);
        bg_task.execute();
    }
}

This is the class MyArrayAdapter which I wrote for the adapter:

public class MyArrayAdapter extends ArrayAdapter<Weather> {

    private Context context;
    private ArrayList<Weather> array_weather;
    private boolean b;
    private boolean c;

    public MyArrayAdapter(Context context, ArrayList<Weather> objects,boolean b) {
        super(context,R.layout.list_item_layout, objects);
        this.context = context;
        array_weather = objects;
        this.b = b;
        this.c = true;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.list_item_layout,null);
        }

        TextView textView1 = (TextView) convertView.findViewById(R.id.textView);
        TextView textView2 = (TextView) convertView.findViewById(R.id.textView2);
        TextView textView3 = (TextView) convertView.findViewById(R.id.textView3);
        TextView textView4 = (TextView) convertView.findViewById(R.id.textView4);
        textView1.setText(array_weather.get(position).getCity());
        textView2.setText(array_weather.get(position).getHuminity());
        textView3.setText(array_weather.get(position).getHour());
        textView4.setText(array_weather.get(position).getDate());
        if(b){
            ImageView imageView = (ImageView) convertView.findViewById(R.id.imageView);//Display Image
            new DownloadImageTask(imageView).execute(array_weather.get(position).getImage_url());
        }
        return convertView;
    }
    private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
        ImageView bmImage;

        public DownloadImageTask(ImageView bmImage) {
            this.bmImage = bmImage;
        }

        protected Bitmap doInBackground(String... urls) {
            String urldisplay = urls[0];
            Bitmap mIcon11 = null;
            try {
                InputStream in = new java.net.URL(urldisplay).openStream();
                mIcon11 = BitmapFactory.decodeStream(in);
            } catch (Exception e) {
                Log.e("Error", e.getMessage());
                //e.printStackTrace();
            }
            return mIcon11;
        }

        protected void onPostExecute(Bitmap result) {
            bmImage.setImageBitmap(result);
        }

    }
}

Upvotes: 0

Views: 93

Answers (1)

Jaydeep Patel
Jaydeep Patel

Reputation: 156

You have to do some changes after completion of async task. in onPostExecute add below code :

@Override
protected void onPostExecute(Void aVoid) {             
      MyArrayAdapter myArrayAdapter = new MyArrayAdapter(this, getData(),true);
      ListView l = (ListView) findViewById(R.id.listView);
      l.setAdapter(myArrayAdapter);   
}

And your Activity look like below :

//Compare the last hour with the current hour
if(current_hour <= hour) {
    ArrayList<Weather> weather_list= sql_open_helper.addWeatherToList(db);
    MyArrayAdapter myArrayAdapter = new MyArrayAdapter(this, weather_list,false);
    ListView l = (ListView) findViewById(R.id.listView);
    l.setAdapter(myArrayAdapter);
}


else {
    Bg_task bg_task = new Bg_task(this);
    bg_task.execute();
}

Upvotes: 1

Related Questions