java - 如何通过ListAdpter显示多张图片?

标签 java android android-adapter

我有一个从网络服务器读取一些项目的 android Activity ,除图像外一切正常。
我只对一个项目做了同样的事情并且它有效,但无论如何我想不出要阅读许多项目并将它们的值设置为 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());  
        }  
    }  

最佳答案

关于java - 如何通过ListAdpter显示多张图片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26769028/

相关文章:

java - 无法使用 java.sql.Date 返回 List<Object>?

java - 无法安装 JMeter 的 JSON 路径提取器插件

java - 当我尝试将我的 JSON 解析为 ListView 时,我收到“未找到资源”异常?

java - 推送通知中未显示图像

java - 在屏幕上绘制自定义字体

java - 自定义 ArrayAdapter 中无法访问的语句

java - Spring如何 Autowiring session 作用域的bean?

android - 如何在不创建新应用程序的情况下从生产中取消发布应用程序并在 Google Play 中重新发布测试版?

android - 将 android TextView 更改为 ListView

android - 使用适配器时在 Android 上刷新 Activity