This question already has answers here:
How to lazy load images in ListView in Android
(41 answers)
Closed 9 years ago.
What's the way to create a listview with images on the left side and text right after it?
(Note: the images were previously downloaded from the Net)
Thanks in advance!
Here's a complete sample code Lazy load of images in ListView. You can reuse it.
If you have new items in the list you just call adapter.notifyDatasetChanged() and ListView will redisplay all items including the new ones.
The getView() method in adapter inflates item.xml and displays real data in it. You need to start with some basic ListView tutorial, such as the one at Android Series: Custom ListView items and adapters.
A ListView item can have it's own custom layout. When you create your adapter for the ListView you can pass in the layout id to the Adapter constructor. See SimpleAdapter and ArrayAdapter.
=> You will have to extend an Adapter and implement getView() to property set the image+text.
Hi this class is used to make the image to bind with list view use simple adater and use the following class
class MyViewBinder implements ViewBinder {
public boolean setViewValue(View view, Object data,String textRepresentation) {
if( (view instanceof ImageView) & (data instanceof Bitmap) ) {
ImageView iv = (ImageView) view;
Bitmap bm = (Bitmap) data;
iv.setImageBitmap(bm);
return true;
}
return false;
}
}
Related
i have one Fragment in that i defined one GridView,so iam attaching listener for the LinearLayout from GridView Adapter so i click the LinearLayout of the Adapter it should change the background color of selected layout..
First in your girdview adapter class take on variable exp.
int selected;
then make on method in grid view like that
public void selectedPosition(int postion)
{
selected = postion;
}
then you can also put a code in getview method of gridview. Like this
if(position==selected)
{
imageView.setBackgroundColor(Color.WHITE);
}
else
{
imageView.setBackgroundColor(Color.parseColor("#578FFF"));
}
now in onitemclick of grid view you can post like that
adapter.selectcrop(position);
adapter.notifyDataSetChanged();
here is a solution example I will give you step by step i think you can solve your problem using that.
I have viewflipper which contains 3 childs, one GridView and two custom ListView, every view has different adapter , and every adapter has a image loader with Universal Image Loader library.
The items are the same for all adapters, my goal is to show content in different way (grid , list , and big list), but in this way every image loads 3 times. Is there any way to load images once and show them to their childs?
So you need an object common to the three views, which manages image loading and holds memory. You have it: it's the Adapter. Use a single one and just switch layouts.
For instance, you could define this method inside the adapter:
int layoutResId;
public void changeLayout(int layoutResId) {
this.layoutResId = layoutResId;
notifyDataSetChanged(); //force the adapter to call getView() again
}
Then in your getView() method you just inflate the layout defined by layoutResId.
Well the thing that you seem to be asking is if you can pass the images along to the other adapters. It would be something like if image is not empty them use it in the other views, if not then use the image loader.
public class Constants {
public static Constants INSTANCE = new Constants();
public Constants() {
}
public Uri IMAGE_PATH = Uri.EMPTY;
public File IMAGE_FILE = null;
public Bitmap IMAGE = null;
}
Presumably you would choose only one of these kinds to save from your view. And then check them to see if constants possesses an image for you to display.
Hi friends i have a listview and the contents are fetched from a webservice call. In that webservice call, there are fields like
"OGType": "ORG" and "OGType": "GROUP"
If click a button, the listview must shows the item having "OGType": "ORG", and hide the item having "OGType": "GROUP". Hope you understand what i meant. Please anyone help me for that. Thanks in Advance.
Try to set new data (only with ORG) to adapter and then call
adapter.notifyDataSetChanged();
You can do it in your getView Method in your Adapter Class. That's the header
public View getView(int pos, View convertView, ViewGroup, parent)
There you can properly hide the element(s) you want, you know, using the method setVisibility()
For more help you can take a look here
You can create a custom adapter and pass data to it in the form of Array or ArrayList (ArrayList is better when dealing with Custom Adapters). Whenever you need to add or remove the data from ListView, just add or remove the item to or from you ArrayList and call notifyDataSetChanged() on your custom adapter and it will update the ListView automatically.
In your case, whenever you click a button, edit you ArrayList and call your custom adapter's method called notifyDataSetChanged() and that's it. You'll see every time you call this method ListView will refresh itself if you have made any changes to the data. Hope it helps.
NOTE - CUSTOM ADAPTER IS NOT COMPULSORY. ANY ADAPTER CAN BE USED e.g SimpleAdapter, ArrayAdapter etc.
You can use a visible list and filters lists. You should use "visible" for complete the BaseAdpter as always, then, you can change the pointer of visible to other list (all, filter...)
Don't worry by the memory, are pointers, you only have each element only once.
public class MyAdapter extends BaseAdapter {
private ArrayList<MyItem> visible;
private ArrayList<MyItem> all;
private ArrayList<MyItem> filter;
public MyAdapter(ArrayList<MyItem> items) {
all = items;
visible = all; //Set all as visible
filter = new ArrayList<Item>();
for (Item i : items)
if (i.getType().equals("ORG"))
filter.add(i);
}
//Complete adapter using "visible"
public void showOnlyOrg() {
visible = filter;
notifydatasetchanged();
}
}
The non hackish way will be to remove the items from your Collection which you use to generate the listview and then call notifyDataSetChanged();
I have SimpleAdapter in which I want to display an image from the Internet.
final String[] from = { ATTRIBUTE_NAME_TEXT, ATTRIBUTE_NAME_IMAGE,ATTRIBUTE_NAME_DESCRIPTION };
final int[] to = { R.id.tvText, R.id.ivImg,R.id.textDescr };
sAdapter = new SimpleAdapter(getActivity(), data, R.layout.itemvideo, from, to);
sAdapter.notifyDataSetChanged();
setListAdapter(sAdapter);
I fill the data in a separate thread
for (Element titles : title) {
m = new HashMap<String, Object>();
m.put(VideoList.ATTRIBUTE_NAME_TEXT, titles.select("a[href]").text());
m.put(VideoList.ATTRIBUTE_NAME_IMAGE, Uri.parse(titles.select("img").attr("abs:src")));
m.put(VideoList.ATTRIBUTE_NAME_DESCRIPTION,titles.select("div[style]").first().text());
m.put("link",titles.select("a[href]").attr("abs:href"));
data.add(m);
}
Displayed normal data adapter, but the picture is not displayed. if doing so
m.put(VideoList.ATTRIBUTE_NAME_IMAGE, R.drawable.ic_launcher);
then the standard output adapter icon android, but I need to display an image from the Internet
Hi you can use an Universal Image loader for this purpose, what you can do is make a custom list , before show a progress icon or dialog and later replace that with an image loaaded from the internet please visit
https://github.com/nostra13/Android-Universal-Image-Loader
It is much better to use a custom adapter as #Raghunandan suggested. You have much more control and it's not that hard.
First, create a class that extends ArrayAdapter and pass the type of object you want to handle:
public class MyCustomAdapter extends ArrayAdapter<MyObjectModel> {
Your adapter must contain a Collection (Lists are fine) of your MyModelObject objects. You can pass this list in the constructor of the adapter and keep a reference in a variable. Each object should have a property with the URL of an image.
Second, create a simple layout for each item in your list. Your layout must contain an ImageView somewhere:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" ... >
<ImageView
android:id="#+id/myImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
... />
Third, back to your adapter, you must implement the getView() method. This method returns a view, based on your custom layout, that contains the info from your MyObjectModel object at the given position in the list.
Inflate your layout : view = inflater.inflate(R.layout.custom_list_item, null);
Get your ImageView component : imageView = (ImageView)view.findViewById(R.id.myImage);
Get the object from the list : item = list.get(position);
Download the image in an asynctask or use a 3rd party tool such as Universal Image Loader, Picasso etc. Personally I use UrlImageViewHelper : UrlImageViewHelper.setUrlDrawable(imageView, item.imageURL, ...);
IMO it's the only way to have a nice and custom list because default Android options don't allow much customization.
Have a look at http://lucasr.org/2012/04/05/performance-tips-for-androids-listview/ for some tips to improve your custom adapter.
I have a ListView that should display items that contain some text and an image.
The data is loaded from a local SQLite db file (that contains the text and image URLs).
I'd like to:
Get the text, url from DB.
Asynchronously download the image from the URL
Bind both values to the ListView (using the SimpleCursorAdapter).
So far, i was able to read the values from DB, however i am not sure how i can run the bind only AFTER i have successfully loaded each image?
In other words, i'd like to asynchronously bind each element as it's loaded to the appropriate UI item.
Here is a nice example which shows how to this http://www.androidkit.com/loading-images-from-remote-server-over-http-on-a-separate-thread.
Briefly,
1) you need to have a Map<Url, Bitmap>.
2) Have a default image that is displayed when image data from server is not available yet.
3) Have onScroll listener for your ListView, to know which items are currently displayed.
4) First, download those that are being displayed.
5) Once an image is downloaded, call notifyDataSetChanged() to bind available Image to the view.
6) You can use Softreferences or LRUCache to avoid OutofMemoryException
I have solved a similar problem to this. I received a XML from server and store the information in a Database. After that I populated the list using the CursorAdapter. In my case I have both images and text.
To solve the problem in the cursor adapter I did something like this:
#Override
public void bindView(View v, Context ctx, Cursor c) {
TextView title = (TextView) v.findViewById(R.id.titleID);
title.setText(c.getString(c.getColumnIndex(yourColumName)));
ImageView i = (ImageView) v.findViewById(R.id.ImageID);
String s = c.getString(c.getColumnIndex(youtImageColumn));
imageLoader.DisplayImage(s,i);
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View v = inflater.inflate(R.layout.yourRowLayout, parent, false);
return v;
}
In this case the ImageLoader is an async lazy image loader gotten from here:
https://github.com/thest1/LazyList