使用从JSON数据收集的URL在List中加载imageview

时间:2017-01-09 14:51:30

标签: android listview android-imageview

我正在使用JSON API来收集列表视图的数据。 lisview有几个textview和一个imageview。我已经通过许多示例来加载带有URL的imageview,但它们都不适用于listview图像。这是我的主文件。

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 个答案:

答案 0 :(得分:1)

您可以查看此链接,它可以正常工作,您需要再触发一个异步任务从网址下载图像。

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

答案 1 :(得分:0)

使用Glide加载图片

在build.gradle(app)中添加依赖项

Sub setFormTimer()
    FormTimer  = Now + TimeValue("00:00:10")
    Application.OnTime EarliestTime:=FormTimer, Procedure:="Shutdown", Schedule:=True
End Sub

Sub stopFormTimer()
    Application.OnTime EarliestTime:=FormTimer, Procedure:="Shutdown", Schedule:=False
End Sub

在onActivityResult

中添加以下代码
compile 'com.github.bumptech.glide:glide:3.7.0'

加载图片非常有效

有关详细信息refer