How can I use Universal Image Loader to load images from URLs?
I have HashMap:
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
map.put("pict", "http://server/image");
map.put("name", "Name of the picture");
mylist.add(map);
Then I have SimpleAdapter:
ListView list = (ListView) findViewById(R.id.prod_listview);
SimpleAdapter mSchedule = new SimpleAdapter(prod.this, mylist, R.layout.prod_rows, new String[] {"pict", "name"}, new int[] {R.id.prod_pict, R.id.prod_name});
list.setAdapter(mSchedule);
I gues I should download 'http://server/image', make it bitmap and in some magical way put it to the SimpleAdapter. But how?
I saw Universal Image Loader but I do not get it how to use it in my case.
Yes you have to download the images using Asynctask process here you have an example of this
Loading Image Asynchronously in Android
Using ImageDownloaderTask and setting the image asynchronously in your ImageView
if (holder.imageView != null) {
new ImageDownloaderTask(holder.imageView).execute(newsItem.getUrl());
}
Related
Using the image of the server instead of native images
I want my server images and text on my list.
Thank you very much
List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();
for(int i=0;i<10;i++){
HashMap<String, String> hm = new HashMap<String,String>();
hm.put("txt", "Country : " + countries[i]);
hm.put("cur", "Currency : " + currency[i]);
hm.put("flag",Integer.toString(R.drawable.abc_ab_share_pack_holo_light) );
aList.add(hm);
}
// Keys used in Hashmap
String[] from = { "flag","txt","cur" };
// Ids of views in listview_layout
int[] to = { R.id.flag,R.id.txt,R.id.cur};
// Instantiating an adapter to store each items
// R.layout.listview_layout defines the layout of each item
SimpleAdapter adapter = new SimpleAdapter(getActivity().getBaseContext(), aList, R.layout.listview_layout1, from, to);
setListAdapter(adapter);
You can store the Image URL as string and then can retrieve the image to your View as you like.You can use some Image Loaders Like Picasso(http://square.github.io/picasso/) to load the image to your view!
I am trying to do a listview with 2 database collumn. I succeded but my problem is that only the first information of the database appears in the listview. If i have x data in the database it will show the x rows in the listview with only the first entry.
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
data = new ArrayList();
Cursor cursor = mydb.rawQuery("SELECT * FROM projeto", null);
if (cursor.moveToFirst()) {
Toast.makeText(getApplicationContext(), "Caminhos:", 3000).show();
data.clear();
do {
data.add(cursor.getString(cursor.getColumnIndex("id_proj")));
data.add(cursor.getString(cursor.getColumnIndex("nome")));
map.put("id", data.get(0).toString());
map.put("nome", data.get(1).toString());
map.put("hora", "17:00");
mylist.add(map);
} while (cursor.moveToNext());
SimpleAdapter resul = new SimpleAdapter(MainActivity.this, mylist,R.layout.list_item,new String[] {"id", "nome", "hora"}, new int[] {R.id.i, R.id.n, R.id.h});
lista.setAdapter(resul);
I see two problems in your code:
You don't clear data (Like said in a previous answer) so you always get the first two values
You always use the same map. Since map is an object you will always modify the same and your list will be fill will one map.
To fix this two points try this:
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
data = new ArrayList();
Cursor cursor = mydb.rawQuery("SELECT * FROM projeto", null);
if (cursor.moveToFirst()) {
Toast.makeText(getApplicationContext(), "Caminhos:", 3000).show();
do {
HashMap<String, String> map = new HashMap<String, String>();
data.clear();
data.add(cursor.getString(cursor.getColumnIndex("id_proj")));
data.add(cursor.getString(cursor.getColumnIndex("nome")));
map.put("id", data.get(0).toString());
map.put("nome", data.get(1).toString());
map.put("hora", "17:00");
mylist.add(map);
} while (cursor.moveToNext());
SimpleAdapter resul = new SimpleAdapter(MainActivity.this, mylist,R.layout.list_item,new String[] {"id", "nome", "hora"}, new int[] {R.id.i, R.id.n, R.id.h});
lista.setAdapter(resul);
look at this,
map.put("id", data.get(0).toString());
map.put("nome", data.get(1).toString());
map.put("hora", "17:00");
You always put the same key in the map, always "id, nome and hora" you need put diferent key.
ps: Why you create a List of Map? You don't need to this.
You need to clear data each time through the loop. You need to move your call to data.clear() to just after the do { loop begins. What you're doing at the moment will mean that after the first item is retrieved from the cursor, data will contain 2 items. After the second time it will contain 4, then 6, etc. You then always read the same two items. If you clear data it should fix your problem.
this error apear when i add bitmap image in hashmap to display it in listview
any help please??
hm = new HashMap<String, Object>();
hm.put(IMAGE, ops);
hm.put(TITLE, text);
hm.put(CARDS_COUNT, cardsCount +" Stampii");
items.add(hm);
}
final SimpleAdapter adapter = new SimpleAdapter(this, items, R.layout.main_listview,
new String[]{TITLE, CARDS_COUNT, IMAGE}, new int[]{ R.id.main_name, R.id.main_info, R.id.main_img});
listView.setAdapter(adapter);
It sounds as you are adding the Bitmap object to the map, but what you should add is a uri that points to the bitmap (e.g. a file uri, a content uri, ...) not the bitmap it self. You can also add an integer resource id (e.g. R.drawable.my_bitmap).
I have a listview which i want it for displaying a text and corrs image. I have used an arrayadapter for it. I am able to get an arraylist of hashmaps containing the values of the text and the url for the image.
<Arraylist<Hashmap<String,string>> testdata : "name" and "image_url"
Now i am trying to bind it. But no image is shown and the logcat shows resolveuri failed on bad bitmap.
( my url is "/com.example.vocab.MainActivity/res/drawable-hdpi/right_icon.png" ). What am i doing wrong? Thanx in advance for any help.
// Binding resources Array to ListAdapter
this.setListAdapter(new SimpleAdapter(Grammar_tab_all.this, testdata ,
R.layout.list_item, new String[] { "name","img_url"},
new int[] { R.id.module_name_item, R.id.img_recom}));
final ListView lv = getListView();
To show the drawable images in listview, best method is to store only the int id of drawable image.
Try this.
listItems = new ArrayList<HashMap<String,Integer>>();
String fieldName = "image_id";
HashMap<String, Integer> listData1 = new HashMap<String, Integer>();
HashMap<String, Integer> listData2 = new HashMap<String, Integer>();
listData1.put(fieldName, R.drawable.camera_icon_focus_dim);
listData2.put(fieldName, R.drawable.camera_icon_scene_mode);
listItems.add(listData1);
listItems.add(listData2);
SimpleAdapter listItemAdapter = new SimpleAdapter(
this,
listItems,
R.layout.image_list_item,
new String[] { fieldName },
new int[] { R.id.listitem_img });
Instead of this <Arraylist<Hashmap<String,string>> testdata try with this <Arraylist<Hashmap<String,Object>> testdata if you need more refer this link http://developerboards.att.lithium.com/t5/AT-T-Developer-Program-Blogs/Developing-Apps-for-Android-Beyond-quot-Hello-World-quot-Part-I/ba-p/28983/page/2
You have to use a custom list view :
check out this website
http://blog.sptechnolab.com/2011/02/01/android/android-custom-listview-items-and-adapters/
If you use images from android resources folder then you can use the
// get Drawable from resources folder
Resources res = context.getResources();
Drawable drawable = res.getDrawable( R.drawable.myImage );
ImageView mImageView.setImageDrawable( mDrawable );
// or
ImageView mImageView.setImageBitmap( mBitmap );
The ImageView is the one from your ListItems layout. I wrote for every ListView a own ListAdapter in which i inflate the special layout and set the data to the layout.
You need a custom listadapter if you want to have different images and this one is the best tutorial I have ever found on internet about this topic :)
Is it possible to pass both strings and drawables as objects in the same HashMap, and then use this hashmap to populate a ListView via SimpleAdapter ?
I want this because I first get JSON data which also contains the URL to a thumbnail. Then I download this thumbnail. Relevant code (I think):
for (...) {
...
InputStream is = (InputStream)content;
Drawable image = Drawable.createFromStream(is, "src");
// Hashmap
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("title", new String(jsonObject.getString("Title")));
map.put("thumb", image);
mylist.add(map);
}
ListAdapter adapter = new SimpleAdapter(getActivity(), mylist, R.layout.listitem,
new String[] { "title", "thumb"},
new int[] { R.id.title, R.id.thumb });
setListAdapter(adapter);
R.id.title = TextView, and R.id.thumb = ImageView
This works for the title string, but not for the drawable. Is this approach just stupid?
Thanks in advance.
you should create a XML layout file for your row representation in the list and a custom adapter that inflate your row
As far as I can tell from the SimpleAdapter documentation, ImageViews are supported only for binding with a resource identifier or a String, which (the String, I mean) must represent an image resource or an image URI. It won't work if you pass it directly a Drawable object.