gpt4 book ai didi

android - 如何在android中使用bundle将 Activity 之间的数据传递给 fragment

转载 作者:行者123 更新时间:2023-11-29 18:10:36 28 4
gpt4 key购买 nike

我可以将值存储在一个变量中。现在我想将该变量传递给一个 fragment 使用下面的代码,我可以加载 fragment :

public class AndroidListFragmentActivity extends Activity {
Fragment2 f2;
public static String itemname;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.apetiserfragement);
itemname=getIntent().getStringExtra("itemname");
Bundle args=new Bundle();
args.putString("itemname", itemname);
f2=new Fragment2();
f2.setArguments(args);
}
} /* (Here I load fragment using xml page) itemname */

输出被分成 2 个窗口,一个用于扩展 listfragment(用于 ListView ),一个用于 fragment 。

fragment 2.xml

public class Fragment2 extends Fragment {
String itemname;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
System.out.println(getArguments().getString("itemname"));

return inflater.inflate(R.layout.fragment2, container, false);
}

@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
}

此类 itemname 中的 AndroidListFragmentActivity 我想传递 Fragment2.class.. 请帮助我

最佳答案

如果两个 fragment 都在进行其他 Activity ,则可以使用 intent

如果在同一 Activity 上,则可以对该特定 Activity 进行操作

如本link

查看类 TitlesFragment 的 onListItemClick

**
* Helper function to show the details of a selected item, either by
* displaying a fragment in-place in the current UI, or starting a
* whole new activity in which it is displayed.
*/
void showDetails(int index) {
mCurCheckPosition = index;

if (mDualPane) {//<---------------------f on same activity then can do operation on that particular fragment
// We can display everything in-place with fragments, so update
// the list to highlight the selected item and show the data.
getListView().setItemChecked(index, true);

// Check what fragment is currently shown, replace if needed.
DetailsFragment details = (DetailsFragment) //<------------------------see use getFragmentManager
getFragmentManager().findFragmentById(R.id.details);
if (details == null || details.getShownIndex() != index) {
// Make new fragment to show this selection.
details = DetailsFragment.newInstance(index);

// Execute a transaction, replacing any existing fragment
// with this one inside the frame.
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.details, details);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
}

} else { //<----------If both fragment are on other activity then can use intent
// Otherwise we need to launch a new activity to display
// the dialog fragment with selected text.
Intent intent = new Intent();
intent.setClass(getActivity(), DetailsActivity.class);
intent.putExtra("index", index);
startActivity(intent);
}

关于android - 如何在android中使用bundle将 Activity 之间的数据传递给 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10960828/

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