Note that there are some explanatory texts on larger screens.

plurals
  1. POnot getting resource id of images in my code
    primarykey
    data
    text
    <p>I am trying to make a custom list view with Imageviews in each row that would set their image resource through a string i get from database. The database only contains the name of the images and I need to load it from resources. I've tried all the methods to get resource by name but the function always returns 0. I cannot get my id based on name no matter what I do. This is the Custom adapter that I am using.... </p> <p>public class OwnAddptor extends BaseAdapter {</p> <pre><code>private LayoutInflater mInflater; String[] names; String[] detailarray; String[] adapimage; int size; Context mycont; public OwnAddptor(Context context,String[] poi_names,String[] details,String[] imageadap) { mInflater = LayoutInflater.from(context); this.names = poi_names; this.detailarray = details; this.adapimage = imageadap; this.mycont = context; } @Override public int getCount() { // TODO Auto-generated method stub return names.length; } @Override public Object getItem(int puhj) { // TODO Auto-generated method stub return puhj; } @Override public long getItemId(int arg0) { // TODO Auto-generated method stub return arg0; } @Override public View getView(int position, View convertView, ViewGroup parent) { View vi=convertView; ViewHolder holder; if (vi == null) { vi = mInflater.inflate(R.layout.blockview, null); holder = new ViewHolder(); holder.headline = (TextView) vi.findViewById(R.id.head_line); holder.detail = (TextView) vi.findViewById(R.id.detail); holder.image = (ImageView) vi.findViewById(R.id.image); vi.setTag(holder); } else { holder = (ViewHolder) vi.getTag(); } holder.headline.setText(names[position]); holder.detail.setText(detailarray[position]); // Drawable imgdraw = mycont.getResources().getDrawable(adapimage[position]); holder.image.setImageDrawable(getAndroidDrawable(adapimage[position], mycont)); System.out.println(adapimage[position]); return vi; } static class ViewHolder { TextView headline; TextView detail; ImageView image; } public int getImageId(Context context, String name) { System.out.println("current name"+name); return context.getResources().getIdentifier("drawable/"+name, null, context.getPackageName()); } static public Drawable getAndroidDrawable(String pDrawableName,Context cont){ int resourceId=Resources.getSystem().getIdentifier(pDrawableName, "drawable", cont.getPackageName()); System.out.println("resource ids****"+resourceId); if(resourceId==0){ return null; } else { return Resources.getSystem().getDrawable(resourceId); } } } </code></pre> <p>And this is the main activity where I query the database and pass the array to the custom adapter class. The text views set correctly according to the position. The only problem is in getting the int id of the images from the resource folder. public class MainActivity extends Activity {</p> <pre><code>DataBaseHelper baseHelper ; SQLiteDatabase database; OwnAddptor adapcustom; TextView detail; ListView view; TextView headline; String[] name; String[] images; String[] desc; int[] imgids; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); List&lt;RowItem&gt; rowItems; view = (ListView) findViewById(R.id.mylist); baseHelper = new DataBaseHelper(this); try { baseHelper.createDatabase(); } catch (IOException ioe) { throw new Error("Unable to create database"); } try { baseHelper.openDataBase(); } catch(SQLException sqle) { throw sqle; } database = baseHelper.getReadableDatabase(); Cursor cursor= database.rawQuery("Select * from tblArpoi_local", null); int i = 0; if (cursor.getCount()&gt;0) { cursor.moveToFirst(); name = new String[cursor.getCount()]; desc = new String[cursor.getCount()]; images= new String[cursor.getCount()]; imgids = new int[cursor.getCount()]; while (cursor.moveToNext()) { images[i]= "poi_"+cursor.getString(cursor.getColumnIndex("arPoiImageLink")); name[i] = cursor.getString(cursor.getColumnIndex("arPoiName")); desc[i] = cursor.getString(cursor.getColumnIndex("arPoiDescription")); i++; } System.out.println("****"+images.length); database.close(); cursor.close(); adapcustom = new OwnAddptor(this,name,desc,images); view.setAdapter(adapcustom); } } public static int getImageId(Context context, String name) { System.out.println("current name"+name); return context.getResources().getIdentifier("drawable/"+name, null, context.getPackageName()); } </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload