将数据从 Fragment 传递到另一个 Fragment
Posted
技术标签:
【中文标题】将数据从 Fragment 传递到另一个 Fragment【英文标题】:Passing data From Fragment to another fragment 【发布时间】:2021-02-19 21:23:28 【问题描述】:我正在搜索数周,但我无法解决我的问题。我想将数据从一个片段传递到另一个片段。
我尝试过使用捆绑包和界面,但它们都不起作用。你能帮我解决这个问题吗?我将不胜感激 我想从这个片段中传递数据,它的名字是“FragmentGelir”
public class FragmentGelir extends Fragment
private Toolbar toolbarGelir;
private FloatingActionButton fabGelir;
private RecyclerView rvgelir;
private ArrayList<Gelirler> gelirlerArrayList;
private GelirlerAdapter adapter;
private Veritabani vt;
public double toplamgelir = 0.0;
public String anahtar;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
View rootView = inflater.inflate(R.layout.fragment_gelir, container, false);
anahtar = "test";
FragmentHomepage fragmentHomepage = new FragmentHomepage();
Bundle bundle = new Bundle();
bundle.putString("anahtar", anahtar);
fragmentHomepage.setArguments(bundle);
fabGelir = rootView.findViewById(R.id.fabGelir);
toolbarGelir = rootView.findViewById(R.id.toolbarGelir);
rvgelir = rootView.findViewById(R.id.rvgelir);
toolbarGelir.setTitle("Gelir Ekle");
vt = new Veritabani(getActivity());
rvgelir.setHasFixedSize(true);
rvgelir.setLayoutManager(new LinearLayoutManager(getActivity()));
gelirlerArrayList = new GelirlerDatabaseAccessObject().tumGelirler(vt);
adapter = new GelirlerAdapter(getActivity(), gelirlerArrayList, vt);
rvgelir.setAdapter(adapter);
fabGelir.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
alertgoster();
);
for (Gelirler gel : gelirlerArrayList)
toplamgelir = toplamgelir + gel.getGelirTutar();
toolbarGelir.setSubtitle("toplam Gelir: " + toplamgelir);
return rootView;
public void alertgoster()
LayoutInflater layout = LayoutInflater.from(getActivity());
View tasarim = layout.inflate(R.layout.alert_gelir, null);
final EditText editTextGelir = tasarim.findViewById(R.id.editTextGider);
final EditText editTextGelirTutar = tasarim.findViewById(R.id.editTextGiderTutar);
final AlertDialog.Builder gelir = new AlertDialog.Builder(getActivity());
gelir.setTitle("Gelir Ekle");
gelir.setView(tasarim);
gelir.setPositiveButton("Ekle", new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface dialog, int which)
String gelir_ekle = editTextGelir.getText().toString().trim();
Double gelir_tutar = Double.parseDouble(editTextGelirTutar.getText().toString().trim());
new GelirlerDatabaseAccessObject().GelirEkle(vt, gelir_ekle, gelir_tutar);
gelirlerArrayList = new GelirlerDatabaseAccessObject().tumGelirler(vt);
adapter = new GelirlerAdapter(getActivity(), gelirlerArrayList, vt);
rvgelir.setAdapter(adapter);
Toast.makeText(getContext().getApplicationContext(), gelir_ekle + " - " + gelir_tutar + " olarak Eklendi", Toast.LENGTH_SHORT).show();
);
gelir.setNegativeButton("İptal", new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface dialog, int which)
);
gelir.create().show();
这是我的第二个片段,它应该获取数据“Fragmenthomepage”
public class FragmentHomepage extends Fragment
private Toolbar toolbarHome;
private TextView textViewbugunGelir;
private TextView textViewbugunGider;
public String strtext;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
View rootView = inflater.inflate(R.layout.fragment_homepage, container, false);
toolbarHome = rootView.findViewById(R.id.toolbarHome);
toolbarHome.setTitle("Gelir gider hesaplar");
textViewbugunGider = rootView.findViewById(R.id.textViewbugunGider);
strtext = getArguments().getString("anahtar"); //this is not working getting error "null object reference"
textViewbugunGelir = rootView.findViewById(R.id.textViewbugunGelir);
textViewbugunGelir.setText(strtext);
return rootView;
这是我的 MainActivity
public class MainActivity extends AppCompatActivity
private BottomNavigationView bottom_navigation;
private Fragment temporaryFragment;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportFragmentManager().beginTransaction().add(R.id.frame_holder, new FragmentHomepage()).commit();
bottom_navigation = findViewById(R.id.bottom_navigation);
bottom_navigation.setSelectedItemId(R.id.action_home);
bottom_navigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener()
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item)
if (item.getItemId() == R.id.action_home)
temporaryFragment = new FragmentHomepage();
if (item.getItemId() == R.id.action_gelir)
temporaryFragment = new FragmentGelir();
if (item.getItemId() == R.id.action_gider)
temporaryFragment = new FragmentGider();
getSupportFragmentManager().beginTransaction().replace(R.id.frame_holder, temporaryFragment).commit();
return true;
);
【问题讨论】:
你的意思是从fragment gelir发送数据到fragment首页? Framgents 属于同一个活动? 是的片段属于同一活动,即 Mainactivity 这能回答你的问题吗? How to pass values between Fragments 【参考方案1】:1- 您可以使用接口,并使用此处描述的主机活动与其他片段共享数据。
开发者文档:https://developer.android.com/training/basics/fragments/communicating
2- 您可以使用视图模型通过主机活动在片段之间共享数据。这是现在推荐的片段之间数据共享的方式。使用视图模型在片段之间共享数据。
https://developer.android.com/topic/libraries/architecture/viewmodel#sharing
创建一个视图模型类:
// Item is your desired data type
public class SharedViewModel extends ViewModel
private final MutableLiveData<Item> selected = new MutableLiveData<Item>();
public void select(Item item)
selected.setValue(item);
public LiveData<Item> getSelected()
return selected;
在你的 FragmentGelir 中:
public void onViewCreated(@NonNull View view, Bundle savedInstanceState)
super.onViewCreated(view, savedInstanceState);
model = new ViewModelProvider(requireActivity()).get(SharedViewModel.class);
itemSelector.setOnClickListener(item ->
model.select(item);
);
在您的 FragmentHomePage 中:
public void onViewCreated(@NonNull View view, Bundle savedInstanceState)
super.onViewCreated(view, savedInstanceState);
SharedViewModel model = new ViewModelProvider(requireActivity()).get(SharedViewModel.class);
model.getSelected().observe(getViewLifecycleOwner(), item ->
// Update the UI.
);
3- Hosting Activity 的基本用法
在主要活动中:
private DataType data;
public SetData(DataType data)
this.data = data;
public GetData()
return this.data;
在片段中设置数据:
((MainActivity) requireActivity()).SetData(data);
在片段中获取数据:
DataType data;
data = ((MainActivity) requireActivity()).GetData;
【讨论】:
非常感谢您的回答,我正在尝试解决此解决方案,但我不明白什么是“requireActivity()”,您能帮我解决这个问题吗?提前致谢 您可以从片段中调用主机活动。 requireActivity() 是你的 FragmentActivity 你成功了吗? 非常感谢先生。艾伦为你的帮助。我从早上 9 点开始研究这个问题 :)。但它没有再次工作。但我认为我可以将数据从片段传递到 Mainactivity,然后从 Mainactivity 传递到另一个片段。这是很长的路要走,但我还能做什么? 是的,这是另一种传输数据的方式。顺便推荐一下视图建模。如果需要帮助可以联系我【参考方案2】:在Activity中创建静态变量并获取它的简单方法
((MainActivity)getActivity()).MY_VAR
【讨论】:
【参考方案3】:场景是这样的:
在 GelirFragment 中:
class GelirFragment extends Fragment
public void onClick()
Fragment homePageFragment = new HomePageFragment();
Bundle bundle = new Bundle();
bundle.putInt("someKey", value);
fragment.setArguments(bundle);
getFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container, homePageFragment)
.addToBackStack(null)
.commit();
在 HomePageFragment 中:
class HomePageFragment extends Fragment
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
Bundle args = getArguments();
if (args != null)
Integer passedValue = arguments.getInt("someKey");
return view;
【讨论】:
非常感谢您的回复。因为我使用 BottomNavigationView ,所以我没有再次工作。 Mainactivity 上有 3 个片段,当我点击“gelirfragment”时它崩溃了。以上是关于将数据从 Fragment 传递到另一个 Fragment的主要内容,如果未能解决你的问题,请参考以下文章
如何将数据从一个 Fragment 发送到另一个 Fragment?
从 android Activity 传递到 Fragment 的 Parcelable 对象
在 Android 中的两个 Fragment 之间传递数据都有哪些不同的方式? [复制]
使用 Serializable - AndroidX 将数据从 RecyclerView 传递到 RecyclerViewMore