简体   繁体   English

Android Studio:如何在我的第二个活动中看到intent方法携带的信息?

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

Hi guys I am new to android development and I find the recyclerview quite confusing.大家好,我是 android 开发的新手,我发现 recyclerview 很混乱。 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


/**
 * 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 Make an interface to handle click listener in your adapter and register those listener in your activity. 1 创建一个接口来处理适配器中的单击侦听器,并在您的活动中注册这些侦听器。

2 Fix the intent keys as have duplicated keys 2 将意图键修复为具有重复键

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Android的第二个活动中显示信息 - How do I display information in my second activity in Android 我怎样才能通过 Intent 转移到 nex Activity - How can I move to nex Activity through Intent 如何获得我的按钮以在Android Studio中打开第二个活动? - How to get my button to open a second activity in Android Studio? 如何设置一个意图,以便我可以将数据发送到第二个活动,然后从那里发送到第三个活动? - How to set an intent so that I can send a data to the second activity and from there to the third activity? 我如何在此代码中插入intent方法,当单击按钮时它将显示第二个活动 - How i insert intent method to this code for when click button it will show second activity 当我打算获取 arrayList 的数据时,如何在第二个活动和 setText 和 Image 中获取这些数据? - When I intent the arrayList's data, and how can I get those data in the second activity and setText and Image? Android如何查看我的SQLite? - Android How can I see my SQLite? Android Studio Java:如果产品已经存在于我的购物车活动中,我该如何更新我的数量? - Android Studio Java: How can I update my quantity if the product already exist's in my Cart Activity? Android Studio-当满足“ if”时,如何触发意图 - Android Studio - How can I trigger an intent when an 'if' is fulfilled Activity 第二次通过 Intent 启动时崩溃 - Activity crashing the second time it is launched through intent
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM