大佬教程收集整理的这篇文章主要介绍了Android Studio:如何在我的第二个 Activity 中查看通过 Intent 方法携带的信息?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
大家好,我是 androID 开发的新手,我发现 recyclervIEw 很混乱。我目前正在尝试将一项活动的信息传递给另一项活动,但在我的第二项活动中,仅显示 cardvIEw。任何人都可以请帮我吗?非常感谢!
我的布局文件
<?xml version="1.0" enCoding="utf-8"?>
<androIDx.cardvIEw.Widget.CardVIEw xmlns:androID="http://scheR_810_11845@as.androID.com/apk/res/androID"
xmlns:app="http://scheR_810_11845@as.androID.com/apk/res-auto"
xmlns:tools="http://scheR_810_11845@as.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>
我的第二个活动
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());
}
}
我的适配器类
/**
* 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 制作一个接口来处理适配器中的点击监听器和 在您的活动中注册这些侦听器。
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);
如果你在某些地方声明意图键常量会更好
public static int INTENT_EXERCISE_NAME = "INTENT_EXERCISE_NAME"
用法:
intent.putExtra(INTENT_EXERCISE_NAME,exercisesArray.get(position).getExercise_name());
以上是大佬教程为你收集整理的Android Studio:如何在我的第二个 Activity 中查看通过 Intent 方法携带的信息?全部内容,希望文章能够帮你解决Android Studio:如何在我的第二个 Activity 中查看通过 Intent 方法携带的信息?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。