I am trying to add image in this list view - android

public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.offers);
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
String xml = XMLfunctions.getX## Heading ##ML();
Document doc = XMLfunctions.XMLfromString(xml);
int numResults = XMLfunctions.numResults(doc);
if((numResults <= 0))
{
Toast.makeText(OffersActivity.this, "Geen resultaten gevonden", Toast.LENGTH_LONG).show();
finish();
}
ImageView newImg = (ImageView) findViewById(R.id.thumbimage);
NodeList nodes = doc.getElementsByTagName("result");
for (int i = 0; i < nodes.getLength(); i++)
{
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element)nodes.item(i);
map.put("id", XMLfunctions.getValue(e, "id"));
map.put("name", "" + XMLfunctions.getValue(e, "name"));
map.put("Score", "" + XMLfunctions.getValue(e, "score"));
map.put("thumbnail", "" + XMLfunctions.getValue(e, "thumbimg"));
mylist.add(map);
}
ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.listitems,
new String[] { "name", "Score", "thumbnail"},
new int[] { R.id.item_title, R.id.item_subtitle, R.id.thumbimage });
setListAdapter(adapter);
}
I have addresses of the images which i want to display are in
map.put("thumbnail", "" + XMLfunctions.getValue(e, "thumbimg"));
And i am also getting them. Actually they are coming from live xml. And i want to put them in a image view. Please help!
thanks

You need to download the images in the background and then set them in the correct ImageViews.
The adapters don't give you all that functionality by default, so you'll have to do some extra coding yourself.
Take a look at this StackOverflow answer: Lazy load of images in ListView and you'll see what I mean!

There is a List view named LazyListview.I think this will help you for sure.This Lazy List will help you to download the images in background.As you open the List Activity intialy you will see the the default image is loaded after downloading the image will be setting one by one which makes an awesome User Interface.
I think this code will help you a lot.... :-)

Related

Using the images of the server (ListFragment Android)

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!

How to use URL images in SimpleAdapter?

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());
}

android ListView Simple Adapter, item repeating

I created a custom ListView and in java filled that ListView with a SimpleAdapter. Here's my code:
setContentView(R.layout.activity_my);
list = (ListView) findViewById(R.id.lvList);
itemList = new ArrayList<HashMap<String, String>>();
itemMap = new HashMap<String, String>();
for (int i = 0; i < songs.length; i++) {
itemMap.put("song", songs[i]);
itemMap.put("artist", artists[i]);
System.out.println(songs[i] + " " + artists[i]);
itemList.add(itemMap);
}
SimpleAdapter adapter = new SimpleAdapter(this, itemList,
android.R.layout.simple_list_item_2, new String[] {"song",
"artist"}, new int[] { android.R.id.text1,
android.R.id.text2 });
list.setAdapter(adapter);
Now I created two String Arrays Song and Artist respectively and put them in a HashMap and then put the HashMap in a ArrayList called itemList. After that i set up the adapter with the default list item id's in the parameter.
The problem I am facing is that when i run the application, the list view shows only the last item and subItem of the String Arrays repeatedly. Here's the image for reference.
My ListView
I'd like to know why this is happening. I tried putting the itemList.add(itemMap); outside the for() loop but it still didn't work. Help would really be appreciated with a proper explanation, cause i'm kinda new to this. Thank You!
You can achieve this by recreating your itemMap object at every iteration. Try the following way.
itemMap = new HashMap<String, String>();
for (int i = 0; i < songs.length; i++) {
itemMap.put("song", songs[i]);
itemMap.put("artist", artists[i]);
System.out.println(songs[i] + " " + artists[i]);
itemList.add(itemMap);
}
Change this to
for (int i = 0; i < songs.length; i++) {
itemMap = new HashMap<String, String>();
itemMap.put("song", songs[i]);
itemMap.put("artist", artists[i]);
System.out.println(songs[i] + " " + artists[i]);
itemList.add(itemMap);
}
Explaination:
Refer here. Note HashMap is a key value pair which means it will store some value for a key. So HashMap will not allow duplicate key. From your code already youe have added the value for song and artist. So you can not add the value for these keys again. But the itemlist add the itemMap at every iteration. That is why the list repeating the same record. To avoid this problem simply re instantiate the itemMap object.
I hope this will help you.
bro, you won't believe but you are doing wrong.
make a new object of HashMap on each iteration.

