Call drawable from Content Provider

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Call drawable from Content Provider

Postby Exeter » Tue Jan 05, 2010 1:11 am

Hi there. I'm hoping for a tutorial on how to dynamically pass an array of drawables (saved as string references in a table) from a Content Provider. Ideally, the Cursor would take the place of mThumbIds[position] below,


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.   imageview.setImageResource(mThumbIds[position]);
  3.  
  4.  
  5.     private Integer[] mThumbIds = {
  6.  
  7.     R.drawable.icon1,  R.drawable.icon2,  R.drawable.icon3,  R.drawable.icon4  
  8.  
  9.  
  10.     }
Parsed in 0.030 seconds, using GeSHi 1.0.8.4



I've been wrestling with it for a while but my limited knowledge of Java presents a challenge.


Best Regards,


Exeter
Exeter
Junior Developer
Junior Developer
 
Posts: 18
Joined: Tue Dec 22, 2009 4:17 pm

Top

Call drawable from Content Provider

Postby Exeter » Tue Jan 05, 2010 6:18 pm

The following code is untested b/c I'm still debugging my ContentProvider. Nevertheless, the idea is to pull R.drawable references from an SQLite database and display them in an ImageView.

This example stores the ResourceName b/c ResourceIds aren't permanent (adding or removing drawables causes reallocation).


*** Content Provider (com.example.ImageProvider) not included***


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. import android.app.Activity;
  2. import android.database.Cursor;
  3. import android.net.Uri;
  4. import android.widget.BaseAdapter;
  5. import android.content.Context;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8. import android.widget.GridView;
  9. import android.widget.ImageView;
  10. import android.widget.Toast;
  11.  
  12.  
  13. public class ImageAdapter extends BaseAdapter {
  14.     private Context mContext;
  15.  
  16.     public ImageAdapter(Context c) {
  17.         mContext = c;
  18.     }
  19.  
  20.     public int getCount() {
  21.         return cursor.getCount();
  22.     }
  23.  
  24.     public Object getItem(int position) {
  25.         return null;
  26.     }
  27.  
  28.     public long getItemId(int position) {
  29.         return 0;
  30.     }
  31.  
  32.     //Get Dataset from Database
  33.     Uri uri = com.example.ImageProvider.CONTENT_URI;
  34.     String[] projection = new String[] { Constants.KEY_ID, Constants.KEY_IMG };
  35.     String selection = null;
  36.     String[] selectionArgs = null;
  37.     String sortOrder = Constants.KEY_ID + " ASC";
  38.    
  39.    
  40.         Activity activity = new Activity();    
  41.         Cursor cursor = activity.managedQuery(uri, projection, // Which columns to return
  42.                 selection, // WHERE clause; which rows to return(all rows)
  43.                 selectionArgs, // WHERE clause selection arguments (none)
  44.                 sortOrder // Order-by clause (ascending by name)
  45.                 );
  46.  
  47.                 //Cursor cursor =  activity.getContentResolver().query(uri,projection, selection, selectionArgs, sortOrder);
  48.  
  49.                 Integer[] mThumbIds = CreateIntegerArray(mContext, cursor);
  50.  
  51.     // create a new ImageView for each item referenced by the Adapter
  52.     public View getView(final int position, View convertView, ViewGroup parent) {
  53.         ImageView imageview;
  54.  
  55.         if (convertView == null) {  // if it's not recycled, initialize some attributes
  56.             imageview = new ImageView(mContext);
  57.             imageview.setLayoutParams(new GridView.LayoutParams(30, 30));
  58.             imageview.setScaleType(ImageView.ScaleType.CENTER_CROP);
  59.             imageview.setPadding(0, 0, 0, 0);
  60.             //cursor.moveToPosition(position);      
  61.            
  62.         imageview.setOnClickListener(new View.OnClickListener() {
  63.  
  64.         //      @Override
  65.               public void onClick(View view){
  66.  
  67.                            Toast t;
  68.                           t = Toast.makeText(mContext, "Image " + position + " selected", Toast.LENGTH_SHORT);
  69.                            t.show();
  70.                     }
  71.                 });
  72.     }
  73.           else {
  74.             imageview = (ImageView) convertView;
  75.           }
  76.      
  77.         imageview.setImageResource(mThumbIds[position]);
  78.         return imageview;
  79.     }
  80.  
  81.  
Parsed in 0.041 seconds, using GeSHi 1.0.8.4



Method to enumerate IntegerArray...
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.    
  2.     private Integer[] CreateIntegerArray(Context mContext, Cursor cursor){
  3.                          int i = 0;
  4.                          StringBuilder sb = new StringBuilder();
  5.                     if (cursor != null) {
  6.                     /* Check if at least one Result was returned. */
  7.  
  8.                        if (cursor.isBeforeFirst()) {
  9.  
  10.                          //cursor.moveToFirst();
  11.                          /* Loop through all Results */
  12.                              do {
  13.                                   i++;
  14.                                   int resID = mContext.getResources().getIdentifier(
  15. cursor.getColumnIndex(Constants.KEY_IMG), "drawable", "com.example");
  16.                                   sb.append(resID);   // Converts Row to string and appends to end.
  17.                                   sb.append(",");     //  Array List that can be derived from a Cursor
  18.                              } while (cursor.moveToNext());
  19.                         }
  20.                     }
  21.  
  22.                          //Converts  Stringbuilder into Integer Array
  23.                          Integer[] thumbIds = { Integer.parseInt(sb.toString())};
  24.                          return thumbIds;
  25.     }
  26.  
  27. }
  28.  
  29.  
  30. }
Parsed in 0.040 seconds, using GeSHi 1.0.8.4
Exeter
Junior Developer
Junior Developer
 
Posts: 18
Joined: Tue Dec 22, 2009 4:17 pm

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 4 guests