java—通过bundle将数据从活动传递到片段?

vfhzx4xs  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(273)

我试图让mainactivity中插入列表视图的字符串通过一个bundle传递到列表视图片段中,但是按下按钮什么也做不了。我不确定是哪里出了问题,因为它以前显示的是片段,但没有传递数据(而且它与活动布局重叠)。
主活动.java

import androidx.fragment.app.Fragment;;
import android.os.Bundle;
import android.provider.Telephony;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import org.w3c.dom.Text;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    public Button login;
    public EditText name;
    public EditText email;
    public EditText address;

    String Name;
    String Email;
    String Address;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        login = (Button) findViewById(R.id.button);
        name = (EditText) findViewById(R.id.name);
        email = (EditText) findViewById(R.id.email);
        address = (EditText) findViewById(R.id.address);
        //try {
       //     Thread.sleep(5000);
        //} catch (InterruptedException e) {
       //     e.printStackTrace();
       // }

        Name = name.getText().toString();
        Email = email.getText().toString();
        Address = address.getText().toString();

        login.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Fragment fragment = new ListViewFragment();
                Bundle args = new Bundle();
                args.putString("Name", Name);
                args.putString("Email", Email);
                args.putString("Address", Address);
                fragment.setArguments(args);
                getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment);//, fragment, fragment.getClass().getSimpleName()).addToBackStack(null).commit();
            }
        });
    }
    private void ListViewFragment(){
        // create new ListViewFragment ("ListViewFragment.java" ...)
        ListViewFragment lvFragment = new ListViewFragment();
// create new FragmentManager
        FragmentManager fm = getSupportFragmentManager();
// create a FragmentTransaction to begin the transaction and replace the Fragment
        FragmentTransaction transaction = fm.beginTransaction();
// your animation...
        transaction.setCustomAnimations(R.animator.anim1, R.animator.anim2);
// replace your activity_main layout with your Fragment layout
        transaction.add(R.id.mainFrag, lvFragment);
        transaction.addToBackStack("");
        transaction.commit(); // save changes
      /*  Bundle info = new Bundle();
        info.putString("name", Name);
        info.putString("email", Email);
        info.putString("address", Address);

        Fragment fragment = new Fragment();
        fragment.setArguments(info);
        android.app.FragmentTransaction transaction = getFragmentManager().beginTransaction();
       // transaction.setCustomAnimations(R.animator.fade_in, R.animator.fade_out);
        transaction.addToBackStack(null);
        transaction.replace(R.id.account, fragment);
        transaction.commit();*/
    }
}

主活动.xml

<androidx.constraintlayout.widget.ConstraintLayout 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/mainFrag"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="Smart-Home Sensors System"
        android:textSize="24sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.931" />

    <EditText
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="100dp"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="Name"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.497"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

    <EditText
        android:id="@+id/email"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="Email"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.497"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/name" />

    <EditText
        android:id="@+id/address"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="Address"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.497"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/email" />
</androidx.constraintlayout.widget.ConstraintLayout>

listviewfragment.java文件

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;

import java.lang.reflect.Array;
import java.util.ArrayList;

import static android.provider.AlarmClock.EXTRA_MESSAGE;

public class ListViewFragment extends Fragment {
    TextView nameDisp;
    TextView emailDisp;
    TextView addressDisp;
   // String myValue = this.getArguments().getString("message");
    public static ListViewFragment newInstance(String name, String email, String password) {
        ListViewFragment f = new ListViewFragment();
        return f;
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String nname = getArguments().getString("Name");
        String eemail = getArguments().getString("Email");
        String aaddress = getArguments().getString("Address");
        View v = inflater.inflate(R.layout.mainmenu, container, false);
        nameDisp =getView().findViewById(R.id.name);
        emailDisp = getView().findViewById(R.id.email);
        addressDisp = getView().findViewById(R.id.address);

        nameDisp.setText("Name: "+ nname);
        emailDisp.setText("Email: "+ eemail);
        addressDisp.setText("Address: "+ aaddress);
        final ListView list = v.findViewById(R.id.list);
        ArrayList<String> arrayList = new ArrayList<>();
        arrayList.add("View sensor 1");
        arrayList.add("View sensor 2");
        arrayList.add("View login details");
        arrayList.add("Logout");
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(v.getContext(), android.R.layout.simple_list_item_1, arrayList);
        list.setAdapter(arrayAdapter);
        TextView textView = new TextView(v.getContext());
        textView.setText("Please select an option:");
        list.addHeaderView(textView);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            /*private void showFragment() {
                Fragment fragment = new Fragment();
                FragmentTransaction transaction = getFragmentManager().beginTransaction();
                transaction.addToBackStack(null);
                // transaction.setCustomAnimations(R.animator.fade_in, R.animator.fade_out);
                transaction.replace(R.id.account, fragment, "fragment");
                transaction.commit();
            }*/

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String clickedItem = (String) list.getItemAtPosition(position);
                Toast.makeText(getView().getContext(), clickedItem, Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(getView().getContext(), LoginFragment.class);
                intent.putExtra(EXTRA_MESSAGE, clickedItem);
                startActivity(intent);
            }
        });
        return v;
    }
    private void ListViewFragment(Fragment showinfo){
        Bundle arguments = getArguments();
        String userName = arguments.getString("Name");
        String userEmail = arguments.getString("Email");
        String userAddress = arguments.getString("Address");

        nameDisp =getView().findViewById(R.id.name);
        emailDisp = getView().findViewById(R.id.email);
        addressDisp = getView().findViewById(R.id.address);

        nameDisp.setText("Name: "+ userName);
        emailDisp.setText("Email: "+ userEmail);
        addressDisp.setText("Address: "+ userAddress);
        Fragment fragment = showinfo;
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        transaction.setCustomAnimations(R.animator.anim1, R.animator.anim2);
        transaction.addToBackStack(null);
        transaction.replace(R.id.account, fragment);
        transaction.commit();

    }
}

mainmenu.xml(片段的xml)

<androidx.constraintlayout.widget.ConstraintLayout
    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/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/nameDisp"
        android:layout_width="95dp"
        android:layout_height="34dp"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        android:layout_weight="1"
        android:text="TextView"
        app:layout_constraintBottom_toTopOf="@+id/list"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.552" />

    <TextView
        android:id="@+id/emailDisp"
        android:layout_width="90dp"
        android:layout_height="38dp"
        android:layout_weight="1"
        android:text="TextView"
        app:layout_constraintBottom_toTopOf="@+id/list"
        app:layout_constraintEnd_toStartOf="@+id/nameDisp"
        app:layout_constraintHorizontal_bias="0.758"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/addressDisp"
        android:layout_width="95dp"
        android:layout_height="34dp"
        android:layout_marginEnd="4dp"
        android:layout_marginRight="4dp"
        android:layout_weight="1"
        android:text="TextView"
        app:layout_constraintBottom_toTopOf="@+id/list"
        app:layout_constraintEnd_toStartOf="@+id/emailDisp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.421" />

    <ListView
        android:id="@+id/list"
        android:layout_width="381dp"
        android:layout_height="496dp"
        android:layout_marginTop="72dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/selectOption"
        android:layout_width="189dp"
        android:layout_height="98dp"
        android:layout_weight="1"
        android:hint="@string/testStringForToast"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/list" />
</androidx.constraintlayout.widget.ConstraintLayout>

另外,如果我可以问的话,是否可以保留传递到这个片段中的值,在另一个片段中显示这些相同的细节?我想使列表视图选项之一成为查看这些详细信息的选项,并删除listview片段中的textviews。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题