1

My application retrieves data from the Firebase Database and displays it in a RecyclerView. The app already displays the title, the headline and the 3 labels just fine. Additionally it should check if a title is the String "Test". If it is it should display a certain image.

This is my Adapter

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {

   Context context;
   ArrayList<NewsCard> newsCards;


   public  MyAdapter(Context c , ArrayList<NewsCard> n){
       context = c;
       newsCards = n;
   }
   @NonNull
   @Override
   public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
       return new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.row, parent, false));
   }

   @Override
   public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
       holder.title.setText(newsCards.get(position).getTitle());
       holder.headline.setText(newsCards.get(position).getHeadline());
       holder.label1.setText(newsCards.get(position).getLabel1());
       holder.label2.setText(newsCards.get(position).getLabel2());
       holder.lable3.setText(newsCards.get(position).getLabel3());

   }


   @Override
   public int getItemCount() {

       return newsCards.size();
   }

   class MyViewHolder extends RecyclerView.ViewHolder
   {

       TextView title, headline,label1,label2,lable3;
       ImageView icon;



       public MyViewHolder(@NonNull View itemView) {

           super(itemView);

           title = (TextView) itemView.findViewById(R.id.title);

           headline = (TextView) itemView.findViewById(R.id.headline);
           label1 = (TextView) itemView.findViewById(R.id.lable1);
           label2 = (TextView) itemView.findViewById(R.id.lable2);
           lable3 = (TextView) itemView.findViewById(R.id.lable3);
           icon = (ImageView) itemView.findViewById(R.id.iconT);

           String test = title.getText().toString();
           if (test == "Test"){
               icon.setImageResource(R.drawable.common_google_signin_btn_icon_dark_focused);

           }

       }
   }
}

As you can see I tried to extract the String from the TextView but somehow it isn't working even if there are titles with the value "Test".

 @Override
    public int getItemCount() {

        return newsCards.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder
    {

        TextView title, headline,label1,label2,lable3;
        ImageView icon;



        public MyViewHolder(@NonNull View itemView) {

            super(itemView);

            title = (TextView) itemView.findViewById(R.id.title);

            headline = (TextView) itemView.findViewById(R.id.headline);
            label1 = (TextView) itemView.findViewById(R.id.lable1);
            label2 = (TextView) itemView.findViewById(R.id.lable2);
            lable3 = (TextView) itemView.findViewById(R.id.lable3);
            icon = (ImageView) itemView.findViewById(R.id.iconT);

            String test = title.getText().toString();
            if (test == "Test"){
                icon.setImageResource(R.drawable.common_google_signin_btn_icon_dark_focused);

            }

        }
    }

I have already checked if the problem is with the RecyclerView itself but I am confident that that's not the reason why it isn't working.

Markus Kauppinen
  • 3,025
  • 4
  • 20
  • 30
nwpL
  • 39
  • 4
  • 3
    Does this answer your question? [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Markus Kauppinen Jun 01 '20 at 12:03

1 Answers1

3

Try to use if (test.equals("Test")) while comparing strings

== tests for reference equality (whether they are the same object).

.equals() tests for value equality (whether they are logically "equal").

Andrey Molochko
  • 727
  • 1
  • 6
  • 21