Kevin M.
Kevin M.

Reputation: 162

Android Firebase RecyclerView get Item

I have a recyclerView that populates from Firebase(so it uses Firebase own Recycler adapter), I want to get information about the objects displayed the same way I do with my Listview, which is a lot simpler in this case: just adding addOnItemTouchListener on the listView will do it. But the recyclerView doesn't have this method, a similar one is addOnItemTouchListener and I can't understand how can I get item information from it, as the methods don't include an index or some sort of identificacion for the rows.

I've tried searching on the problem but I lack the understanding of this "new" method, or I can't formulate the approrpiate question for this.

recyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
            @Override
            public boolean onInterceptTouchEvent(@NonNull RecyclerView recyclerView, @NonNull MotionEvent motionEvent) {
                Partida partida = (Partida) recyclerView.getChildItemId(motionEvent.getActionIndex());

                return false;
            }

            @Override
            public void onTouchEvent(@NonNull RecyclerView recyclerView, @NonNull MotionEvent motionEvent) {

            }

            @Override
            public void onRequestDisallowInterceptTouchEvent(boolean b) {

            }
        });

I expect to get the attributes of my class "Juego" but I don't know how to get it in the first place. Thanks.

Upvotes: 0

Views: 228

Answers (2)

SeyyedMojtaba
SeyyedMojtaba

Reputation: 1514

You can add a View.OnClickListener in onBindViewHolder of your adapter:

@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
    holder.itemView.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
          }
      }
}

Upvotes: 1

Vedprakash Wagh
Vedprakash Wagh

Reputation: 3712

You'll get the View from RecyclerView through LayoutManager that you've used.

Check out the code below:

View v = recyclerView.getLayoutManager().findViewByPosition(position);

With this you'll be able to get the View at position(but not the actual data object). You can extract the data from TextView or EditText if there's any using methods like

String name = ((TextView)v.findViewById(R.id.textViewId)).getText().toString();

Upvotes: 1

Related Questions