0

Here is my code. The image space remaining empty. Not being loaded. What is my mistake here?
what kind of code i need again. give more definition pls.

public class MainActivity extends AppCompatActivity {

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

        private ProgressDialog progressDialog;
        private ListView listView;

        // JSON data url
        private static String Jsonurl = "http://microblogging.wingnity.com/JSONParsingTutorial/jsonActors";
        ArrayList<HashMap<String, String>> contactJsonList;

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

            contactJsonList = new ArrayList<>();
            listView = (ListView) findViewById(R.id.listview);
            new GetContacts().execute();
        }

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

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

            @Override
            protected Void doInBackground(Void... arg0) {
                HTTPHandler httpHandler = new HTTPHandler();

                // request to json data url and getting response
                String jsonString = httpHandler.makeServiceCall(Jsonurl);
                Log.e(TAG, "Response from url: " + jsonString);
                if (jsonString != null) {
                    try {
                        JSONObject jsonObject = new JSONObject(jsonString);
                        // Getting JSON Array node
                        JSONArray contacts = jsonObject.getJSONArray("actors");

                        for (int i = 0; i < contacts.length(); i++) {

                            JSONObject c = contacts.getJSONObject(i);
                            String name = c.getString("name");
                            String country = c.getString("country");
                            String spouse = c.getString("spouse");
                            String dob = c.getString("dob");
                            String description = c.getString("description");
                            String children = c.getString("children");
                            String image = c.getString("image");

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

                            // adding each child node to HashMap key => value
                            contact.put("name", name);
                            contact.put("country", country);
                            contact.put("spouse", spouse);
                            contact.put("dob", dob);
                            contact.put("description", description);
                            contact.put("children", children);
                            contact.put("image", image);

                            // adding contact to contact list
                            contactJsonList.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, "Could not get json from server.");
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getApplicationContext(),"Could not get json from server.",Toast.LENGTH_LONG).show();
                        }
                    });
                }
                return null;
            }

            @Override
            protected void onPostExecute(Void result) {
                super.onPostExecute(result);
                // Dismiss the progress dialog
                if (progressDialog.isShowing())
                    progressDialog.dismiss();

                /**     * Updating parsed JSON data into ListView    * */
                ListAdapter adapter = new SimpleAdapter(MainActivity.this, contactJsonList, R.layout.row,
                        new String[]{"name","country", "spouse", "dob", "description", "children", "image"},
                        new int[]{R.id.name, R.id.country, R.id.spouse, R.id.dob, R.id.description, R.id.children, R.id.imageview});

                listView.setAdapter(adapter);
            }
        }
    }

thanks for your help

1
  • The image you are reading I guess it is a URL. So you have to load your bitmap from the image url inside your adapter. So check your adapter code. Commented Jun 20, 2017 at 11:29

3 Answers 3

1

If you want to load image from URL Use custom adapter and use picasso or Glide library to load image. or If you want to use simpleAdapter then check this link Image from URL in ListView using SimpleAdapter

Sign up to request clarification or add additional context in comments.

Comments

0

you can user Glide library to load image from url look the below code it can help you in simple way

compile this library

compile 'com.github.bumptech.glide:glide:4.0.0-RC0'

than load image like this

Glide.with(HomeClass.this)
            .load(userProfileUrl)
            .centerCrop()
            .diskCacheStrategy(DiskCacheStrategy.NONE)
            .skipMemoryCache(true)
            .dontAnimate()
            .into(imageview);

4 Comments

I have an issue updating ListAdapter to highlight search text here: stackoverflow.com/questions/79041428/…? I would appreciate any insights or ideas you could offer on how to fix.
Hey @AJW can you please share your code in a GitHub repo so I can try to help you
I have not done that previously but would be happy to set that up. Will circle back to let you know when it is loaded.
@AJW any update?
0

Do you want to load list of images from url? then check out the link below, there is detailed example working with list of images using json with volly library. Example

I hope this will help you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.