Doubt to pass an icon from ViewHolder to an activity

0

I have a reciclerView with a list and each element is a card_view with time information, including an icon, all that loads me well, in the onCreate of the adapter I put a listener so that when pressed an element of the view, go to another activity to visualize with more detail and more information the time of each day, the only thing that I can not send by the intent is the icon (ImageView) to show it, all the other data I send them well.

Constructor viewHolder :

public ForecastItemViewHolder(View itemView) {
    super(itemView);
    cardView = (CardView) itemView.findViewById(R.id.forecast_card);
    weatherIcon = (ImageView) itemView.findViewById(R.id.sweather_icon);    
   day = (TextView) itemView.findViewById(R.id.stext_view_card_day);
....................

....................

}

@Override
public void onBindViewHolder(final ForecastItemViewHolder forecastItemViewHolder, int position) {
    final ForecastItem forecastItem = forecastItemList.get(position);
    forecastItemViewHolder.day.setText(forecastItem.day);

// this function determines the icon that is displayed according to the value of forecastItem.icom

setWeatherIcon (forecastItemViewHolder, forecastItem, forecastItem.icon);

the function above makes the following instruction, according to the value of forecastItem.icon puts an icon or another

forecastItemViewHolder.weatherIcon.setImageResource(R.drawable.icon_01d)

.......

.......

}

The oncreateView that is where I call the show to see the detail of each day, but I do not know how to recover the icon and how to send it in the intent, I've tried things like these, but I have not achieved anything:

Drawable iconD = forecastItemViewHolder.weatherIcon.getDrawable();

Bitmap icon = forecastItemViewHolder.weatherIcon.getDrawingCache();
Bundle extras = new Bundle();

extras.putParcelable("imagebitmap", (Parcelable) iconD);

This is the oncreate of the Show task to show the detail:

public ForecastItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.forecast_card, parent, false);
    final ForecastItemViewHolder forecastItemViewHolder = new ForecastItemViewHolder(view);


    view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Intent intent = new Intent(context, ShowForecastActivity.class);
            String day = forecastItemViewHolder.day.getText().toString();
            intent.putExtra("DAY", day);
           context.startActivity(intent);
                   .................

}

This is the oncreate of the show, here I do not know how to recover the icon if I could send it by the intent.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_show_forecast);

    textViewValueDay = (TextView) findViewById(R.id.text_view_card_day);
     Intent intent = getIntent();
    String day = intent.getStringExtra("DAY");
    textViewValueDay.setText(day);
     ......................

}
    
asked by Francisco Pagan 26.11.2016 в 18:50
source

1 answer

0

To be able to pass your icon to your detail, you must create an interface

import android.view.View;

  /**
   * A click listener for items.
   */
  public interface RecyclerOnItemClickListener {
      /**
       * Called when an item is clicked.
       *
       * @param childView View of the item that was clicked.
       * @param position  Position of the item that was clicked.
       */
      public void onItemClick( int position);
  }

Implement this interface in your activity and overwrite the method

@Override     public void onItemClick (int position)     {

    ForecastItem forecastItem = forecastItemList.get(position);
    //Aqui recuperas a la cadena de tu icono y lo envias en el intent hacia tu    detalle
}

Initialize the listener in your adapter's constructor

mItemClickListener = recyclerOnItemClickListener;

In your viewHolder, implement the onClickListener and pass the listener value to the view you want the event to have, for example

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

View rootView;


public ViewHolder(View itemView) {
    super(itemView);

    rootView =  itemView.findViewById(R.id.rootView);
    rootView.setOnClickListener(mItemClickListener);

}

    @Override
    public void onClick(View v)
    {
        if (mItemClickListener != null)
        {
            mItemClickListener.onItemClick( getPosition());
        }
    }

}

This way you can get your ForecastItem object in your activity and get the information you need.

I hope it serves you.

Greetings.

    
answered by 26.11.2016 / 19:43
source