Android 基于选定微调器显示ListView
在我的第一个片段中,我有两个微调器和一个按钮Android 基于选定微调器显示ListView,android,fragment,android-spinner,Android,Fragment,Android Spinner,在我的第一个片段中,我有两个微调器和一个按钮 spinner1(Type of Foods) spinner2(Color) 我想知道的是,如果用户在第一个旋转器中选择一种食物类型以及什么颜色,然后单击按钮,他们应该被引导到FragmentTwo,而listview则取决于所选的值 例如:用户在spinner1中选择蔬菜,并在单击按钮后选择绿色spinner2,他们将进入FragmentTwo,并显示绿色蔬菜的列表视图 FragmentOne() FragmentTwo-XML <Rel
spinner1(Type of Foods)
spinner2(Color)
我想知道的是,如果用户在第一个旋转器中选择一种食物类型以及什么颜色,然后单击按钮,他们应该被引导到FragmentTwo
,而listview则取决于所选的值
例如:用户在spinner1
中选择蔬菜,并在单击按钮后选择绿色spinner2
,他们将进入FragmentTwo
,并显示绿色蔬菜的列表视图
FragmentOne()
FragmentTwo-XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tvType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Type of Food"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/tvColor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="@string/Color"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true" >
</ListView>
试试这个
在您的FragmentOne.class中
Button btnSearch = (Button) view.findViewById(R.id.btnSearch);
btnSearch.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
String type = spinner1.getSelectedItem().toString().trim();
String color = spinner2.getSelectedItem().toString().trim();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
FragmentTwo fragment = new FragmentTwo();
Bundle bundle = new Bundle();
bundle.putString("type", type);
bundle.putString("color", color);
fragment.setArguments(bundle);
ft.replace(R.id.content_frame, fragment);
ft.commit();
}
});
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.foodlist, container,
false);
//TextView
tv1 = (TextView) view.findViewById(R.id.tvType);
tv2 = (TextView) view.findViewById(R.id.tvColor);
String type = "",color = "";
Bundle args = getArguments();
if (args != null && args.containsKey("type"))
type = args.getString("type");
if (args != null && args.containsKey("color"))
color = args.getString("color");
tv1.setText(type);
tv2.setText(color);
//ListView
listView = (ListView) view.findViewById(R.id.listView1);
return view;
}
和FragmentTwo.class
Button btnSearch = (Button) view.findViewById(R.id.btnSearch);
btnSearch.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
String type = spinner1.getSelectedItem().toString().trim();
String color = spinner2.getSelectedItem().toString().trim();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
FragmentTwo fragment = new FragmentTwo();
Bundle bundle = new Bundle();
bundle.putString("type", type);
bundle.putString("color", color);
fragment.setArguments(bundle);
ft.replace(R.id.content_frame, fragment);
ft.commit();
}
});
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.foodlist, container,
false);
//TextView
tv1 = (TextView) view.findViewById(R.id.tvType);
tv2 = (TextView) view.findViewById(R.id.tvColor);
String type = "",color = "";
Bundle args = getArguments();
if (args != null && args.containsKey("type"))
type = args.getString("type");
if (args != null && args.containsKey("color"))
color = args.getString("color");
tv1.setText(type);
tv2.setText(color);
//ListView
listView = (ListView) view.findViewById(R.id.listView1);
return view;
}
你试过什么?您是否尝试过
spinner1.getSelectedItemPosition()代码>?是的,我不能让它工作你说“我不能让它工作”是什么意思?有错误吗?错误结果?无错误,但应用程序保留crashing@KenHollis试试我的ans,它在工作,但我还是有问题,在我的示例中,如果用户在spinner1
中选择蔬菜,并在spinner2
中选择绿色,将显示颜色为的蔬菜列表视图green@KenHollis比较FragmentTwo.class
中的类型和颜色,并在ListView
中填充它们。你能举个例子吗?@KenHollis你要去哪里填充列表视图
。发布代码。我仍然不知道该怎么做抱歉第一次这么做你能给我一个我需要做的例子吗?真的很抱歉