简体   繁体   中英

how to set value from fragment to a different activity

I am creating an app in android studio, where I am able to fetch some data from the server in my fragment. But I want those data to be displayed in another different activity. I have try to use LayoutInflater to perform the task but I am receiving an error. I am a junior developer and this is quite new topic to me, I have try to search for a solution but none of them worked. kindly assist.

1.my fragment Codes

//These are some of my instance variables
public ImageView imageV;
public TextView txtV;
public LinearLayout linearLayout;




  @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view= inflater.inflate(R.layout.fragment_project_tab, container, false);

    mAuth= FirebaseAuth.getInstance();


        inflater = (LayoutInflater) getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);


        View myLayout = inflater.inflate(R.layout.activity_project_view, (ViewGroup) view, false);
        linearLayout = (LinearLayout) view.findViewById(R.id.linearlayout);

        imageV = (ImageView) myLayout.findViewById(R.id.imageV);
        txtV = (TextView) myLayout.findViewById(R.id.txtV);..............


//Below is the onClick method Where I want those data to be displayed 
  //to another activity.


  @Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

 DataSnapshot  myDataSnapShot  = uids.get(position);

  String Downloaduri = (String) myDataSnapShot.child("ImageLink").getValue();
  String title = (String) myDataSnapShot.child("Title").getValue();

    txtV.setText(title);

  Picasso.get().load(Downloaduri).into(imageV, new com.squareup.picasso.Callback() {………... }
      linearLayout.addView(imageV);
      linearLayout.addView(txtV);



2. xml file of another activity
<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ProjectView">

<LinearLayout
    android:layout_width="match_parent"
    android:id="@+id/linearlayout"
    android:layout_height="match_parent"
    android:orientation="vertical" >



<ImageView
    android:id="@+id/imageV"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:layout_gravity="center"
    android:layout_centerHorizontal="true"
    app:srcCompat="@drawable/wheel" />

<TextView
    android:id="@+id/txtV"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/imageV" />
</LinearLayout>

First you have to Start the activity otherwise you will get Null

Intent intent = new Intent(Objects.requireNonNull(getActivity()).getBaseContext(),
            ExampleActivity.class);

Then you can pass the data to any method in the target Activity like this example

((ExampleActivity)getContext()).parsData(data);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM