0

I am using a JSON API to gather data for a list view. The lisview has several textviews and one imageviews. I have gone through many examples to load the imageview with URL but none of them works for the listview image. Here is my Main file.

public class MainActivity extends AppCompatActivity {

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

    private ProgressDialog pDialog;
    private String userphoto,radiusdb,carWash,carpetWash,commerical,domestic, garden,industrial,laundry,oven,pest,tile,vet,windowWash,verification, serviceRadius, listing_price;
    private ListView lv;

    // URL to get contacts JSON
    private static String url = api url;

    ArrayList<HashMap<String, String>> resultList;

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

        resultList = new ArrayList<>();

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

        new GetResults().execute();
    }

    /**
     * Async task class to get json by making HTTP call
     */
    private class GetResults 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 results = jsonObj.getJSONArray("Results");

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


                        //verification="";

                        String id = c.getString("id");
                        String title = c.getString("title");
                        String url = c.getString("url");


                        // Phone node is JSON Object
                        JSONObject user_data = c.getJSONObject("user_data");
                        String wlt_membership = user_data.getString("wlt_membership");
                        try {
                            userphoto = user_data.getString("userphoto");
                        }
                        catch(final JSONException e){
                            userphoto="https://www.kleanify.com/wp-content/themes/BT/templates/template_business_theme/img/defaultverify.jpg";

                        }

                        JSONObject post_meta = c.getJSONObject("post_meta");
                        String addressCount1 = post_meta.getString("addressCount1");
                        try{
                            listing_price = post_meta.getString("listing_price");
                        }
                        catch(final JSONException e){
                            listing_price="";
                        }
                        try{
                            radiusdb = post_meta.getString("radiusdb");
                        }
                        catch(final JSONException e){
                            radiusdb="";
                        }

                        JSONObject categories = c.getJSONObject("categories");
                        try {
                            windowWash = categories.getString("window-wash");
                        }
                        catch(final JSONException e){
                            windowWash="";
                        }
                        try {
                            carWash = categories.getString("car-wash");
                        }
                        catch(final JSONException e){
                            carWash="";
                        }
                        try {
                            carpetWash = categories.getString("carpet-wash");
                        }
                        catch(final JSONException e){
                            carpetWash="";
                        }
                        try {
                            commerical = categories.getString("commercial-cleaning");
                        }
                        catch(final JSONException e){
                            commerical="";
                        }
                        try {
                            domestic = categories.getString("domestic-cleaning");
                        }
                        catch(final JSONException e){
                            domestic="";
                        }
                        try {
                            garden = categories.getString("garden-cleaning");
                        }
                        catch(final JSONException e){
                            garden="";
                        }
                        try {
                            industrial = categories.getString("industrial-cleaning");
                        }
                        catch(final JSONException e){
                            industrial="";
                        }
                        try {
                            laundry = categories.getString("laundry");
                        }
                        catch(final JSONException e){
                            laundry="";
                        }
                        try {
                            oven = categories.getString("oven-cleaning");
                        }
                        catch(final JSONException e){
                            oven="";
                        }
                        try {
                            pest = categories.getString("pest-control");
                        }
                        catch(final JSONException e){
                            pest="";
                        }
                        try {
                            tile = categories.getString("tile-cleaning");
                        }
                        catch(final JSONException e){
                            tile="";
                        }
                        try {
                            vet = categories.getString("vet");
                        }
                        catch(final JSONException e){
                            vet="";
                        }


                        if(wlt_membership.equals("0")){
                            verification="";
                        }
                        else if(wlt_membership.equals("1")){
                            verification="Silver Member";
                        }
                        else if(wlt_membership.equals("3")){
                            verification="Verified";
                        }

                        serviceRadius="Service Radius "+radiusdb+" miles";



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

                        // adding each child node to HashMap key => value
                        result.put("id", id);
                        result.put("title", title);
                        result.put("url", url);
                        result.put("verification",verification);
                        result.put("userphoto",userphoto);
                        result.put("serviceRadius",serviceRadius);
                        result.put("addressCount1",addressCount1);
                        result.put("listing_price",listing_price);


                        // adding contact to contact list
                        resultList.add(result);
                    }
                } 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, resultList,
                    R.layout.list_item, new String[]{"title","verification","serviceRadius","addressCount1","listing_price"}, new int[]{
                    R.id.title, R.id.verification_type,R.id.radius,R.id.address,R.id.service});
            /*MySimpleAdapter adapter = new MySimpleAdapter(MainActivity.this, resultList,
                    R.layout.row, new String[] {}, new int[] {});*/

            lv.setAdapter(adapter);
        }

    }
}
2
  • please add adapter class for this Commented Jan 9, 2017 at 14:58
  • why don't you use picasso image library to show the image in list view Commented Jan 9, 2017 at 14:59

2 Answers 2

1

You can look up to this link it works fine you need to fire one more async task to download image from url.

http://www.wingnity.com/blog/android-json-parsing-and-image-loading-tutorial/

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

1 Comment

Life saving one. Suits perfectly my need. Thanks very much!
0

Use Glide for loading image

add a dependency in build.gradle(app)

compile 'com.github.bumptech.glide:glide:3.7.0'

add below code inside your onActivityResult

  Glide
  .with(mContext)
  .load(url)
  .centerCrop()
  .placeholder(R.color.black)
  .crossFade()
  .into(imageView);

Very efficient for loading image

For more details refer

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.