[英]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);
//
// }
}
}
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.