2017-03-24 56 views
0

我試圖獲取Android資源ID並將ImageView src設置爲來自JSON數組的特定ID。從適配器中的字符串獲取Android資源ID

我用下面的代碼

String str = mDataset.get(position).icon_res.substring(0, mDataset.get(position).icon_res.lastIndexOf('.')); 
     int res = Resources.getSystem().getIdentifier("ic_" + str, "drawable", this.getClass().getPackage().getName()); 
     if(res == 0) 
     { 
      Log.d("is null", "null"); 
      Log.d("string = ", "ic_" + str); 
      Log.d("package", this.getClass().getPackage().getName()); 
     } 

但從某種原因,它的檢測資源詮釋爲0包的名字似乎是正確的,字符串是正確的,但由於某種原因,它沒有找到正確的繪製資源。我在這裏做錯了什麼?

編輯:

Full adapter code 

package com.xyz 

import android.app.Activity; 
import android.app.Application; 
import android.content.Context; 
import android.content.res.Resources; 
import android.graphics.drawable.Drawable; 
import android.media.Image; 
import android.support.constraint.ConstraintLayout; 
import android.support.v7.widget.RecyclerView; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ImageView; 
import android.widget.RelativeLayout; 
import android.widget.TextView; 

import java.util.ArrayList; 
import java.util.List; 

/** 
* Created by Kreso on 23.3.2017.. 
*/ 

public class CategoryAdapter extends RecyclerView.Adapter<CategoryAdapter.ViewHolder>{ 


    private List<Category> mDataset; 
    private Context context; 

    public CategoryAdapter(List<Category> categoriesList) { 
     mDataset = categoriesList; 
    } 


    // Provide a reference to the views for each data item 
    // Complex data items may need more than one view per item, and 
    // you provide access to all the views for a data item in a view holder 
    public class ViewHolder extends RecyclerView.ViewHolder { 
     // each data item is just a string in this case 
     public TextView txtHeader; 
     public ConstraintLayout holderLayout; 
     public ImageView icon; 

     public ViewHolder(View v) { 
      super(v); 
      txtHeader = (TextView) v.findViewById(R.id.firstLine); 
      holderLayout = (ConstraintLayout) v.findViewById(R.id.itemHolder); 
      icon = (ImageView) v.findViewById(R.id.icon); 
      context = v.getContext(); 
     } 
    } 

    /*public void add(int position, String item) { 
     mDataset.add(position, item); 
     notifyItemInserted(position); 
    }*/ 

    public void remove(int position) { 
     mDataset.remove(position); 
     notifyItemRemoved(position); 
     notifyItemRangeChanged(position, mDataset.size()); 
    } 

    // Provide a suitable constructor (depends on the kind of dataset) 
    /* public CategoryAdapter(ArrayList<String> myDataset) { 
     mDataset = myDataset; 
    }*/ 

    // Create new views (invoked by the layout manager) 
    @Override 
    public CategoryAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, 
                int viewType) { 
     // create a new view 
     View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item_row, parent, false); 
     // set the view's size, margins, paddings and layout parameters 
     ViewHolder vh = new ViewHolder(v); 
     return vh; 
    } 

    // Replace the contents of a view (invoked by the layout manager) 
    @Override 
    public void onBindViewHolder(ViewHolder holder, final int position) { 
     // - get element from your dataset at this position 
     // - replace the contents of the view with that element 
    final String name = mDataset.get(position).name; 
     holder.txtHeader.setText(mDataset.get(position).name); 
     //holder.icon.setImageResource(R.drawable.ic_beer); 
     String str = mDataset.get(position).icon_res.substring(0, mDataset.get(position).icon_res.lastIndexOf('.')); 
     int res = context.getResources().getSystem().getIdentifier("ic_" + str, "drawable", context.getPackageName()); 
     if(res == 0) 
     { 
      Log.d("is null", "null"); 
      Log.d("string = ", "ic_" + str); 
      Log.d("package", this.getClass().getPackage().getName()); 
     } 

     holder.icon.setImageResource(res); 
     holder.holderLayout.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      remove(position); 
     } 
    }); 

} 

    // Return the size of your dataset (invoked by the layout manager) 
    @Override 
    public int getItemCount() { 
     return mDataset.size(); 
    } 

} 
+0

分享您的適配器的完整代碼 –

+0

'Resources.getSystem()'不是您的軟件包的'Resources'。這就是爲什麼它沒有找到你的標識符。 –

+0

@MikeM。我應該使用什麼?這有點令人困惑,我第一次這樣做。我的回答給了我所需的結果,但如果你的精神更好,我想知道更多關於背景的信息。 –

回答

1

使用此代碼,從資源獲取的圖像解決這個問題:

int res = context.getResources().getIdentifier("ic_" + str, "drawable", context.getPackageName()); 
0

我已成功地通過使用略有不同的方法

// Remove file extension 
     String str = mDataset.get(position).icon_res.substring(0, mDataset.get(position).icon_res.lastIndexOf('.')); 
     // Replace - with underscore _ 
     str = str.replace("-", "_"); 

     int res = 0; 
     // Try to find the resource with that name (icons in drawable folder) 
     try { 
      res = R.drawable.class.getField("ic_" + str).getInt(null); 
     } catch (IllegalAccessException e) { 
      e.printStackTrace(); 
     } catch (NoSuchFieldException e) { 
      // if no icon is found 
      holder.icon.setImageResource(R.drawable.ic_coffee); 
     } 
     // Set icon resource 
     holder.icon.setImageResource(res);