Error in (swap) resolve method when trying to set a buttonOnClick listener to open a filtered recycler view list

huangapple 未分类评论44阅读模式
英文:

Error in (swap) resolve method when trying to set a buttonOnClick listener to open a filtered recycler view list

问题

我正在尝试在body_workouts中设置一个按钮的点击监听器,以便打开一个已过滤的回收站列表,其中只包含字符串“planks”(过滤的数据),基于完整的数组列表。然后,我在回收站布局中使用了一个交换方法来清除现有的数组列表,并将过滤后的数组添加到回收站视图中,并通知适配器UI已更改。然而,我不确定我是否将交换和按钮的onClick方法放在了正确的Java文件中,因为我一直在得到“无法解析方法错误”的错误消息。

我对此还是个新手,对于任何帮助都会非常感激!我已经花了几个星期试图弄清楚,但真的在这个问题上卡住了 Error in (swap) resolve method when trying to set a buttonOnClick listener to open a filtered recycler view list

以下是您提供的代码的翻译:

这是 exercise 类

这是 exercise_adapter 类

这是 recycler_layout 类

这是 body_workouts 类

请注意,上述翻译仅包含您提供的代码内容,不包含额外的回答或解释。如果您有任何进一步的问题或需要其他帮助,请随时提问。

英文:

I am trying to set a button on click listener in body_workouts to open a filtered recycler list containing only the string "planks" (filtered data) based on a full array list. I am then using a swap method in recycler layout to clear the existing array list and add the filtered array to recycler view and notify the adapter that the UI has changed. However, I am not sure if I'm placing swap and onClick button methods in the right java files because I keep on getting a 'cannot resolve method error"?

I am new to this and any help would be very appreciated!! Have spent weeks trying to figure it out but really stuck on this Error in (swap) resolve method when trying to set a buttonOnClick listener to open a filtered recycler view list

This is the exercise class

import android.os.Build;

import androidx.annotation.RequiresApi;


import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

public class exercise {
    private String name;
    private int image;
    private String intensity;
    private String time;
    private String level;
    private String desc;


    public exercise() {
    }

