• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Create an image URL, download image and display

Pooveshin

Newbie
Hi All

I am working on an android app, the app downloads data from a json object and displays them.
The file contains an array of JSON objects, each one of which represents an Android version.
For example, one entry is as follows:
Code:
{
"name":"Cupcake",
"version":"1.5",
"released":"April 27, 2009",
"api":"3",
"image":"images/cupcake.jpg"
}
Once the file has been downloaded, it should be parsed and the data stored in an array of Java
Model objects representing each version.
The main screen of your app should display a list of the Android versions. Each entry in the list
must show a thumbnail image on the left side, downloaded from the given image URL in the
JSON entry. On the right you should display the name (e.g., Cupcake).
When the user taps on an entry, a new screen must be displayed giving all the other details
related to that version, such as version number, release date and API.

I need assistance with the image downloading (so the image url must be taken attached to the main url and download the image)and when users click on an item to open a new screen and view the rest of the data. My code is below as well as my layouts, so far the app works in downloading the data.

Code:
public class MainActivity extends AppCompatActivity {

    private String TAG = MainActivity.class.getSimpleName();

    private ProgressDialog pDialog;
    private ListView lv;

    // URL to get Android Version Data JSON
    private static String url = "http://codetest.cobi.co.za/androids.json";
    private static String url1 = "http://codetest.cobi.co.za/";

    ArrayList<HashMap<String, String>> androidversions;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        androidversions = new ArrayList<>();

        lv = (ListView) findViewById(R.id.list);

        new GetVersions().execute();
    }

    private class GetVersions extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // Showing progress dialog
            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setMessage("Please wait...");
            pDialog.setCancelable(false);
            pDialog.show();
        }
        @Override
        protected Void doInBackground(Void... arg0) {
            HttpHandler sh = new HttpHandler();

            // Making a request to url and getting response
            String jsonStr = sh.makeServiceCall(url);

            Log.e(TAG, "Response from url: " + jsonStr);

            if (jsonStr != null) {
                try {
                    JSONObject jsonObj = new JSONObject(jsonStr);

                    // Getting JSON Array node
                    JSONArray versions = jsonObj.getJSONArray("versions");

                    // looping through All Versions
                    for (int i = 0; i < versions.length(); i++) {
                        JSONObject c = versions.getJSONObject(i);

                        String name = c.getString("name");
                        String version = c.getString("version");
                        String released = c.getString("released");
                        //String api = c.getString("api");
                        //String image = c.getString("image");

                        // tmp hash map for single version
                        HashMap<String, String> contact = new HashMap<>();

                        // adding each child node to HashMap key => value
                        contact.put("name", name);
                        contact.put("version", version);
                        contact.put("released", released);

                        // adding Data to version list
                        androidversions.add(contact);
                    }
                } catch (final JSONException e) {
                    Log.e(TAG, "Json parsing error: " + e.getMessage());
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getApplicationContext(),
                                    "Json parsing error: " + e.getMessage(),
                                    Toast.LENGTH_LONG)
                                    .show();
                        }
                    });

                }
            } else {
                Log.e(TAG, "Couldn't get json from server.");
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getApplicationContext(),
                                "Couldn't get json from server. Check LogCat for possible errors!",
                                Toast.LENGTH_LONG)
                                .show();
                    }
                });

            }

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            // Dismiss the progress dialog
            if (pDialog.isShowing())
                pDialog.dismiss();
            /**
             * Updating parsed JSON data into ListView
             * */
            ListAdapter adapter = new SimpleAdapter(
                    MainActivity.this, androidversions,
                    R.layout.list_item, new String[]{"name", "version",
                    "released"}, new int[]{R.id.name,
                    R.id.version, R.id.released});

            lv.setAdapter(adapter);
        }

    }
}
Layout
Code:
<TextView
    android:id="@+id/name"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="2dip"
    android:paddingTop="6dip"
    android:textColor="@color/colorPrimaryDark"
    android:textSize="16sp"
    android:textStyle="bold" />

<TextView
    android:id="@+id/version"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="2dip"
    android:textColor="@color/colorAccent" />

<TextView
    android:id="@+id/released"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#5d5d5d"
    android:textStyle="bold" />

Code:
<ListView
    android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
 
Back
Top Bottom