gpt4 book ai didi

java - 从 Activity 中将文本设置为 Fragment

转载 作者:行者123 更新时间:2023-12-02 04:40:12 26 4
gpt4 key购买 nike

我徒劳地尝试简单地从 FormActivity 中获取值并使用它来填充 Fragment 中的 EditText。我必须等待 Activity 从远程服务器检索值并用它填充本地 SQLite 表,这意味着我在 fragment 中尝试过的解决方案都不起作用(空错误,因为它还没有那么早填充)。我需要帮助。

FormActivity.java的相关部分

public void DrawText() {
// Fetching user details from sqlite
HashMap<String, String> user = db.getUserDetails();

if (user.size() != 0) {
Log.e(TAG, "string surname: " + surname);

// Displaying the user details on the fragment

FirstFragment f1 = new FirstFragment();

//put info into a bundle to pass to fragments
Bundle drawbundle = new Bundle();
drawbundle.putString("surname", surname);

f1.setArguments(drawbundle);

FragmentManager FM = getSupportFragmentManager();
FragmentTransaction FT = FM.beginTransaction();
FT.add(R.id.something, f1);
FT.commit();

}else{
Log.e(TAG, "table was empty");
}
}

private class MyPagerAdapter extends FragmentPagerAdapter {

public MyPagerAdapter(FragmentManager fm) {

super(fm);
}

@Override
public Fragment getItem(int pos) {
switch(pos) {

case 0: return FirstFragment.newInstance("@string/form_instruct");
case 1: return SecondFragment.newInstance("SecondFragment, Instance 1");
case 2: return ThirdFragment.newInstance("ThirdFragment, Instance 1");
case 3: return FourthFragment.newInstance("FourthFragment, Instance 1");
//case 4: return FifthFragment.newInstance("ThirdFragment, Instance 3");
default: return FirstFragment.newInstance("FirstFragment, Default");

}
}

显然,id.something不正确,我不知道如何获取id。我读到我可以使用标签,但我的尝试没有成功。我尝试了以下方法:

FirstFragment fragA = (FirstFragment) getSupportFragmentManager().findFragmentByTag("fragA");

这是我的 FirstFragment.java

public class FirstFragment extends android.support.v4.app.Fragment {

//create variables & Logcat tag
private static final String TAG = FirstFragment.class.getSimpleName();
private EditText inputTitle;
private EditText inputName;
private EditText inputSurname;
//private SQLiteHandler db;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_first, container, false);
TextView tv = (TextView) v.findViewById(R.id.tvFragFirst);
tv.setText(getArguments().getString("msg"));

inputSurname = (EditText) getActivity().findViewById(R.id.surnameText);
inputSurname.setText(getArguments().getString("surname"));

return v;
}

public static FirstFragment newInstance(String text) {

FirstFragment f = new FirstFragment();
Bundle b = new Bundle();
b.putString("msg", text);

f.setArguments(b);

return f;
}

我正在绕这个圈子,有人可以帮忙吗?

最佳答案

如果您使用 ContentProvider,您可以使用 ContentObserver 在表格填满以及调用 onChange 时收到通知,您可以更新 TextView。如果您没有使用 ContentProvider,则可以使用 LocalBroadcastMangerBroadcastReceiver 来实现相同的效果

关于java - 从 Activity 中将文本设置为 Fragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30274960/

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