    public exercise(String name, int image, String intensity, String time, String level, String desc) {
        this.name = name;
        this.image = image;
        this.intensity = intensity;
        this.time = time;
        this.level = level;
        this.desc = desc;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getImage() {
        return image;
    }

    public void setImage(int image) {
        this.image = image;
    }

    public String getIntensity() {
        return intensity;
    }

    public void setIntensity(String intensity) {
        this.intensity = intensity;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public String getLevel() {
        return level;
    }

    public void setLevel(String level) {
        this.level = level;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public static ArrayList<exercise> exerciseDetail() {
        ArrayList<exercise> elist = new ArrayList<>();
        elist.add(new exercise("Planks", R.drawable.Plank, "LOW", "2 sets - 60 seconds each", "Beginner", "The plank strengthens your spine, your rhomboids and trapezius, and your abdominal muscles, which naturally result in a strong posture as they grow in strength."));
        elist.add(new exercise("Bicycle Crunches", R.drawable.BicycleCrunches, "MEDIUM", "3 Sets - 10 Reps", "Beginner", "The primary muscles worked in the bicycle crunch are the rectus abdominus, hips, and obliques. Keeping your legs off the ground targets your lower abs. The rotation activates your obliques. The pedaling of your legs stimulates the hips. \n"));
        elist.add(new exercise("Mountain Climbers", R.drawable.MountainClimbers, "HIGH", "3 Sets - 30 Reps", "Beginner", "Mountain climbers are a compound cardio exercise that works several joints and muscles at the same time, from your neck down to your feet. In particular, it targets your triceps, deltoids, abs, back, hip flexors, quads, hamstrings and butt.\n"));
        elist.add(new exercise("Bird Dog", R.drawable.birdDog, "LOW", "2 Sets - 10 Reps", "Beginner", "The bird dog is a simple core exercise that improves stability, encourages a neutral spine, and relieves low back pain. It strengthens your core, hips, and back muscles. It also promotes proper posture and increases range of motion."));
        elist.add(new exercise("Superman", R.drawable.superman, "MEDIUM", "2 Sets - 15 Reps", "Beginner", "The Superman is one of the best exercises to strengthen your upper and lower back muscles. If done regularly, the Superman may help alleviate back pain that is related to weak back muscles. In addition to strengthening back muscles, the Superman works your glutes and your hamstring muscles. "));
        elist.add(new exercise("Back exercise", R.drawable.ic_launcher_background, "MEDIUM", "20 Reps or 1 Minute", "Beginner", "Bicycle crunches are a great way to target the rectus abdominis and the obliques in one easy exercise"));
        elist.add(new exercise("Bridge", R.drawable.bridge, "LOW", "3 Sets -10 Reps", "Beginner", "The basic bridge isolates and strengthens your gluteus (butt) muscles and hamstrings (back of the thigh). A basic bridge stretches the stabilizers of the posterior chain, including your hip abductors, gluteus maximus, and hamstrings. "));
        elist.add(new exercise("Squats", R.drawable.squats, "MEDIUM", "3 Sets -20 Reps", "Beginner", "Squats help build your leg muscles – quadriceps, hamstrings, and calves. These drills also create an anabolic environment, which promotes body-wide muscle building, improving muscle mass. Squats, and all of their variations, are a great exercise for the whole body"));
        elist.add(new exercise("Lunges", R.drawable.lunges, "HIGH", "2 Sets - 12 Reps", "Beginner", "A lunge is a single-leg bodyweight exercise that works your hips, glutes, quads, hamstrings, and core and the hard-to-reach muscles of your inner thighs. Lunges can help you develop lower-body strength and endurance while also improving balance, coordination, and stability."));
        elist.add(new exercise("Pushups", R.drawable.pushups, "LOW", "2 Sets - 10 Reps", "Beginner", "Push-ups are known as a fantastic upper-body strengthener, used to build endurance and power in the chest and shoulders. When done properly, however, the push-up activates the core — including the ab muscles — for stabilization."));
        elist.add(new exercise("Dips", R.drawable.dips, "MEDIUM", "2 Sets - 10 Reps", "Beginner", "A dip is an upper-body strength exercise. Narrow, shoulder-width dips primarily train the triceps, with major synergists being the anterior deltoid, the pectoralis muscles (sternal, clavicular, and minor), and the rhomboid muscles of the back. "));
        elist.add(new exercise("Decline Pushups", R.drawable.declinePushups, "HIGH", "2 Sets - 10 Reps", "Beginner", "A decline variation targets your upper chest and fronts of shoulders more aggressively than a regular push-up performed on level ground. Perform it in addition to a regular push-up as well as with other chest exercises to get the most well-rounded chest development."));
        elist.add(new exercise("Arm Circles", R.drawable.Arm_Circles, "LOW", "2 Sets - 20 Reps", "Beginner", "Arm circle is an easy exercise and is mostly done as a warm up before any strenuous exercise such as strength training. Practising arm circles targets your triceps, back, biceps and shoulders. Practising it daily will first reduce arm fat followed by toning of muscles"));
        elist.add(new exercise("Arm Raises", R.drawable.armRaises, "MEDIUM", "2 Sets - 20 Reps", "Beginner", "Arm Raise is an isolation exercise which isolates shoulder flexion. It primarily works the anterior deltoid, with assistance from the serratus anterior, biceps brachii and clavicular portions of the pectoralis major."));
        elist.add(new exercise("Up Down Plank", R.drawable.UpDownPlank, "HIGH", "3 Sets - 10 Reps", "Beginner", "The up and down plank strengthens and tones your core, glutes, arms, wrists, and shoulders. This exercise helps to improve your posture, tightens the midsection and boosts weight loss.\n"));
        elist.add(new exercise("Leg Raises", R.drawable.legRaise, "LOW", "3 Sets - 10 Reps", "Beginner", "The leg raise is a strength training exercise which targets the thigh and hip muscle. Because the abdominal muscles are used isometrically to stabilize the body during the motion, leg raises are also often used to strengthen the rectus abdominis muscle"));
        elist.add(new exercise("Squat Jumps", R.drawable.squatJump, "MEDIUM", "3 Sets - 5 Reps", "Beginner", "Jump squats increase your explosive power, improve upper and lower body strength, and burn calories faster than regular squats. Squat jumps and their variations help shed fat from the lower body, tone your butt and legs, and improve strength and balance\n"));
        elist.add(new exercise("Side Lunges", R.drawable.sideLunge, "HIGH", "2 Sets - 20 Reps", "Beginner", " Side lunges' lateral movement targets the inner and outer thighs and helps to strengthen and tone those areas of your legs. This is also an excellent move to strengthen your quads, hamstrings and glutes, which makes the side lunge a very complete lower body exercise"));
        return elist;

    }

    // CREATE NEW FILTERED ARRAY LIST
//this second list filters the data in the arraylist elist to store top 3 values for abs into a seperate variable called filtered exercises
    public static ArrayList<exercise> filterExercises(String abs) {
        ArrayList<exercise> filteredData = new ArrayList<>();

        for (int i = 0; i < exerciseDetail().size(); i++) {
            //for elist, get the position of the item, get name and if it contains/equals abs, add to filtered data
            if (exerciseDetail().get(i).getName().contains(abs)) {
                filteredData.add(exerciseDetail().get(i));
            }
        }
        return filteredData;
    }
}

This is the exercise_adapter class

import android.content.Context;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;

public class exercise_adapter extends RecyclerView.Adapter<exercise_adapter.ExerciseViewHolder> {

    public static ArrayList<exercise> myExercises;
    private RecyclerViewClickListener mListener;


    // adapter constructor which takes in list, and recycler list listener
    public exercise_adapter(ArrayList<exercise> elist, RecyclerViewClickListener listener) {
        myExercises = elist;
        mListener = listener;
    }

   
    //detect click and interpret click
    public interface RecyclerViewClickListener {
        void onClick(View view, int position);
    }

    public static class ExerciseViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        public TextView name, intensity, level;
        private RecyclerViewClickListener mListener;

        public ExerciseViewHolder(View v, RecyclerViewClickListener listener) {
            super(v);
            mListener = listener;
            v.setOnClickListener(this);
            name = v.findViewById(R.id.tvExerciseName);
            intensity = v.findViewById(R.id.tvExerciseIntensity);
            level = v.findViewById(R.id.tvExerciseLevel);
        }

        @Override
        public void onClick(View view) {

            mListener.onClick(view, getAdapterPosition());
        }
    }

    @Override
    public exercise_adapter.ExerciseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.exercise_list, parent, false);
        return new ExerciseViewHolder(v, mListener);
    }

