theOzmic theOzmic - 9 days ago 6
Android Question

How do I pass image from a network to another activity in recyclerview

I have a recyclerview displaying items from a json file online, the recyclerview also has a thumbnail image in each list item.

When I click on an item, it starts a new activity, how do I pass the thumbnail image fetched from the server to the new activity?

Here is my new activity code:

public class ItemModal extends AppCompatActivity {

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_item_modal);

TextView ItemNameV = (TextView) findViewById(R.id.textView);
TextView ItemDescV = (TextView) findViewById(R.id.textView2);
TextView ItemPriceV = (TextView) findViewById(R.id.textView3);
ImageView ItemImageV = (ImageView) findViewById(R.id.imageView);

String itemName = getIntent().getStringExtra("ItemName");
String itemDesc = getIntent().getStringExtra("ItemDesc");
String itemPrice = getIntent().getStringExtra("ItemPrice");
String itemImage = getIntent().getStringExtra("ItemImage");

ItemNameV.setText(itemName);
ItemDescV.setText(itemDesc);
ItemPriceV.setText(itemPrice);
ItemImageV.setId(Integer.parseInt(itemImage));
}



}


Here is the Adapter Code:

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

private Context context;
private LayoutInflater inflater;
List<DataItems> data= Collections.emptyList();
DataItems current;
int currentPos=0;

// create constructor to innitilize context and data sent from MainActivity
public AdapterItems(Context context, List<DataItems> data){
this.context=context;
inflater= LayoutInflater.from(context);
this.data=data;
}

// Inflate the layout when viewholder created
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view=inflater.inflate(R.layout.container_items, parent,false);
MyHolder holder=new MyHolder(view);
return holder;
}

// Bind data
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

// Get current position of item in recyclerview to bind data and assign values from list
MyHolder myHolder= (MyHolder) holder;
DataItems current=data.get(position);
myHolder.textItemName.setText(current.itemName);
myHolder.textDesc.setText(current.itemDesc);
myHolder.textPrice.setText("# " + current.price);
myHolder.textPrice.setTextColor(ContextCompat.getColor(context, R.color.colorAccent));

// load image into imageview using glide
Glide.with(context).load(current.itemImage)
.placeholder(R.drawable.ic_img_error)
.error(R.drawable.ic_img_error)
.into(myHolder.ivItem);

}

// return total item from List
@Override
public int getItemCount() {
return data.size();
}


class MyHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

TextView textItemName;
ImageView ivItem;
TextView textDesc;
TextView textPrice;

// create constructor to get widget reference
public MyHolder(View itemView) {
super(itemView);
textItemName = (TextView) itemView.findViewById(R.id.textItemName);
ivItem = (ImageView) itemView.findViewById(R.id.ivItem);
textDesc = (TextView) itemView.findViewById(R.id.textDesc);
textPrice = (TextView) itemView.findViewById(R.id.textPrice);

itemView.setOnClickListener(this);
}

@Override
public void onClick(View v) {

Intent intent = new Intent(context, ItemModal.class);
intent.putExtra("ItemName", textItemName.getText().toString());
intent.putExtra("ItemImage", ivItem.getId());
intent.putExtra("ItemDesc", textDesc.getText().toString());
intent.putExtra("ItemPrice", textPrice.getText().toString());
Toast.makeText(context, textDesc.getText().toString() , Toast.LENGTH_SHORT ).show();
context.startActivity(intent);
}
}

}


Here is the code starting the new activity:

public void onClick(View v) {

Intent intent = new Intent(context, ItemModal.class);
intent.putExtra("ItemName", textItemName.getText().toString());
intent.putExtra("ItemImage", ivItem.getId());
intent.putExtra("ItemDesc", textDesc.getText().toString());
intent.putExtra("ItemPrice", textPrice.getText().toString());
Toast.makeText(context, textDesc.getText().toString() , Toast.LENGTH_SHORT ).show();
context.startActivity(intent);
}


Here I am just passing the id of the image which is not helping.

Answer

MyHolder class:

class MyHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
    String imageUrl;
    // .....

    @Override
    public void onClick(View v) {
        // ...
        intent.putExtra("ItemImage", imageUrl);
        context.startActivity(intent);
    }
}

Adapter onBindViewHolder:

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

    // ...
    myHolder.imageUrl = current.itemImage;

}

ItemModal class:

public class ItemModal extends AppCompatActivity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_item_modal);
        // ...
        ImageView ItemImageV = (ImageView) findViewById(R.id.imageView);
        String itemImage = getIntent().getStringExtra("ItemImage");
        Glide.with(this).load(itemImage)
                .placeholder(R.drawable.ic_img_error)
                .error(R.drawable.ic_img_error)
                .into(ItemImageV);

    }
}
Comments