Search code examples
androidjsonlazy-loadinguniversal-image-loader

How to Show Dynamic Data in LazyList or Universal Image Loader?


I'm testing both LazyList (LL) and Universal Image Loader (UIL) that I found here. Also I have successfully made some modifications to retrieve the URL's to load from a JSON document. The JSON is from Flickr. I got the url (for obtain photos) and more data like title, description and author from JSON. Before using these image loading tools I passed data to the view with a SimpleAdapter like this:

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

Then some code to put JSON DATA into itemsList, then

        ListAdapter adapter = new SimpleAdapter(this, itemsList,
            R.layout.item,
            new String[] { TAG_TITLE, TAG_DESCRIPTION, TAG_AUTHOR }, new int[] {
                    R.id.title, R.id.description, R.id.author });

    setListAdapter(adapter);

    // selecting single ListView item
    ListView list = getListView();


    // Launching new screen on Selecting Single ListItem
    list.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            // getting values from selected ListItem
            String title = ((TextView) view.findViewById(R.id.title)).getText().toString();
            String description = ((TextView) view.findViewById(R.id.description)).getText().toString();
            String author = ((TextView) view.findViewById(R.id.author)).getText().toString();

            // Starting new intent
            Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
            in.putExtra(TAG_TITLE, title);
            in.putExtra(TAG_DESCRIPTION, description);
            in.putExtra(TAG_AUTHOR, author);
            startActivity(in);
        }
    });

Following this guide. Now that I tried to merged my code with LL or UIL I can't make it work.

Those image loaders uses their own adapter with a GetWiew method. So finally here's the question: How do I pass my own dynamic data to their adapters?


To make it more clear, I want to show info like title, description and author at the right side of the photo instead of "item X" from picture bellow:

Example from LazyList


Solution

  • Use custom adapter:

    public class MySimpleAdapter extends SimpleAdapter {
    
        private List<String> mImageUrls;
    
        public MySimpleAdapter(Context context, List<? extends Map<String, ?>> data, List<String> imageUrls, int resource, String[] from, int[] to) {
            super(context, data, resource, from, to);
            mImageUrls = imageUrls;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = super.getView(position, convertView, parent);
    
            ImageView imageView = view.findViewById(R.id.my_image);
            ImageLoader.getInstance().displayImage(mImageUrls.get(position), imageView);
    
            return view;
        }
    }
    

    And pass imageUrls to constructor.