alireza
alireza

Reputation: 29

how to show image with json url in android list view

I write program to get data(text) from json and show it in list view , i write the code and its working but now i get url of image with json but i dont know how to show the image in list view with image view, please help me

here is my code

MainActivity:

public class MainActivity extends ListActivity {

private ProgressDialog pd;
JSONParser jParser=new JSONParser();
ArrayList<HashMap<String,String>> P;
JSONArray s=null;
private final String url="http://192.168.1.4:81/upload/travel.php";


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

}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    String id_code=((TextView)v.findViewById(R.id.id_code)).getText().toString();
    Intent in=new Intent(MainActivity.this,ADD.class);
    in.putExtra("id",id_code);
    startActivity(in);

}

class travel extends AsyncTask<String,Void,String>{

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pd=new ProgressDialog(MainActivity.this);
        pd.setMessage("login");
        pd.show();
    }

    @Override
    protected String doInBackground(String... params) {

        List<NameValuePair> parms=new ArrayList<>();
        JSONObject json=jParser.makeHTTPRequest(url,"GET");

        try {
            int t=json.getInt("t");
            if(t==1){
                s=json.getJSONArray("travel");
                for(int i=0;i<s.length();i++){
                    JSONObject c=s.getJSONObject(i);
                    String id=c.getString("id");
                    String companyname=c.getString("companyname");
                    String cod=c.getString("cod");
                    String bign=c.getString("bign");
                    String stop=c.getString("stop");
                    String date=c.getString("date");
                    String time=c.getString("time");
                    String price=c.getString("price");
                    String url_image=c.getString("url_image");

                    HashMap<String,String>map=new HashMap<String,String>();
                    map.put("id",id);
                    map.put("companyname",companyname);
                    map.put("cod",cod);
                    map.put("bign",bign);
                    map.put("stop",stop);
                    map.put("date",date);
                    map.put("time",time);
                    map.put("price",price);
                    map.put("url_image",url_image);

                    P.add(map);

                }
            }else {
                Toast.makeText(MainActivity.this,"No Data Found",Toast.LENGTH_SHORT).show();
            }


        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        pd.dismiss();
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                ListAdapter adapter = new SimpleAdapter(MainActivity.this, P, R.layout.item_list,
                        new String[]{"url_image","companyname", "cod", "bign", "stop", "date", "time", "price"},
                        new int[]{R.id.id_code,R.id.companyname, R.id.cod, R.id.bign, R.id.stop, R.id.date, R.id.time1, R.id.price});


                setListAdapter(adapter);
            }
        });

    }

}

}

Upvotes: 0

Views: 68

Answers (2)

Humza Malik
Humza Malik

Reputation: 343

i would suggest you use best library for image loading ASYNC PICASSO LIBRARY LINK

Now you should write code

Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);

Upvotes: 1

Yarden Cohen
Yarden Cohen

Reputation: 594

to show image from url into an ImageView use this class:

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

In order to use it just use this command in your code:

new DownloadImageTask(imageView).execute(imageLinkURL);

Upvotes: 0

Related Questions