Search code examples
androidsqlitecachingsimplecursoradapteruniversal-image-loader

Using Images from Database with URL


I am caching all data I'm getting in my App inside a SQLite DB... Whenever the user doesn't have internet connection the data is loaded with a SimpleCursorAdapter from the database. The database also contains the URL of Images. The images are cached by the image loading framework I'm using (Universal-Image-Loader), so when I run the framework with the URL from the database it gets the cached image. My question is how I can call this method inside my SimpleCursorAdapter? Until now I have only been able to get all the texts from the database and attach them to TextViews...

Thanks for your help!


Solution

  • So I was finally able to get this done myself...

    I had to write a new method which extends CursorAdapter...

    There I had to assign all my Texts I got from my DB with the TextView. So I was also able to give my Image Loader the Link to the (cached) image to load...

    This is my code:

    private static final class MyCursorAdapter extends CursorAdapter {
    
        MyCursorAdapter(Context context, Cursor cursor, int flags) {
            super(context, cursor, flags);
    
            mInflater = LayoutInflater.from(context);
        }
    
        @Override
        public View newView(Context context, Cursor cursor, ViewGroup parent) {
            View v = mInflater.inflate(R.layout.row_twitter, parent, false);
            return v;
        }
    
        @Override
        public void bindView(View view, Context context, Cursor cursor) {
            TextView listtitle = (TextView) view.findViewById(R.id.listtitle);
            TextView listusername = (TextView) view.findViewById(R.id.listusername);
            TextView listdate = (TextView) view.findViewById(R.id.listdate);
    
            listtitle.setText(cursor.getString(cursor.getColumnIndex(DataDBAdapter.KEY_TWITTER_TWEET)));
            listusername.setText(cursor.getString(cursor.getColumnIndex(DataDBAdapter.KEY_TWITTER_USERNAME)));
            listdate.setText(cursor.getString(cursor.getColumnIndex(DataDBAdapter.KEY_TWITTER_DATE)));
    
            listtitle.setTextColor(Color.GRAY);
            listdate.setTextColor(Color.GRAY);
    
            ImageView iv = (ImageView) view.findViewById(R.id.image);
            imageLoader.displayImage(cursor.getString(cursor.getColumnIndex(DataDBAdapter.KEY_TWITTER_IMAGEURL)),
                    iv, options);
        }
    
        LayoutInflater mInflater;
    }
    

    And this actually works pretty well :)