0

i tried to populate a ListView using SimpleAdapter, and for String values, my coding works fine.

...
ListView lv = (ListView)findViewById(R.id.list_jobs);
data = new ArrayList<Map>();
String[] from = {"job_postitle", "job_company", "job_location", "job_spec", "company_logo"};
int[] to = {R.id.job_postitle, R.id.job_company, R.id.job_location, R.id.job_spec, R.id.company_logo};
adapter = new SimpleAdapter(this, (List<? extends Map<String, ?>>) data, R.layout.activity_job_each, from, to);

lv.setAdapter(adapter);
...

I use HashMap to set the values,

...
Map m = new HashMap();
m.put("job_postitle", iObj.getString("title"));
m.put("job_company", iObj.getString("company"));
m.put("job_location", iObj.getJSONArray("location").toString());
m.put("job_spec", iObj.getJSONArray("specialization").toString());
m.put("company_logo", R.drawable.ic_launcher);
...

The company_logo is an ImageView, if I use R.drawable.ic_launcher as the value, it works. But if I set a URL of a remote image, or a Bitmap resource etc, the values will always be prepended with slash(/), for example:

...
URL url = new URL( Matcher.quoteReplacement( params[0] ) );
URLConnection connection = url.openConnection();
HttpURLConnection httpConnection = (HttpURLConnection) connection;
httpConnection.setRequestMethod("GET");
httpConnection.connect();

stream = httpConnection.getInputStream();
bitmap = BitmapFactory.decodeStream(stream, null, bmOptions);
stream.close();
...
return bitmap;
...

----------------------------update----------------------------

where params is an array from AsyncTask doInBackground function

protected Bitmap doInBackground(String... params) {...}

when i print the params value in LogCat, i will get these... for Log.d( TAG, params.toString() );

03-16 06:06:43.781: D/param(951): [Ljava.lang.String;@41820230

and for Log.d( TAG, params[0].toString() );

03-16 06:06:43.781: D/param(951): http://10.0.2.2/assets/img/blog/2.jpg

----------------------------update----------------------------

and replace the company_logo value with this bitmap, then I will get this error:

03-14 20:49:55.780: E/BitmapFactory(948): Unable to decode stream: java.io.FileNotFoundException: /android.graphics.Bitmap@4183f340: open failed: ENOENT (No such file or directory)

and if I replace with a URL, I will get there error:

03-14 21:06:46.240: E/BitmapFactory(1003): Unable to decode stream: java.io.FileNotFoundException: /http:/10.0.2.2/assets/img/blog/1.jpg: open failed: ENOENT (No such file or directory)

Notice the slash at the beginning of the Bitmap or URL? anyone know how to remove slash?

sanusi
  • 739
  • 7
  • 10
  • I mean the argument inside .execute() of AsyncTask – nikis Mar 16 '14 at 11:03
  • @nikis that param is a complete, accessible URL (exp "http://10.0.2.2/assets/img/blog/2.jpg"); fyi, after i created the Bitmap from string, I was able to get the height and width of the image, so the returned Bitmap if fine. – sanusi Mar 16 '14 at 11:13

0 Answers0