    // Replace the contents of a view (invoked by the layout manager)
    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    public void onBindViewHolder(ExerciseViewHolder holder, int position) {
        exercise ex = myExercises.get(position);
        holder.name.setText(ex.getName());
        holder.level.setText(ex.getLevel());
        holder.intensity.setText(ex.getIntensity());

    }

    // Return the size of your dataset (invoked by the layout manager)

    public int getItemCount() {
        return myExercises.size();
    }
}

This is the recycler layout class

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import java.util.ArrayList;

public class recycler_layout extends AppCompatActivity {
    public static final String EXTRA_MESSAGE = "au.edu.unsw.infs3634.beers.MESSAGE";

   RecyclerView mRecyclerView;
 static RecyclerView.Adapter mAdapter;
     RecyclerView.LayoutManager mLayoutManager;

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

        mRecyclerView = findViewById(R.id.rList);
        mRecyclerView.setHasFixedSize(false);
        mLayoutManager = new LinearLayoutManager(getApplicationContext());
        mRecyclerView.setLayoutManager(mLayoutManager);
        //create a new listener for the recycler view list

        exercise_adapter.RecyclerViewClickListener listener = new exercise_adapter.RecyclerViewClickListener() {

            //on click of the listener ,a detail view will be launched based on its position
            @Override
            public void onClick(View view, int position) {

                launchDetailActivity(position);
            }
        };

