0

2 つのテキスト文字列と画像の URL を格納するデータベースがあります。私の質問は、その URL から画像をダウンロードし、それを 2 つの文字列と共にリストビュー項目に入れる方法です。私のコードは文字列を受け取り、リンクから画像をダウンロードしますが、画像がリストビューに表示されません。

public class NotificariActivity extends ListActivity {

// Progress Dialog
private ProgressDialog pDialog;

// Creating JSON Parser object
JSONParser jParser = new JSONParser();

ArrayList<HashMap<String, Object>> productsList;

// url to get all products list
private static String url_not = "http://gtc.flavdesign.com/get_notificari.php";

// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_PRODUCTS = "notificari";
private static final String TAG_PID = "id";
private static final String TAG_TEXT = "text";
private static final String TAG_DATE = "data";
private static final String TAG_LINK = "link";

// products JSONArray
JSONArray products = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.all_oferte);

    // Hashmap for ListView
    productsList = new ArrayList<HashMap<String, Object>>();

    // Loading products in Background Thread
    new LoadAllProducts().execute();

}

/**
 * Background Async Task to Load all product by making HTTP Request
 * */
class LoadAllProducts extends AsyncTask<String, String, String> {

    /**
     * Before starting background thread Show Progress Dialog
     * */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(NotificariActivity.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>();
        // getting JSON string from URL
        JSONObject json = jParser.makeHttpRequest(url_not, "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_PRODUCTS);

                // 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_PID);
                    String text = c.getString(TAG_TEXT);
                    String date = c.getString(TAG_DATE);
                    String link = c.getString(TAG_LINK);

                    // creating new HashMap
                    HashMap<String, Object> map = new HashMap<String, Object>();

                    // adding each child node to HashMap key => value
                    map.put(TAG_PID, id);
                    map.put(TAG_TEXT, text);
                    map.put(TAG_DATE, date);

                    Bitmap bitmap;
                    try
                    {
                    URL url = new URL(link);
                    HttpGet httpRequest = null;
                    httpRequest = new HttpGet(url.toURI());
                    HttpClient httpclient = new DefaultHttpClient();
                    HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);
                    HttpEntity entity = response.getEntity();
                    BufferedHttpEntity b_entity = new BufferedHttpEntity(entity);
                    InputStream input = b_entity.getContent();
                    bitmap = BitmapFactory.decodeStream(input); 
                    }
                    catch(Exception e)
                    {
                        bitmap=Bitmap.createBitmap(null);
                    }

                    map.put("IMAGE",bitmap);


                    // adding HashList to ArrayList
                    productsList.add(map);
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        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
                 * */
                ListAdapter adapter = new SimpleAdapter(
                        NotificariActivity.this, productsList,
                        R.layout.list_oferte, new String[] { TAG_PID,
                                TAG_TEXT, TAG_DATE, "IMAGE"},
                        new int[] { R.id.pid, R.id.text, R.id.date, R.id.imageView1});

                // updating listview
                setListAdapter(adapter);
            }
        });

    }

}
}

リスト アイテムの XML ファイルは次のようになります。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<!-- Product id (pid) - will be HIDDEN - used to pass to other activity -->

<!-- Name Label -->

     <TextView
         android:id="@+id/pid"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:textColor="#FFFFFF"
         android:visibility="gone" />

     <TextView
         android:id="@+id/text"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:paddingLeft="6dip"
         android:paddingTop="6dip"
         android:textSize="17dip"
         android:textColor="#FFFFFF"
         android:textStyle="bold" />

     <TextView
         android:id="@+id/date"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:paddingLeft="6dip"
         android:paddingTop="6dip"
         android:textSize="17dip"
         android:textColor="#FFFFFF"
         android:textStyle="bold" />

     <ImageView
         android:id="@+id/imageView1"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:src="@android:drawable/gallery_thumb" />

</LinearLayout>
4

1 に答える 1