在Android中显示图片ID而不是图片

时间:2016-11-19 10:23:10

标签: android android-recyclerview android-arrayadapter android-imageview android-image

我有一个可扩展的回收站视图,它有一些父项和一些子项,就像这样。

enter image description here

我正在尝试使用以下代码添加每个文件名的图像:

  data.add(new ExpandableListAdapter.Item(ExpandableListAdapter.CHILD, obj1.optString("category").trim()+"  "+R.drawable.download48));
  data.add(new ExpandableListAdapter.Item(ExpandableListAdapter.CHILD, obj1.optString("filename").trim()));

但它会打印图像ID(红色圆圈区域)而不是实际图像。如何在这里打印实际图像?

这是我的适配器代码:

public class ExpandableListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    public static final int HEADER = 0;
    public static final int CHILD = 1;

    private List<Item> data;

    public ExpandableListAdapter(List<Item> data) {
        this.data = data;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int type) {
        View view = null;
        Context context = parent.getContext();
        float dp = context.getResources().getDisplayMetrics().density;
        int subItemPaddingLeft = (int) (18 * dp);
        int subItemPaddingTopAndBottom = (int) (5 * dp);
        LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        switch (type) {
            case HEADER:

                view = inflater.inflate(R.layout.list_header, parent, false);
                ListHeaderViewHolder header = new ListHeaderViewHolder(view);
                return header;
            case CHILD:
                view = inflater.inflate(R.layout.listchild, parent, false);
                ListChildViewHolder child = new ListChildViewHolder(view);
                return child;
        }
        return null;
    }

    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final Item item = data.get(position);
        switch (item.type) {
            case HEADER:
                final ListHeaderViewHolder itemController = (ListHeaderViewHolder) holder;
                itemController.refferalItem = item;
                itemController.header_title.setText(item.text);
                if (item.invisibleChildren == null) {
                    itemController.btn_expand_toggle.setImageResource(R.drawable.circle_minus);
                } else {
                    itemController.btn_expand_toggle.setImageResource(R.drawable.circle_plus);
                }
                itemController.btn_expand_toggle.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (item.invisibleChildren == null) {
                            item.invisibleChildren = new ArrayList<Item>();
                            int count = 0;
                            int pos = data.indexOf(itemController.refferalItem);
                            while (data.size() > pos + 1 && data.get(pos + 1).type == CHILD) {
                                item.invisibleChildren.add(data.remove(pos + 1));
                                count++;
                            }
                            notifyItemRangeRemoved(pos + 1, count);
                            itemController.btn_expand_toggle.setImageResource(R.drawable.circle_plus);
                        } else {
                            int pos = data.indexOf(itemController.refferalItem);
                            int index = pos + 1;
                            for (Item i : item.invisibleChildren) {
                                data.add(index, i);
                                index++;
                            }
                            notifyItemRangeInserted(pos + 1, index - pos - 1);
                            itemController.btn_expand_toggle.setImageResource(R.drawable.circle_minus);
                            item.invisibleChildren = null;
                        }
                    }
                });
                break;
            case CHILD:




                final ListChildViewHolder itemController1 = (ListChildViewHolder) holder;
                itemController1.refferalItem1 = item;
                itemController1.header_title1.setText(item.text);



                break;
        }
    }


    @Override
    public int getItemViewType(int position) {
        return data.get(position).type;
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    private static class ListHeaderViewHolder extends RecyclerView.ViewHolder {
        public TextView header_title;
        public ImageView btn_expand_toggle;
        public Item refferalItem;

        public ListHeaderViewHolder(View itemView) {
            super(itemView);
            header_title = (TextView) itemView.findViewById(R.id.header_title);
            btn_expand_toggle = (ImageView) itemView.findViewById(R.id.btn_expand_toggle);
        }
    }
    private static class ListChildViewHolder extends RecyclerView.ViewHolder {
        public TextView header_title1;
        public ImageView btn_expand_toggle1;
        public Item refferalItem1;

        public ListChildViewHolder(View itemView) {
            super(itemView);
            header_title1 = (TextView) itemView.findViewById(R.id.header_title1);
            btn_expand_toggle1 = (ImageView) itemView.findViewById(R.id.btn_expand_toggle1);
        }
    }
    public static class Item {
        public int type;
        public String text;
        public List<Item> invisibleChildren;

        public Item() {
        }

        public Item(int type, String text) {
            this.type = type;
            this.text = text;
        }
    }
}

2 个答案:

答案 0 :(得分:1)

添加此行

btn_expand_toggle1.setImageDrawable(getResources().getDrawab‌​le(R.drawable.ic_lau‌​ncher));

之后

final ListChildViewHolder itemController1 = (ListChildViewHolder) holder;
itemController1.refferalItem1 = item;
itemController1.header_title1.setText(item.text); 

它会起作用

答案 1 :(得分:1)

您的onBindViewHolder()方法的case CHILD应该看起来像这样

case CHILD:
        final ListChildViewHolder itemController1 = (ListChildViewHolder) holder;
        itemController1.refferalItem1 = item;
        itemController1.header_title1.setText(item.text);
        itemController1.btn_expand_toggle1.setImageResource(item.resId);
        break;

你的班级Item应该是这样的:

public static class Item {
    public int type;
    public String text;
    private int resId;
    public List<Item> invisibleChildren;

    public Item() {
    }

    public Item(int type, String text, @DrawableRes int resId) {
        this.type = type;
        this.text = text;
        this.resId = resId;
    }
}

初始化时应该是

data.add(new ExpandableListAdapter.Item(ExpandableListAdapter.CHILD, obj1.optString("category").trim(), R.drawable.download48));