Comparing text of items from a recyclerview

Asked

Viewed 91 times

-1

I wonder how to compare text of items of a recyclerView on android.

And if the texts are the same, render the text of the entry below GONE.

public void pegarTexto (){

    TextView um =  (TextView) mRecyclerView.findViewHolderForItemId(mAdapter.getItemId(-1)).itemView.findViewById(R.id.nomePais);
    TextView dois = (TextView) mRecyclerView.findViewHolderForItemId(mAdapter.getItemId(0)).itemView.findViewById(R.id.nomePais);

    String pais1 = um.toString();
    String pais2 = dois.toString();

    if (pais1 == pais2){

        dois.setVisibility(View.GONE);
        mAdapter.notifyDataSetChanged();


    }

Follows images of the problem.inserir a descrição da imagem aqui

In the image above items 2 and 3 have the same name as item 1, therefore they would be invisible (GONE).

In the image below I show how it should look.

inserir a descrição da imagem aqui

  • In the onBindViewHolder method, make a comparison with the previous one, only do a treatment for when it is the first position. If it is equal you place the setVisibility of the item that has the text for GONE

  • The doubt is really how to make this comparison, and how to infuse the previous item.

  • if(position > 0){ if(mList.get(position - 1).equals(Holder.tvDate.gettext().toString().Trim()){ Holder.tvDate.setVisibility(View.GONE); } }

  • I don’t know how you’re working, so I made an example with a string list

1 answer

0

== Tests the references of the two objects (if it is the same object in memory) .

.equals() Tests if the values of objects are equal (if they are different objects in memory, but with the same values/content).

Therefore, use the .equals() to compare.

Reference: https://stackoverflow.com/a/513839/4916458

Browser other questions tagged

You are not signed in. Login or sign up in order to post.