How to get a link to a resource in the adapter, or how to do it when sending? The code for receiving data from JSON (fragment)

private static final String NOTE_ID = "itemID" ; private static final String NOTE_TITLE = "title" ; private static final String NOTE_TAGS = "tag" ; private static final String NOTE_IMAGE = "image" ; private final String TAG_FRAGMENT = "tag_fragment"; private JSONAdapter noteAdapter; private RecyclerView recyclerView; private LinearLayoutManager mLLM; private JSON note; private JSONObject jsonObject; private JSONArray jsonArray; private String mJson; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment final View rv = inflater.inflate( R.layout.fragment_list, container, false); //recyclerview/// noteAdapter = new JSONAdapter(); recyclerView = (RecyclerView) rv.findViewById(R.id.recyclerview); recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST)); mLLM = new LinearLayoutManager(recyclerView.getContext()); recyclerView.setLayoutManager(mLLM); recyclerView.setAdapter(noteAdapter); try { loadNotes(); } catch (JSONException e) { e.printStackTrace(); } return rv; } private void loadNotes() throws JSONException { jsonArray = new JSONArray(getJSONString()); for(int i=0; i < jsonArray.length(); i++){ jsonObject = jsonArray.getJSONObject(i); note = new JSON(jsonObject.getString(NOTE_TITLE), jsonObject.getString(NOTE_TAGS), jsonObject.getString(NOTE_ID), jsonObject.getString(NOTE_IMAGE)); noteAdapter.addNote(note); } noteAdapter.notifyDataSetChanged(); } private String getJSONString(){ if(getArguments() != null){ mJson = getArguments().getString(TAG_FRAGMENT); } if(mJson == null || mJson.isEmpty() || mJson.equals("")) return null; String json = null; try { InputStream is = getContext().getAssets().open(mJson); // go to main/asset directory to see notes.json int size = is.available(); byte[] buffer = new byte[size]; is.read(buffer); is.close(); json = new String(buffer, "UTF-8"); } catch (IOException ex) { ex.printStackTrace(); return null; } return json; } 

and sent to the adapter. It is necessary that the adapter has already received a link to the resource Example!

 Glide.with(holder.mImageView.getContext()) .load(holder.mBoundImage = notes.get(position).getImage()) .fitCenter() .into(holder.mImageView); 

himself note json

  { "title" : "TITLE OF NOTE", "tag" : "tag t", "itemID" : "1", "image" : "R.drawable.cheese_1" }, 

or how to do it through getResources().getIdentifier(res, "drawable", packageName); and I can change the "image" : "R.drawable.cheese_1" to "image" : "cheese_1"

    1 answer 1

    With the help of reflection, it is done like this:

     public static int getResId(String resName, Class<?> c) { try { Field idField = c.getDeclaredField(resName); return idField.getInt(idField); } catch (Exception e) { e.printStackTrace(); return -1; } } 

    Usage looks like this:

     getResId("icon", R.drawable.class); 

    The method you mention getIdentifier also uses reflection, but does it twice.

    • You can tell what's wrong, it swears in the log. holder.mBoundImage = getResId(notes.get(position).getImage(), Drawable.class); - djo
    • @djo resource name must be specified without "R.drawable." - Stas Lelyuk
    • Yes, I did, he even tried -1 on a clean sheet of paper (in principle, it doesn't matter anymore (I am through Bitmap, but I’m interested in finding out the method) - djo
    • Strange, is the name of the resource accurately indicated without errors? - Stas Lelyuk
    • resource name just a string and no file format? so did well as in the example exactly - djo