1 Replies - 181 Views - Last Post: 02 June 2019 - 04:40 AM

#1 awthz   User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 76
  • Joined: 13-February 17

On click listener in RecyclerView

Posted 02 June 2019 - 04:37 AM

Hi Guys,

I am really confuse why I am getting error on this code.

I implemented the an interface for me to be able to click on the recycler view


this is my addapter code
package com.assignment2.myapplication;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;

public class Adapter extends RecyclerView.Adapter<Adapter.AdapterViewHolder> {


    private ArrayList<StudentListPojo> listPojos;
    private OnRecyclerViewListener mOnRecyclerViewListener;

    Adapter(ArrayList<StudentListPojo> listPojos, OnRecyclerViewListener onRecyclerViewListener)
    {
        this.listPojos =listPojos;
        this.mOnRecyclerViewListener = onRecyclerViewListener;
    }


    //DECLARATION OF THE FIELDS
    public class AdapterViewHolder extends RecyclerView.ViewHolder implements View.onclickListener{
        private TextView mFname, mLname, mAge, mGender, mBirthday;
        OnRecyclerViewListener onRecyclerViewListener;

        private AdapterViewHolder(@NonNull View itemView, OnRecyclerViewListener onRecyclerViewListener) {
            super(itemView);

            mFname = itemView.findViewById(R.id.fnameTextView);
            mLname = itemView.findViewById(R.id.lnameTextView);
            mAge = itemView.findViewById(R.id.ageTextView);
            mGender = itemView.findViewById(R.id.genderTextView);
            mBirthday = itemView.findViewById(R.id.birthdayTextView);
            this.onRecyclerViewListener = onRecyclerViewListener;

            itemView.setonclickListener(this);


        }

        @Override
        public void onclick(View v) {
            onRecyclerViewListener.onRecyclerViewClick(getAdapterPosition());

        }
    }


    @NonNull
    @Override
    public Adapter.AdapterViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.activity_adapter, viewGroup, false);
        return new AdapterViewHolder(view, mOnRecyclerViewListener);
    }

    @Override
    public void onBindViewHolder(@NonNull Adapter.AdapterViewHolder adapterViewHolder, int i) {

        adapterViewHolder.mFname.setText(listPojos.get(i).getFname());
        adapterViewHolder.mLname.setText(listPojos.get(i).getLname());
        adapterViewHolder.mAge.setText(listPojos.get(i).getAge());
        adapterViewHolder.mGender.setText(listPojos.get(i).getGender());
        adapterViewHolder.mBirthday.setText(listPojos.get(i).getBirthday());


    }

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


    public interface OnRecyclerViewListener{
        void onRecyclerViewClick(int position);
    }

}






and this is my activity code

the error is at the line 062.

package com.assignment2.myapplication;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.widget.Toast;

import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;

public class DashBoardTeacher extends AppCompatActivity implements Adapter.OnRecyclerViewListener {

    DatabaseReference databaseReference;
    ArrayList<StudentListPojo> listPojos;
    RecyclerView recyclerView;
    SearchView searchView;
    BottomNavigationView bottomNavigationView;
    FirebaseAuth firebaseAuth;
    FirebaseUser firebaseUser;
    Adapter adapter;


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


    }


    @Override
    protected void onstart() {
        super.onstart();
        if (databaseReference != null)
        {
            databaseReference.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    if (dataSnapshot.exists())
                    {
                        listPojos = new ArrayList<>();
                        for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren())
                        {
                            listPojos.add(dataSnapshot1.getValue(StudentListPojo.class));
                        }
                        adapter =  new Adapter(listPojos, this);
                        recyclerView.setAdapter(adapter);
                    }
                    else
                    {
                        Toast.makeText(DashBoardTeacher.this, "No student on the list, Enter student first", Toast.LENGTH_LONG).show();
                    }
                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {
                    Toast.makeText(DashBoardTeacher.this, "Database Error, Please contact the developer", Toast.LENGTH_LONG).show();
                }
            });
        }
        else
        {
            Toast.makeText(DashBoardTeacher.this, "databaseReference != null", Toast.LENGTH_LONG).show();
        }

        if (searchView != null){

            searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
                @Override
                public boolean onQueryTextSubmit(String s) {
                    return false;
                }

                @Override
                public boolean onQueryTextChange(String s) {
                    search(s);
                    return true;
                }
            });
        }
        else
        {
            Toast.makeText(DashBoardTeacher.this, "SearchView != null ", Toast.LENGTH_LONG).show();
        }




    }

    public void search (String str){

        ArrayList<StudentListPojo> listPojos1 = new ArrayList<>();
        for (StudentListPojo object : listPojos1)
        {
            if (object.getFname().toLowerCase().contains(str.toLowerCase()) || object.getFname()
            .toUpperCase().contains(str.toUpperCase()))
            {
                listPojos1.add(object);
            }
            else
            {
                Toast.makeText(DashBoardTeacher.this, "Error", Toast.LENGTH_LONG).show();
            }
        }
        adapter = new Adapter(listPojos1, this);
        recyclerView.setAdapter(adapter);
    }



    private void activity(){

        databaseReference = FirebaseDatabase.getInstance().getReference().child("studentList");

        recyclerView = findViewById(R.id.dashBoardRecyclerView);
        searchView = findViewById(R.id.dashBoardSearchView);
        bottomNavigationView = findViewById(R.id.bottom_navigation);

        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {

                switch (menuItem.getItemId()){
                    case R.id.action_recents:
                        startActivity(new Intent(DashBoardTeacher.this, CreateStudent.class));
                        break;

                    case R.id.action_favorites:
                        firebaseAuth.getInstance().signOut();
                        startActivity(new Intent(DashBoardTeacher.this, LoginDashboard.class));
                        Toast.makeText(DashBoardTeacher.this, "favorites", Toast.LENGTH_SHORT).show();
                        break;

                    case R.id.action_nearby:
                        Toast.makeText(DashBoardTeacher.this, "nearby", Toast.LENGTH_SHORT).show();
                        break;

                }

                return true;
            }
        });
    }

    @Override
    public void onRecyclerViewClick(int position) {

    }
}



Any solutions will be a great help guys.
I will be forever in your debt.

Cheers

Is This A Good Question/Topic? 0
  • +

Replies To: On click listener in RecyclerView

#2 macosxnerd101   User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12650
  • View blog
  • Posts: 45,824
  • Joined: 27-December 08

Re: On click listener in RecyclerView

Posted 02 June 2019 - 04:40 AM

It would be helpful if you posted the entire error message, exactly as it appears. I would recommend doing this upfront; we can’t help you without it. “My code doesn’t work” isn’t useful for us to help you. With 70+ posts, you should know this.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1