Menu
Who Do Is
  • Home
  • What
  • How
  • Is
  • Can
  • Are
  • Does
  • Do
  • Why
  • Who
  • Where
  • Which
  • Which
  • Should
  • Will
  • When
  • What’s
  • Did
Who Do Is

[ANSWERED] android – How to use OnClick in View holder?

Posted on November 14, 2022

Solution 1 :

Put your click Listner in viewholder

@Override

public void onBindViewHolder(ViewHolder holder, int position)

{
    String item = itemList.get(position);
holder.textview.setText(item);
holder.textview.setTag(item);

GradientDrawable bgShape = (GradientDrawable) holder.button.getBackground();

if (ColorMap().containsKey(itemList.get(position)))
    bgShape.setColor(Color.parseColor(ColorMap().get(itemList.get(position))));
    holder.button.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)      //I've been debugging, this part does not work and just through.
            {
                int pos = getAdapterPosition();
                if (pos != RecyclerView.NO_POSITION)
                {
                    Intent intent = new Intent();
                    intent.putExtra("name", ColorMap().get(itemList.get((pos))));
                    colorSelectionActivity.setResult(Activity.RESULT_OK, intent);
                    notifyItemChanged(pos);
                    colorSelectionActivity.finish();
                }
            }
        });
}

Previously you asked recycler view item to respond on your click listner. But now you asked

holder.button.setOnClickListener

So on click of button only your color will change.

Solution 2 :

You should change this:

itemView.setOnClickListener(new View.OnClickListener()
{
    @Override
    public void onClick(View v)
    {
        ...
    }
});

to this:

itemView.button.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            ...
        }
    });

Problem :

What I wanted was…

When click the button in Main Activity, Color Selection Activity will run, and click the button in the recycler view there, the button in Main Activity will change to the color of the button clicked in the recycler view.

But the recyler view works well, but the OnCilck inside the View Holder doesn’t work.

I searched for a really long time. But I don’t know the reason. I would be very grateful if you could help me.

Color Selection Activity

adapter = new Adapter(this, itemList, ColorSelectionActivity.this);
listview.setAdapter(adapter);

Main Activity

public void onColorButtonClick(View v)
{
    Intent intent = new Intent(getApplicationContext(), ColorSelectionActivity.class);

    startActivityForResult(intent, GET_COLOR_REQUEST);
}

protected void onActivityResult(int requestCode, int resultCode, Intent intent, View v)
{
    super.onActivityResult(requestCode, resultCode, intent);

    if(requestCode == GET_COLOR_REQUEST)
    {
        if(resultCode == RESULT_OK)
        {
            GradientDrawable bgShape = (GradientDrawable) v.getBackground();

            bgShape.setColor(Color.parseColor(intent.getStringExtra("name")));
        }
    }
}

Adapter

public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder>
{
    private ArrayList<String> itemList;
    private Context context;
    private Activity colorSelectionActivity;

public Adapter(Context context, ArrayList<String> itemList, ColorSelectionActivity colorSelectionActivity)
{
    this.context = context;
    this.itemList = itemList;
    this.colorSelectionActivity = colorSelectionActivity;
}

private Map<String, String> ColorMap()
{
    Map<String, String> colorMap = new HashMap<>();

    colorMap.put("Anger", "#ff4343");
    colorMap.put("Confusion", "#ff8e43");
    colorMap.put("Exciting", "#ffff4f");
    colorMap.put("Normal", "#85e070");
    colorMap.put("Joy", "#ffa8ab");
    colorMap.put("Sad", "#80a9e0");
    colorMap.put("Tired", "#6662bf");
    colorMap.put("Melancholy", "#bd72db");

    return colorMap;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
    View view = LayoutInflater.from(context).inflate(R.layout.color_item, parent, false);

    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(ViewHolder holder, int position)
{
    String item = itemList.get(position);

    holder.textview.setText(item);
    holder.textview.setTag(item);

    GradientDrawable bgShape = (GradientDrawable) holder.button.getBackground();

    if (ColorMap().containsKey(itemList.get(position)))
        bgShape.setColor(Color.parseColor(ColorMap().get(itemList.get(position))));
}

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

public class ViewHolder extends RecyclerView.ViewHolder
{
    public TextView textview;
    public Button button;

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

        itemView.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)      //I've been debugging, this part does not work and just through.
            {
                int pos = getAdapterPosition();
                if (pos != RecyclerView.NO_POSITION)
                {
                    Intent intent = new Intent();
                    intent.putExtra("name", ColorMap().get(itemList.get((pos))));
                    colorSelectionActivity.setResult(Activity.RESULT_OK, intent);
                    notifyItemChanged(pos);
                    colorSelectionActivity.finish();
                }
            }
        });

        textview = itemView.findViewById(R.id.item_text);
        button = itemView.findViewById(R.id.item_button);
    }
}
READ  [ANSWERED] android - Flutter app simple login page , iam getting this error after selecting acc "'Future' is not a subtype of type '() => void'"
Powered by Inline Related Posts

Recent Posts

  • How can I play with my cat without toys?
  • What is a bag pipe band called?
  • Are Honda Civics actually fast?
  • Are Yankee candles toxic?
  • How do I pair my Michael Kors smartwatch with my Android?

Recent Comments

No comments to show.

Archives

  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuántas
  • ¿Cuánto
  • ¿Que
  • ¿Quién
  • 90” and 108” so you may have to round up to the nearest size.
  • and delete any Spotify folders from it. Once this is done
  • Android
  • Are
  • At
  • Bei
  • blink
  • C'est
  • Can
  • carbs
  • Comment
  • Did
  • Do
  • Does
  • During
  • For
  • Has
  • How
  • In
  • Is
  • Ist
  • Kann
  • Können
  • nouveau
  • On
  • or 108 inches.2020-08-03
  • Où
  • owning
  • Pourquoi
  • Puis-je
  • Quand
  • Quante
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • spiritual
  • tap the downward-facing arrow on the top left. A downward-facing arrow will appear underneath each song in the album; they'll turn green as the download completes.2020-07-28
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welcher
  • Welches
  • Welke
  • Wer
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Whose
  • Why
  • Wie
  • Will
  • Wo
  • Woher
  • you will receive two curtains each with the same measurements of width 66"" (168cm) x drop 54""(137cm).
  • you'll see a green downward-facing arrow next to each song.2021-02-26
©2023 Who Do Is | Powered by SuperbThemes & WordPress