Android Studio: how can I see the information carried through the intent method on my second activity?

Viewed 19

Hi guys I am new to android development and I find the recyclerview quite confusing. I am currently trying to get the information of one activity to another but on my second activity only the cardview shows up. Anyone could please help me with that? Many thanks!

My layout file


<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/card_view_parent"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="5dp"
    app:cardCornerRadius="5dp"
    app:cardElevation="10dp"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="10dp"
    app:cardBackgroundColor="#808080"
    tools:context=".ChosenExerciseActivity"
    android:elevation="0dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/selected_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:layout_centerHorizontal="true"
            android:layout_marginTop="10dp"
            android:src="@drawable/ic_launcher_background"></ImageView>

        <TextView
            android:id="@+id/selected_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:text="Exercise Name"
            android:textStyle="bold"

            android:layout_marginTop="5dp"
            android:layout_below="@id/selected_image"></TextView>

        <TextView
            android:id="@+id/selected_description"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/selected_name"
            android:layout_marginTop="7dp"
            android:text="Exercise description"
            android:textStyle="italic"
            android:layout_marginStart="5dp"></TextView>

    </LinearLayout>

</androidx.cardview.widget.CardView> 

My second activity


public class ChosenExerciseActivity extends AppCompatActivity {
    TextView exercise_name, exercise_description;
    ImageView exercise_image;

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

        exercise_name = findViewById(R.id.selected_name);
        exercise_description = findViewById(R.id.selected_description);
        exercise_image = findViewById(R.id.selected_image);



        Intent intent = getIntent();
        exercise_name.setText(intent.getStringExtra("selected_name"));
        exercise_description.setText(intent.getStringExtra("selected_description"));
        exercise_image.setImageResource(intent.getIntExtra("selected_image", 0));




        //System.out.println(exercise_name.getText().toString());




    }
} 

My adapter class


/**
 * Class responsible for
 */
public class ExerciseAdapter extends RecyclerView.Adapter<ExerciseAdapter.ExerciseViewHolder> {

    private ArrayList<Exercise> exercisesArray = new ArrayList<>();
    Context context;


    public ExerciseAdapter(ArrayList<Exercise> exercisesArray, Context context){
        this.exercisesArray = exercisesArray;
        this.context = context;
    } //constructor

    @NonNull
    @Override
    public ExerciseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.homepage_design, parent, false); //inflate the layout xml file and display it on the parent
        return new ExerciseViewHolder(view);
    }

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

        holder.bind(exercisesArray.get(position)); //bind method comes from the ProjectViewHolder inner class
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(v.getContext(), ChosenExerciseActivity.class);
                intent.putExtra( "text_view_exercise_name", exercisesArray.get(position).getExercise_name());
                intent.putExtra( "text_view_exercise_description", exercisesArray.get(position).getExercise_description());
                intent.putExtra( "text_view_exercise_description", exercisesArray.get(position).getExercise_image());
                v.getContext().startActivity(intent); //you cannot start an activity without a context(this) so that you a Context object which will refer to the activity you wish to retrieve the information
            }
        });
    }

    @Override
    public int getItemCount() {

        return exercisesArray.size();
    }

    class ExerciseViewHolder extends RecyclerView.ViewHolder {

        TextView text_view_exercise_name, text_view_exercise_description;
        ImageView image_view_exercise_image;
        CardView card_view_exercises;

        public ExerciseViewHolder(@NonNull View itemView){
            super(itemView);

            //itemView.setOnClickListener(this); //set the screen to capture the users click
            card_view_exercises = itemView.findViewById(R.id.card_view_parent);
            text_view_exercise_name = itemView.findViewById(R.id.text_view_exercise_name);
            text_view_exercise_description = itemView.findViewById(R.id.text_view_exercise_description);
            image_view_exercise_image = itemView.findViewById(R.id.image_view_exercise_image);

        }

        public void bind(Exercise exercise) {

            text_view_exercise_name.setText(exercise.getExercise_name());
            text_view_exercise_description.setText(exercise.getExercise_description());
            image_view_exercise_image.setImageResource(exercise.getExercise_image());


        }


//        @Override
//        public void onClick(View v) {
//            int position = getAdapterPosition();
//            Exercise chosenExercise = exercisesArray.get(position);
//
//        }
    }
} 

enter image description here

1 Answers

1 Make an interface to handle click listener in your adapter and register those listener in your activity.

2 Fix the intent keys as have duplicated keys

Intent intent = new Intent(v.getContext(), ChosenExerciseActivity.class);
intent.putExtra( "selected_name", exercisesArray.get(position).getExercise_name());
intent.putExtra( "selected_description", exercisesArray.get(position).getExercise_description());
intent.putExtra( "selected_image", exercisesArray.get(position).getExercise_image());
v.getContext().startActivity(intent);

Would be better if you declare intent key constants some where like

public static int INTENT_EXERCISE_NAME = "INTENT_EXERCISE_NAME"

usage :

intent.putExtra(INTENT_EXERCISE_NAME, exercisesArray.get(position).getExercise_name());
Related