gpt4 book ai didi

java - 如何将 fragment 的值设置为不同的 Activity

转载 作者:行者123 更新时间:2023-12-02 05:18:50 25 4
gpt4 key购买 nike

我正在 android studio 中创建一个应用程序,我可以在其中从 fragment 中的服务器获取一些数据。但我希望这些数据显示在另一个不同的 Activity 中。我尝试使用 LayoutInflater 来执行该任务,但收到错误。我是一名初级开发人员,这对我来说是一个相当新的话题,我尝试寻找解决方案,但没有一个有效。请协助。

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>

最佳答案

首先您必须开始 Activity ,否则您将得到 Null

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

然后您可以将数据传递给目标 Activity 中的任何方法,如本例所示

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

关于java - 如何将 fragment 的值设置为不同的 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56278759/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com