From e9c3d924117df10f1c9018c4ab66e2f32d7c9c8c Mon Sep 17 00:00:00 2001 From: Jimin15 Date: Thu, 25 Apr 2024 07:02:19 -0700 Subject: [PATCH] change picture in SecondFragment --- .../com/example/proj2/Item_selection.java | 28 +++++++++++++++---- .../com/example/proj2/SecondFragment.java | 10 ++++++- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/com/example/proj2/Item_selection.java b/app/src/main/java/com/example/proj2/Item_selection.java index 28c966d..42ff2af 100644 --- a/app/src/main/java/com/example/proj2/Item_selection.java +++ b/app/src/main/java/com/example/proj2/Item_selection.java @@ -1,24 +1,42 @@ package com.example.proj2; +import android.content.Intent; import android.os.Bundle; +import android.view.View; +import android.widget.Button; +import android.widget.ImageView; import androidx.activity.EdgeToEdge; import androidx.appcompat.app.AppCompatActivity; import androidx.core.graphics.Insets; import androidx.core.view.ViewCompat; import androidx.core.view.WindowInsetsCompat; +import androidx.navigation.fragment.NavHostFragment; public class Item_selection extends AppCompatActivity { + Button m_btn; + ImageView imageView; + boolean i = true; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - EdgeToEdge.enable(this); setContentView(R.layout.activity_item_selection); - ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> { - Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars()); - v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom); - return insets; + + Button button1 = findViewById(R.id.button1); + button1.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + Intent intent = new Intent(Item_selection.this, SecondFragment.class); + + //The transmission of data for image modification. + intent.putExtra("imageResourceId", R.drawable.doran_blade); + + //move to SecondFragment + startActivity(intent); + } }); } + + } \ No newline at end of file diff --git a/app/src/main/java/com/example/proj2/SecondFragment.java b/app/src/main/java/com/example/proj2/SecondFragment.java index c562eb3..e6e7c99 100644 --- a/app/src/main/java/com/example/proj2/SecondFragment.java +++ b/app/src/main/java/com/example/proj2/SecondFragment.java @@ -133,10 +133,18 @@ public void onClick(View v) { } }); + // Get the passed image resource id + int imageResourceId = getArguments().getInt("imageResourceId", -1); - } + // Set the image + if (imageResourceId != -1) { + ImageView imageView = view.findViewById(R.id.itemImage1); + imageView.setImageResource(imageResourceId); + } + } + @Override public void onDestroyView() { super.onDestroyView();