        //the list data is contained in the exercise detail array list which displays all exercises
        mAdapter = new exercise_adapter(exercise.exerciseDetail(),listener);
        mRecyclerView.setAdapter(mAdapter);


        //when abs button is clicked, show me only the exercises values for abs
    }
    private void launchDetailActivity(int position) {
        Intent intent = new Intent(this, exercise_detail.class);
        intent.putExtra(EXTRA_MESSAGE, position);
        startActivity(intent);
    }

    public static void swap (ArrayList<exercise> data) {
        exercise_adapter.myExercises.clear();
        exercise_adapter.myExercises.addAll(data);
        mAdapter.notifyDataSetChanged();
    }
}


package com.example.stayfit2;
import android.content.Context;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;

public class exercise_adapter extends RecyclerView.Adapter<exercise_adapter.ExerciseViewHolder> {

    public static ArrayList<exercise> myExercises;
    private RecyclerViewClickListener mListener;
    public static Context mContext;

    // adapter constructor which takes in list, and recycler list listener and a context for a button
    public exercise_adapter(ArrayList<exercise> elist, RecyclerViewClickListener listener) {
        myExercises = elist;
        mListener = listener;
//        mContext = context;
    }

    //interface for buttonAbs
    public interface buttonPressedAbs {
        ArrayList<exercise> getAbs(ArrayList<exercise> array);
    }

    //detect click and interpret click
    public interface RecyclerViewClickListener {
        void onClick(View view, int position);
    }

    public static class ExerciseViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        public TextView name, intensity, level;
        private RecyclerViewClickListener mListener;

        public ExerciseViewHolder(View v, RecyclerViewClickListener listener) {
            super(v);
            mListener = listener;
            v.setOnClickListener(this);
            name = v.findViewById(R.id.tvExerciseName);
            intensity = v.findViewById(R.id.tvExerciseIntensity);
            level = v.findViewById(R.id.tvExerciseLevel);
        }

        @Override
        public void onClick(View view) {

            mListener.onClick(view, getAdapterPosition());
        }
    }

    @Override
    public exercise_adapter.ExerciseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.exercise_list, parent, false);
        return new ExerciseViewHolder(v, mListener);
    }

    // Replace the contents of a view (invoked by the layout manager)
    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    public void onBindViewHolder(ExerciseViewHolder holder, int position) {
        exercise ex = myExercises.get(position);
        holder.name.setText(ex.getName());
        holder.level.setText(ex.getLevel());
        holder.intensity.setText(ex.getIntensity());

    }

    // Return the size of your dataset (invoked by the layout manager)

    public int getItemCount() {
        return myExercises.size();
    }
}

This is the body_workout class

package com.example.stayfit2;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;


public class body_workouts extends AppCompatActivity {
    public static final String EXTRA_MESSAGE = "au.edu.unsw.infs3634.beers.MESSAGE";

    Button buttonGlutes;
    Button buttonArms;
    Button buttonLegs;
    Button buttonBack;
    Button buttonChest;

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

        Button buttonAbs = findViewById(R.id.buttonAbs);
        buttonAbs.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View v) {

                recycler_layout.mAdapter.swap(exercise.filterExercises("Planks"));

            }
        });

        buttonGlutes = findViewById(R.id.buttonGlutes);
        buttonArms = findViewById(R.id.buttonArms);
        buttonLegs = findViewById(R.id.buttonLegs);
        buttonBack = findViewById(R.id.buttonBack);
        buttonChest = findViewById(R.id.buttonChest);
    }
}

huangapple
  • 本文由 发表于 2020年4月10日 09:53:29
  • 转载请务必保留本文链接:https://java.coder-hub.com/61132912.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定