Android setcolour listview particular row (only one particular row)

I want to set color for particular row in listview.That row will know at runtime. I ahve done list view like this :
ArrayList<SalesRoutes> routeList = getSalesRoute();
ArrayList<HashMap<String, String>> routhPath = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < routeList.size(); i++) {
if(Integer.parseInt(routeList.get(i).getOutlets()) >0){
HashMap<String, String> map = new HashMap<String, String>();
map.put("routeCode",((SalesRoutes) routeList.get(i)).getRouteCode());
map.put("routeName",((SalesRoutes) routeList.get(i)).getDescription());
map.put("outlets", ((SalesRoutes) routeList.get(i)).getOutlets());
routhPath.add(map);
}
}
ListView list = getListView();
sd = new SimpleAdapter(this, routhPath, R.layout.route_path,new String[] {"routeCode","routeName","outlets" },new int[] { R.id.routeCode,R.id.routeName,R.id.outlets});
row = getLayoutInflater().inflate(R.layout.route_path_row, null, false);
getListView().addHeaderView(row);
list.setAdapter(sd);
list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
list.setSelected(true);
//list.setSelection(0);
list.setTextFilterEnabled(true);
list.setItemsCanFocus(true);
list.setItemChecked(positions, true);
list.setSelectionAfterHeaderView();
Please tell me how can i do this...
Thanks in advance
One way is to use the index of the row you want to get like
getListView().getChildAt(index).setBackground(#ff0000);
Otherwise you would need to create a custom adapter and overwrite the getView method which is called before rendering each row. You can use that to check any conditions and set the background accordingly.
http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/
The above is a tutorial about that.

how to set images in listview with listactivity?

I want to set imagese in listview but I don't do it properly. What's wrong with images I dont know .plz help me my code is here.
final ArrayList<HashMap<String,String>> dataListfeture = new ArrayList<HashMap<String,String>>();
ArrayList<HashMap<String,String>> dataList = new ArrayList<HashMap<String,String>>();
for(int j=0; j<nodesfeture.getLength(); j++)
{
Element e1 =(Element)nodesfeture.item(j);
Node node = nodesfeture.item(j);
HashMap<String, String> mapfeautre = new HashMap<String, String>();
mapfeautre.put("name",node.getAttributes().getNamedItem("name").getNodeValue());
String rename = mapfeautre.put("name",node.getAttributes().getNamedItem("name").getNodeValue());
rename.replaceAll("", "%20");
mapnamelist= mapfeautre.put("name",node.getAttributes().getNamedItem("name").getNodeValue());
mapfeautre.put("distance",node.getAttributes().getNamedItem("distance").getNodeValue());
mapdistancelist =mapfeautre.put("distance",node.getAttributes().getNamedItem("distance").getNodeValue());
mapfeautre.put("venuetype",node.getAttributes().getNamedItem("venuetype").getNodeValue());
mapvenuelist=mapfeautre.put("venuetype",node.getAttributes().getNamedItem("venuetype").getNodeValue());
mapfeautre.put("imageupload",node.getAttributes().getNamedItem("imageupload").getNodeValue());
String strimageurl=mapfeautre.put("imageupload",node.getAttributes().getNamedItem("imageupload").getNodeValue());
mapimagelist="http://www.afterdarknightlife.com/"+strimageurl;
System.out.println("Image URL "+j+" :"+mapimagelist);
mylistfeature.add(mapfeautre);
}
System.out.println("data list = "+dataListfeture);
ListAdapter adapterfeaturetype = new SimpleAdapter(MainScreen.this, mylistfeature, R.layout.demophoto, new String[]
{
"imageupload","name", "distance","venuetype
},
new int[]
{
R.id.item_featureimage,
R.id.item_featurename,
R.id.item_featuredistance,
R.id.item_featurevenuetype,
});
may be this will help you...
http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List14.html
Please give GreedDroid a try. It has very advanced set of list view items (including an item with an image) which you can use from code and from xml as well.

Categories

Resources