title: Referencing or Creating a Drawable from its Res ID (Int) Value?
body: <p>I would like to be able to store a drawables INT value (ResourceID) in SQLite and later be able to pull it from the database and convert it to a drawable to be viewed in a listview.</p>
  
  <p>The following snippit generates 2 values, the ResID portion is a int value that I would like to be able to convert back to the drawable in another activity.\</p>
  
  <pre><code>Drawable myDrawable = getResources().getDrawable(
                      images[position]);
              Log.e("DRAWABLE", "DrawableId 1:   " + myDrawable);
              int resId = images[position];
              Log.e("DRAWABLE", "DrawableId 2:   " + resId);
  </code></pre>
  
  <p><strong>What should I do to convert ResID to a drawable? If possible</strong></p>
  
  <p><strong>PicturePickerFragment</strong></p>
  
  <pre><code>public class PicturePickerFragment extends DialogFragment {
  
  ListView listView;
  ArrayList&lt;RowItem&gt; rowItems = new ArrayList&lt;RowItem&gt;();
  // attach adapter to a list view
  // A String[] array that will hold the names of the items.
  public static final String[] descriptions = { "Baby", "Baking",
          "Barbershop", "Camping", "Conference Call", "Funeral", "Gambling",
          "Gardening", "Halloweeen", "Medicine", "Meeting", "Poker",
          "Christmas", "Wedding" };
  public static final Integer[] images = { R.drawable.baby,
          R.drawable.baking, R.drawable.barbershop, R.drawable.camping,
          R.drawable.conferencecall, R.drawable.funeral, R.drawable.gambling,
          R.drawable.gardening, R.drawable.halloween, R.drawable.medicine,
          R.drawable.meeting, R.drawable.poker, R.drawable.santa,
          R.drawable.wedding };
  
  Button btn_pic;
  
  @Override
  public Dialog onCreateDialog(Bundle savedInstanceState) {
  
      LayoutInflater inflater = getActivity().getLayoutInflater();
      View convertView = (View) inflater.inflate(R.layout.dialog_view, null);
  
      // defining the alertdialog
      AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
      builder.setView(convertView);
      builder.setTitle(R.string.event_type);
  
       builder.setPositiveButton(R.string.select_picture,
       new DialogInterface.OnClickListener() {
       @Override
       public void onClick(DialogInterface dialog, int which) {
       // call the method on the parent activity when
       // user click the positive button
  
       }
       });
  
      // populating the array list
      for (int i = 0; i &lt; descriptions.length; i++) {
          RowItem item = new RowItem(images[i], descriptions[i]);
          rowItems.add(item);
      }
  
      // defining listview and using array adapter
      listView = (ListView) convertView.findViewById(R.id.listViewFragment2);
      DrawableAdapter adapter = new DrawableAdapter(getActivity(), rowItems);
      listView.setAdapter(adapter);
      listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  
          @Override
          public void onItemClick(AdapterView&lt;?&gt; parent, final View view,
                  int position, long id) {
  
              // final int item = images[position];
              Drawable myDrawable = getResources().getDrawable(
                      images[position]);
              Log.e("DRAWABLE", "DrawableId 1:   " + myDrawable);
              int resId = images[position];
              Log.e("DRAWABLE", "DrawableId 2:   " + resId);
              Button b = (Button) getActivity()
                      .findViewById(R.id.btn_picture);
              b.setCompoundDrawablesWithIntrinsicBounds(null, myDrawable,
                      null, null);
          }
  
      });
  
      adapter.addAll(rowItems);
  
      return builder.create();
  }
  }
  </code></pre>
  
  <p><strong>CreateActivity</strong></p>
  
  <pre><code>public class CreateActivity extends Activity implements OnClickListener {
  
  EditText etTitle;
  Button btDate;
  Button btTime;
  Button btPic;
  
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_create);
      setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
  
      //onclicklistener
      findViewById(R.id.btn_confirm).setOnClickListener(this);
      findViewById(R.id.btn_back).setOnClickListener(this);
  
      etTitle = (EditText) findViewById(R.id.editTextTitle);
      btDate = (Button) findViewById(R.id.btn_date);
      btTime = (Button) findViewById(R.id.btn_time);
      btPic = (Button) findViewById(R.id.btn_picture);
  
  }
  
  // Will be called via the onClick attribute
  // of the buttons in main.xml
  public void onClick(View view) {
      switch (view.getId()) {
      case R.id.btn_confirm:
          String title = etTitle.getText().toString();
          String time = btTime.getText().toString();
          String date = btDate.getText().toString();
  
  
          Drawable drawable = btPic.getCompoundDrawables()[1];
          int 
  
  
          Log.e("DRAWABLE", "DrawableId 3:   " + drawable);
          //Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();
  
  
          Log.e("LOG", title);
          Log.e("LOG", time);
          Log.e("LOG", date);
  
  
          Bundle newBundle = new Bundle();
          newBundle.putString("TITLE", title);
          newBundle.putString("TIME", time);
          newBundle.putString("DATE", date);
          //newBundle.putInt
  
          //Trying to pass a drawable from one activity to another
          //newBundle.putParcelable("DRAWABLE", bitmap);
  
  
          Intent intent = new Intent(this, MainActivity.class);
          intent.putExtras(newBundle);
  
          setResult(RESULT_OK, intent);
  
          finish();
  
          break;
  
      case R.id.btn_back:
          finish();
          break;
      }
  
  }
  
  public void showTimePickerDialog(View v) {
      DialogFragment newFragment = new TimePickerFragment();
      newFragment.show(getFragmentManager(), "timePicker");
  }
  
  public void showDatePickerDialog(View v) {
      DialogFragment newFragment = new DatePickerFragment();
      newFragment.show(getFragmentManager(), "datePicker");
  }
  
  public void showPicturePickerDialog(View v) {
      DialogFragment newFragment = new PicturePickerFragment();
      newFragment.show(getFragmentManager(), "picturePicker");
  }
  
  }
  </code></pre>
  
  <p><strong>Main Activity</strong></p>
  
  <pre><code>public class MainActivity extends FragmentActivity implements OnClickListener {
  
  ListView listView;
  int lastIndex = -1;
  ArrayList&lt;Event&gt; lstEvents = new ArrayList&lt;Event&gt;();
  
  // detail view
  TextView tvTitle, tvTime, tvDate;
  ImageView ivPic;
  View vw_master;
  
  boolean _isBack = true;
  
  ImageButton add;
  
  String title;
  String date;
  String time;
  int resId;
  
  static final int PICK_CONTACT_REQUEST = 0;
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
  
      // // get detail controls
      tvTitle = (TextView) findViewById(R.id.textViewTitle);
      tvDate = (TextView) findViewById(R.id.textViewDate);
      tvTime = (TextView) findViewById(R.id.textViewTime);
      ivPic = (ImageView) findViewById(R.id.imageView1);
  
      add = (ImageButton) findViewById(R.id.add);
      add.setOnClickListener(this);
  
  
      /////////////////////////////////LISTVIEW////////////////////////////////////////
      // Create the adapter to convert the array to views
      EventAdapter adapter = new EventAdapter(this, lstEvents);
  
      // attach adapter to a list view
      listView = (ListView) findViewById(R.id.listViewFragment);
      listView.setAdapter(adapter);
      /////////////////////////////////LISTVIEW////////////////////////////////////////
  
  
  
  
      // /////////////////////////////DATABASE/////////////////////////////////////////////
      DatabaseHandler db = new DatabaseHandler(this);
      // /////////////////////////////DATABASE/////////////////////////////////////////////
  
      List&lt;Event&gt; events = db.getAllContacts();
  
      adapter.addAll(events);
      adapter.notifyDataSetChanged();
  
  
  
  
  
  
  }
  
  @Override
  public void onClick(View v) {
      // TODO Auto-generated method stub
      switch (v.getId()) {
      case R.id.add:
          Intent intent = new Intent(this, CreateActivity.class);
          startActivityForResult(intent, 100);
          break;
      }
  }
  
  @Override
  public void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
  
      // /////////////////////////////DATABASE/////////////////////////////////////////////
      DatabaseHandler db = new DatabaseHandler(this);
      // /////////////////////////////DATABASE/////////////////////////////////////////////
  
      // Create the adapter to convert the array to views
      EventAdapter adapter = new EventAdapter(this, lstEvents);
  
      // attach adapter to a list view
      listView = (ListView) findViewById(R.id.listViewFragment);
      listView.setAdapter(adapter);
  
      if (requestCode == 100) {
          if (resultCode == RESULT_OK) {
              Bundle b = data.getExtras();
              title = b.getString("TITLE");
              time = b.getString("TIME");
              date = b.getString("DATE");
  
              // retrieving bitmap from CreateActivity
              int drawable = b.getInt("DRAWABLE");
              //Bitmap bitmap = (Bitmap) b.getParcelable("DRAWABLE");
  
              // converting from bitmap to drawable
              //Drawable drawable = new BitmapDrawable(getResources(), bitmap);
  
              // Event newEvent = new Event();
              // newEvent.set_date(date);
              // newEvent.set_title(title);
              // newEvent.set_time(time);
  
              // set drawable
              // newEvent.set_drawable(drawable);
  
              // lstEvents.add(newEvent);
  
              // adapter.addAll(lstEvents);
              // adapter.notifyDataSetChanged();
  
              ///////////////////////////////DATABASE/////////////////////////////////////////////
              /**
               * CRUD OPERATIONS
               */
  
              Log.e("Insert: ", "Inserting ..");
              db.addEvent(new Event(0, title, time, date, drawable));
  
              // Reading all contacts
              Log.e("Reading: ", "Reading all contacts..");
              // List&lt;Event&gt; events = db.getAllContacts();
              List&lt;Event&gt; events = db.getAllContacts();
              adapter.addAll(events);
              adapter.notifyDataSetChanged();
  
              //logging all events
  
              for (Event ev : events) {
                  String log = "Id: " + ev.get_Id() + " ,Title: "
                          + ev.get_title() + " ,Date: " +           ev.get_date();
                  // Writing Contacts to log
                  Log.e("Name: ", log);
  
              }
  
              ///////////////////////////////DATABASE/////////////////////////////////////////////
          }
  
      }
  }
  }
  </code></pre>