从列表视图中传递数据到片段
当我点击列表视图时,包可以获取值,但不能将值传递给第二个片段。 第一块碎片从列表视图中传递数据到片段
categorylist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
final String category = ((TextView) view.findViewById(R.id.mainproductitem)).getText().toString();
details_fragment ldf = new details_fragment();
Bundle args = new Bundle();
args.putString("category", category);
ldf.setArguments(args);
}
});
return view;
}
第二块碎片
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
view=inflater.inflate(R.layout.my_fragment2, container, false);
test = (TextView)view.findViewById(R.id.textView);
Bundle args = getArguments();
if (args != null && args.containsKey("category")){
String userId = args.getString("category");
test.setText(userId);
}
return view;
}
HomeActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.activity.HomeActivity"
tools:showIn="@layout/app_bar_home">
//View that will hold all the fragments
<FrameLayout
android:id="@+id/container_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
FirstFragment
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment1, container, false);
view.setBackgroundColor(Color.WHITE);
listViewAdapter.SetOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
SecondFragment secondFrag = new SecondFragment();
Bundle args = new Bundle();
args.putString("Key","some value");
secondFrag .setArguments(args);
getFragmentManager()
.beginTransaction()
.replace(R.id.container_view, fragment)
.addToBackStack(null)
.commit();
}
});
return view;
}
SecondFragment
我尝试了这一点,它完美的作品。
你可能会忘记启动第二个片段
categorylist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
final String category = ((TextView) view.findViewById(R.id.mainproductitem)).getText().toString();
details_fragment ldf = new details_fragment();
Bundle args = new Bundle();
args.putString("category", category);
ldf.setArguments(args);
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.yourContainer, ldf);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
}
和你第二个活动
Bundle agrs = getArguments();
if (args != null) {
String category = agrs.getString("category");
test.setText(category);
}
sry,这里有措辞错误,实际上我不能得到我的第二个片段的值 –
我编辑了我的答案,请尝试让我知道 –
您确定您正在获取第一个片段的类别字符串中的值吗? –
试试这个:在您接收片段:
Bundle agrs = this.getArguments();
if (args != null) {
String myStr = args.getString(key, defaultValue);
}
sry,它对我来说很有用 –
`Use this code in your categorylist.setOnItemClickListener`
// Create fragment and give it an argument specifying the article it should show
details_fragment newFragment = new details_fragment();
Bundle args = new Bundle();
args.putString("category", category)
//Note: if your are in fragment than replace getSupportFragmentManager to getFragmentManager.
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack so the user can navigate back
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
使用组合。下面是一个例子:
Fragment fragment = new Fragment();
Bundle bundle = new Bundle();
bundle.putString(key, value);
fragment.setArguments(bundle);
getFragmentManager().beginTransaction().replace(R.id.container_view, bundle).commit();
Bundle已经为很多数据类型提供了方法。见this在片段
然后,检索的数据(例如,在onCreate()方法)有:
Bundle bundle = this.getArguments();
if (bundle != null) {
int myInt = bundle.getString(key, defaultValue);
}
在第一块碎片
categorylist.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
final String category = ((TextView) view.findViewById(R.id.mainproductitem)).getText().toString();
MainActivity main=new MainActivity();
main.openSecondFrag();
}
});
return view;
}
在您的MainActivity,把openSecondFrag()方法
public void openSecondFrag(){
details_fragment ldf = new details_fragment();
Bundle args = new Bundle();
args.putString("category", category);
ldf.setArguments(args);
}
在第二块碎片
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
view=inflater.inflate(R.layout.my_fragment2, container, false);
test = (TextView)view.findViewById(R.id.textView);
Bundle args = getArguments();
if (args != null && args.containsKey("category")){
String userId = args.getString("category");
test.setText(userId);
}
return view;
}
我相信它会起作用。
sry,应该得到第二个片段的值** –
我认为你没有在片段事务中开始你的第二个片段。 –