如何在android列表视图中使用json url显示图像

时间:2016-06-18 16:24:24

标签: java android json

我编写程序从json获取数据(文本)并在列表视图中显示,我编写代码及其工作但现在我用json获取图像的url但我不知道如何在列表视图中显示图像图像视图, 请帮帮我

这是我的代码

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

    }

}

}

2 个答案:

答案 0 :(得分:1)

我建议您使用最佳库来加载ASYNC图像 PICASSO LIBRARY LINK

现在你应该编写代码

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

答案 1 :(得分:0)

将来自url的图像显示到ImageView中使用此类:

new DownloadImageTask(imageView).execute(imageLinkURL);

要使用它,只需在代码中使用此命令:

{{1}}