Adi Adi - 13 days ago 8
Android Question

Display image Id intead of image in android

I have a expandable recycler view which have some parent and some child items as like this.

enter image description here

I'm trying to add a image with every file name using like this code:

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()));


But it prints the image id(red circled area) instead of actual image. How to print here actual image?

Here is my adapter code:

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;
}
}
}


If any other info needed , please ask.

Answer

Your onBindViewHolder() method's case CHILD should look something like this

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

And your class Item should be something like this:

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;
    }
}

And while initializing it should be

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