如何通过ListAdpter显示许多图像?

时间:2014-11-05 23:20:15

标签: java android android-adapter

我有一个从web服务器读取一些项目的android活动,除了图像外,一切都很好 我只用了一个项目做了同样的工作,但是无论如何我都无法想到阅读很多项目并将它们的值设置为imageView。

是否有允许我在使用hashmap结束后将图像值设置为listAdapter?

非常感谢任何帮助!

以下是代码:

class LoadAllProducts extends AsyncTask<String, String, String> {

    /**
     * Before starting background thread Show Progress Dialog
     * */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(GetAllRecipesActivity.this);
        pDialog.setMessage("Loading products. Please wait...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }

    /**
     * getting All products from url
     * */
    protected String doInBackground(String... args) {
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("category", R_category));
        Log.d("R_category: ", R_category);
        // getting JSON string from URL
        JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);
        // Check your log cat for JSON reponse
        Log.d("All Products: ", json.toString());

        try {
            // Checking for SUCCESS TAG
            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) {
                // products found
                // Getting Array of Products
                products = json.getJSONArray(TAG_table);

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

                    // Storing each json item in variable
                    String id = c.getString(TAG_SCB_ID);
                    String title = c.getString(TAG_TITLE);
                    String name = c.getString(TAG_NAME);

                    name= name.substring(1, name.length());
                    name="http://studentcookbook.comoj.com/android_connect"+name;

                    Log.d("NAME OF THE URL!",name);

                 // here i'm starting to set the image value it's all good here 
                    downloadBitma =downloadBitmap(name);
                    // creating new HashMap
                    HashMap<String, Object> map=new HashMap<String, Object>();

                    // adding each child node to HashMap key => value

                    map.put(TAG_SCB_ID, id);

                    map.put(TAG_TITLE, title);

                    map.put(TAG_NAME, downloadBitma);
                    // adding HashList to ArrayList
                    productsList.add(map);
                }
            } else {

                // Launch Add New product Activity
                Intent i = new Intent(getApplicationContext(),AddRecipeActivity.class);
                // Closing all previous activities
                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(i);
            }
        } catch (JSONException e) {
            e.printStackTrace();
            Log.e("Buffer Error", "Error converting result " + e.toString());
        } catch (IOException e) {

            e.printStackTrace();
            Log.e("Buffer Error", " Image is not passing to hashmap");
        }

        return null;
    }

    /**
     * After completing background task Dismiss the progress dialog
     * **/
    protected void onPostExecute(String file_url) {
        // dismiss the dialog after getting all products
        pDialog.dismiss();
        // updating UI from Background Thread
        runOnUiThread(new Runnable() {
            public void run() {
                /**
                 * Updating parsed JSON data into ListView
                 * */
                // here i'm trying to set the image to imageView but it didn't work
//                  final ImageView imageView = (ImageView) findViewById(R.id.GetAllmg); 
//                   runOnUiThread(new Runnable() {  
//                    @Override  
//                      public void run() {  
//                  imageView.setImageBitmap(downloadBitma);        }  
//                     });  
                ListAdapter adapter = new SimpleAdapter(GetAllRecipesActivity.this, productsList,
                        R.layout.list_recipe, new String[] { TAG_SCB_ID,TAG_TITLE},
                        new int[] { R.id.GetAllscbid, R.id.GetAllTitle });
                // updating listview
                Toast.makeText(GetAllRecipesActivity.this, "onPostExecute",   Toast.LENGTH_SHORT).show();
                setListAdapter(adapter);
            }
        });

    }
// method for downloading images 
    private Bitmap downloadBitmap(String url) throws IOException {  
        HttpUriRequest request = new HttpGet(url.toString());  
        HttpClient httpClient = new DefaultHttpClient();  
        HttpResponse response = httpClient.execute(request);  

        StatusLine statusLine = response.getStatusLine();  
        int statusCode = statusLine.getStatusCode();  
        if (statusCode == 200) {  
            HttpEntity entity = response.getEntity();  
            byte[] bytes = EntityUtils.toByteArray(entity);  

            Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0,  
                    bytes.length);  
            return bitmap;  
        } else {  
            throw new IOException("Download failed, HTTP response code "  
                    + statusCode + " - " + statusLine.getReasonPhrase());  
        }  
    }  

1 个答案:

答案 0 :(得分:-1)