【发布时间】:2016-04-07 19:30:50
【问题描述】:
我正在学习 Android,但我的适配器有问题。我可以在适配器中获取文本,但是设置图像源时遇到问题。
[This is the result if i comment the code ]
[this is an example of what i want but setting the images from my array.
我在arrays.xml中使用带有可绘制项目的整数数组
<integer-array name="champ_image">
<item>@drawable/aatrox</item>
<item>@drawable/ahri</item>
<item>@drawable/akali</item>
<item>@drawable/alistar</item>
<item>@drawable/amumu</item>
<item>@drawable/anivia</item>
<item>@drawable/ashe</item>
</integer-array>
这是我的错误日志:
01-04 03:09:30.057 19392-19392/com.sagar.materialdesigndemo E/AndroidRuntime: FATAL EXCEPTION: main
01-04 03:09:30.057 19392-19392/com.sagar.materialdesigndemo E/AndroidRuntime: Process: com.sagar.materialdesigndemo, PID: 19392
01-04 03:09:30.057 19392-19392/com.sagar.materialdesigndemo E/AndroidRuntime: android.content.res.Resources$NotFoundException: Resource ID #0x0
01-04 03:09:30.057 19392-19392/com.sagar.materialdesigndemo E/AndroidRuntime: at android.content.res.Resources.getValue(Resources.java:1307)
01-04 03:09:30.057 19392-19392/com.sagar.materialdesigndemo E/AndroidRuntime: at android.content.res.Resources.getDrawable(Resources.java:826)
01-04 03:09:30.057 19392-19392/com.sagar.materialdesigndemo E/AndroidRuntime: at android.content.Context.getDrawable(Context.java:403)
01-04 03:09:30.057 19392-19392/com.sagar.materialdesigndemo E/AndroidRuntime: at android.support.v4.content.ContextCompatApi21.getDrawable(ContextCompatApi21.java:26)
01-04 03:09:30.057 19392-19392/com.sagar.materialdesigndemo E/AndroidRuntime: at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:321)
01-04 03:09:30.057 19392-19392/com.sagar.materialdesigndemo E/AndroidRuntime: at android.support.v7.internal.widget.TintManager.getDrawable(TintManager.java:177)
01-04 03:09:30.057 19392-19392/com.sagar.materialdesigndemo E/AndroidRuntime: at android.support.v7.internal.widget.TintManager.getDrawable(TintManager.java:170)
01-04 03:09:30.057 19392-19392/com.sagar.materialdesigndemo E/AndroidRuntime: at android.support.v7.widget.AppCompatImageHelper.setImageResource(AppCompatImageHelper.java:53)
01-04 03:09:30.057 19392-19392/com.sagar.materialdesigndemo E/AndroidRuntime: at android.support.v7.widget.AppCompatImageView.setImageResource(AppCompatImageView.java:74)
01-04 03:09:30.057 19392-19392/com.sagar.materialdesigndemo E/AndroidRuntime: at com.sagar.adapters.ChampAdapter.onBindViewHolder(ChampAdapter.java:55)
01-04 03:09:30.057 19392-19392/com.sagar.materialdesigndemo E/AndroidRuntime: at com.sagar.adapters.ChampAdapter.onBindViewHolder(ChampAdapter.java:15)
01-04 03:09:30.057 19392-19392/com.sagar.materialdesigndemo E/AndroidRuntime: at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5212)
这是我的适配器,名为ChampAdapter,我认为问题出在:holder.champImg.setImageResource(champItem.get(position).champImg); //HERE
public class ChampAdapter extends RecyclerView.Adapter<ChampAdapter.ViewHolder> {
public List<ChampionItemModel> champItem;
public ChampAdapter(List<ChampionItemModel> champItem){this.champItem = champItem;}
public class ViewHolder extends RecyclerView.ViewHolder{
TextView champName;
TextView roleChamp;
ImageView champImg;
public ViewHolder(View itemView) {
super(itemView);
this.champName = (TextView)itemView.findViewById(R.id.champ_name);
this.roleChamp = (TextView)itemView.findViewById(R.id.champ_role);
this.champImg = (ImageView)itemView.findViewById(R.id.champ_image);
}
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_champs,parent,false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.itemView.setClickable(true);
holder.itemView.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
}
});
holder.champName.setText(champItem.get(position).champName);
holder.roleChamp.setText(champItem.get(position).roleChamp);
holder.champImg.setImageResource(champItem.get(position).champImg); //HERE IS THE PROBLEM
}
@Override
public int getItemCount() { return champItem.size();}
}
这是剩下的代码
-
型号:
public class ChampionItemModel { public String champName; public String roleChamp; public int champImg; public ChampionItemModel(String champName, String roleChamp, int champImg){ this.champName = champName; this.roleChamp = roleChamp; this.champImg = champImg; } } -
片段方法调用
initializeChampsItemList:private void initializeChampsItemList(){ ChampionItemModel championItemModel; String[] champsNames = getResources().getStringArray(R.array.champ_name); String[] champsRoles = getResources().getStringArray(R.array.champ_role); int[] champsImages = getResources().getIntArray(R.array.champ_image); final int length = champsNames.length; for(int i=0;i<length;i++){ championItemModel = new ChampionItemModel(champsNames[i], champsRoles[i], champsImages[i]); champItems.add(championItemModel); } }
【问题讨论】:
-
显示
champ_image数组xml -
我已经用 arrays.xml 更新了帖子
-
查看我的回答,帮助您解决问题。谢谢
标签: android android-studio android-recyclerview material-design