Как отобразить только несколько элементов из всего списка переработчиков, основываясь на том, какая кнопка была нажата в предыдущем действии? - PullRequest
1 голос
/ 26 марта 2020

Я пытаюсь получить кнопки abs в body_workouts. java для отображения только тех упражнений, которые соответствуют каждой кнопке (которые являются первыми 3 элементами списка просмотра переработчика. Например - кнопка abs при нажатии должна go в список переработчика и отображать только "Велосипедные хрусты", "Подъемы ног" и "Мосты". Любая помощь?

Основной класс деятельности

 package com.example.stayfit;

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

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

public class MainActivity extends AppCompatActivity {

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

        Button myButton = findViewById(R.id.buttonMain);
        myButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                openWorkouts();

        };

        public void openWorkouts(){


        Intent myIntent = new Intent(getApplicationContext(), body_workouts.class);

            startActivity(myIntent);

        }
        });


        }



    }

Это класс body_workouts

package com.example.stayfit;

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

import android.content.Intent;
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";

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



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

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

           buttonAbs.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {

                    openWorkouts();

                };

                public void openWorkouts(){


                    Intent myIntents = new Intent(getApplicationContext(), exercise_adapter.class);

                    startActivity(myIntents);

                }
            });

            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);



                mRecyclerView = findViewById(R.id.rList);
                mRecyclerView.setHasFixedSize(true);
                mLayoutManager = new LinearLayoutManager(this);
                mRecyclerView.setLayoutManager(mLayoutManager);

                exercise_adapter.RecyclerViewClickListener listener = new exercise_adapter.RecyclerViewClickListener() {
                    @Override
                    public void onClick(View view, int position) {
                        launchDetailActivity(position);
                    }
                };

                mAdapter = new exercise_adapter(exercise.exerciseDetail(), listener);
                mRecyclerView.setAdapter(mAdapter);
            }

            private void launchDetailActivity(int position) {
                Intent intent = new Intent(this, exercise_detail.class);
                intent.putExtra(EXTRA_MESSAGE, position);
                startActivity(intent);
            }

        }

Это класс упражнений

package com.example.stayfit;

import java.util.ArrayList;

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("Bicycle Crunches", 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("Leg Raises", 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("Bridges", 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("Burpees", 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("Side Twists", 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("Planks", 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("Bicycle Crunches", 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("Bicycle Crunches", 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("Bicycle Crunches", 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("Bicycle Crunches", 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("Bicycle Crunches", 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("Bicycle Crunches", 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("Bicycle Crunches", 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("Bicycle Crunches", 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("Bicycle Crunches", 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("Bicycle Crunches", 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("Bicycle Crunches", 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("Bicycle Crunches", 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("Bicycle Crunches", 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"));
            return elist;
        }
    }

Это адаптер повторного просмотра:

package com.example.stayfit;

    import android.content.Context;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.TextView;
    import androidx.recyclerview.widget.RecyclerView;
    import java.text.NumberFormat;
    import java.util.ArrayList;

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

   private ArrayList<exercise> myExercises;
    private RecyclerViewClickListener mListener;


    public exercise_adapter(ArrayList<exercise> elist, RecyclerViewClickListener listener) {
        myExercises = elist;
        mListener = listener;
    }


    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)
    @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)
    @Override
    public int getItemCount() {
        return myExercises.size();
    }
}

1 Ответ

0 голосов
/ 26 марта 2020
  1. Фильтровать данные, возвращаемые exercise.exerciseDetail() (Фильтровать их по свойству name), и сохранить их в переменной. Пример: filteredExercises
  2. Добавьте метод к вашему адаптеру:
public void swap(ArrayList<exercise> data) {
    myExercises.clear();
    myExercises.addAll(data);
    notifyDataSetChanged();     
}
Вызовите метод подкачки вашего адаптера внутри прослушивателя щелчков вашего типа упражнения.
Пример:
buttonArms.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            mAdapter.swap(filteredExercises);
        }
});

РЕДАКТИРОВАТЬ: Для фильтрации, пожалуйста, проверьте здесь: Фильтр пользовательских объектов в списке Array или List

...