Search code examples
androidandroid-recyclerviewrealmcustom-adapterrealm-mobile-platform

I am not able to call recyclerview adapter's functions


I have a recyclerView adapter named as CustomAdapter in the MainActivity, I am getting the data by parsing the JSON and this data is used to set the textview inCustomAdapter class.

When I start my application,I am not showing the recyclerView list and the adapter is not being called. So my problem is I am not able to call the customAdapter's functions like onBindViewHolder,onCreateViewHolder.

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

        Context context;
        RealmResults<Film> realmResults;
        View.OnTouchListener listener;
        Realm realm;

        public CustomAdapter(Context context, View.OnTouchListener listener) {
            this.context = context;
            this.listener = listener;
        }

        @NonNull
        @Override
        public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

            View root = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_liste, parent, false);
            MyViewHolder viewHolder = new MyViewHolder(root);
            return viewHolder;
        }

        @Override
        public void onBindViewHolder(@NonNull MyViewHolder holder, final int position) {

            Realm.init(context);
            RealmResults<Film> films = realm.where(Film.class).findAll();
            Film f = films.get(position);

            for(film film:filmler) {
                try {
                    holder.title.setText(film.getTitle());
                    holder.year.setText(film.getYear());
                    holder.poster.setImageURI(film.getPoster());

                    if (film.getPoster().toString().equals("N/A") == true) {
                        Picasso.get().load(R.drawable.noImage).fit().into(holder.image);
                    } else {
                        Picasso.get().load(film.getPoster()).into(holder.image);
                    }

                } catch (NullPointerException e) {
                    e.printStackTrace();
                }
            }
    @Override
        public int getItemCount() {

            if(realmResults==null)
            {
                return 0;
            }
            return realmResults.size();
        }

        public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnTouchListener {

            public TextView title, year;
            public ImageView image;
            public LinearLayout linearLayout;

            public MyViewHolder(@NonNull View itemView) {
                super(itemView);
                title = itemView.findViewById(R.id.title_id);
                year = itemView.findViewById(R.id.year_id);
                image = itemView.findViewById(R.id.image_id);
                linearLayout = itemView.findViewById(R.id.linearlayout_id);
                linearLayout.setOnTouchListener(listener);
            }

            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if(motionEvent.getAction() == MotionEvent.ACTION_SCROLL){
                    return true;
                }
                return false;
            }
        }
    }

My MainActivity Class:

final StringRequest stringRequest1 = new StringRequest(Request.Method.GET, url1, new Response.Listener<String>() {
  @Override
  public void onResponse(String response) {
          progressDialog.dismiss();
          JSONObject jsonObject1 = new JSONObject(response);
          String response = jsonObject1.getString("Response");

          if (response.equals("True") && jsonObject1.has("Search")==true) {

          JSONArray array1 = jsonObject1.getJSONArray("Search");
          for (int i = 0; i < array1.length(); i++) {

          final JSONObject films = array1.getJSONObject(i);

           realm1.executeTransactionAsync(new Realm.Transaction() {
              @Override
              public void execute(Realm realm1) {
                Film film = realm1.createObject(Film.class);
                  try {        
                       film.setTitle(filmler.getString("Title"));                                     
                       film.setYear(filmler.getString("Year"));                                 
                       film.setPoster(filmler.getString("Poster"));
                       realm1.copyToRealm(film);
                       } catch (JSONException e) {
                         e.printStackTrace();
                       }
                       }
                });

            }

             for (int j = 0; j < array1.length(); j++) {
                array1.remove(j);
             }
             adapter = new CustomAdapter(this, this);
             recycle1.setAdapter(adapter);

Solution

  • you are going wrong in onBindViewHolder. Try to pass list in constructor

       public CustomAdapter(Context context, View.OnTouchListener 
                                     listener,RealmResults<film> realmResults) {
            this.context = context;
            this.listener = listener;
            this.realmResults = realmResults;
        }
    

    and Remove this code from bindViewHolder

            Log.d(TAG, "onBindViewHolder : called.");
            Realm.init(context);
            //film filmler = filmList.get(position);
            //realm = Realm.getDefaultInstance();
            RealmResults<film> filmler = 
            realm.where(film.class).findAll();
            film f = filmler.get(position);
    

    don't call for loop there in onBindViewHolder for set data just do following

            @Override
        public void onBindViewHolder(@NonNull MyViewHolder holder, final int 
            position) {
                     film item = realmResults.get(position);
                     if(item!=null && item.isValid()){
                           holder.baslik.setText(item.getTitle());
                    holder.yil.setText(item.getYear());
                    holder.resim.setImageURI(item.getPoster());
    
                    if (item.getPoster().toString().equals("N/A")) {
                        Picasso.get().load(R.drawable.resimyok).fit().into(holder.resim);
                    } else {
    
                  Picasso.get().load(film.getPoster()).into(holder.resim);
                    }
                     }
                }
    

    Fix this below for getting response and save it to realm

              JSONArray array1 = jsonObject1.getJSONArray("Search");
                        Realm realm = Realm.getDefaultInstance();
                        realm.beginTransaction();
                        realm.createOrUpdateAllFromJson(filn.class, array1);
                        realm.commitTransaction();
    
                        //get all saved data from realm
                        RealmResults<T> results = 
                          realm.where(film.class).query.findAll();
                        adapter = new 
                 CustomAdapter(getApplicationContext(),MainActivity.this,results);
                        recycle1.setAdapter(adapter);
                        arama.getText().clear();