我有2个Spinner(下拉列表)和一个片段内的Button
spinner1
适用于Subject(前SpinnerItem:数学,科学,英语)
spinner2
适用于季度(前SpinnerItem:第1季度,第2季度,第3季度,第4季度)
当用户从spinner1
和spinner2
中选择值并且他们点击按钮时,他们应该移动到一个新的片段,其中包含一个名称列表取决于他们从旋转器中选择的内容
onCreateView
public Fragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_layout, container,
false);
// SPINNER1
Spinner spinner1 = (Spinner) view.findViewById(R.id.spinSubject);
Spinner spinner2 = (Spinner) view.findViewById(R.id.spinQuarter);
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
//what to do
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
//what to do
}
});
// BUTTON
Button btnSearch = (Button) view.findViewById(R.id.btnSearch);
btnSearch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnSearch:
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.content_frame, new SubjectandQuarter());
ft.commit();
break;
}
}
});
return view;
}
XML TextView
<TextView
android:id="@+id/tvSubject"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/subject_here"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/tvQuarter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/quarter_here"
android:textAppearance="?android:attr/textAppearanceLarge" />
SubjectandQuarter类
public class SubjectandQuarterextends Fragment {
public SubjectandQuarter() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.subjectandquarter, container,
false);
TextView tvSubject = (TextView) view.findViewById(R.id.tvSubject);
TextView tvQuarter = (TextView) view.findViewById(R.id.tvQuarter);
return view;
}
}
我不知道我是否做得对。希望有人可以帮我解决我的问题(android中的新功能)
输出应该像(ListView):
答案 0 :(得分:2)
试试这个..
在代码中使用spinner1.getSelectedItem().toString().trim();
btnSearch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnSearch:
String spi1 = spinner1.getSelectedItem().toString().trim();
String spi2 = spinner2.getSelectedItem().toString().trim();
Toast.makeText(getActivity(), "Spinner1 = "+spi1+" Spinner2 = "+spi2, Toast.LENGTH_LONG).show();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
SubjectandQuarter fragment = new SubjectandQuarter();
Bundle bundle = new Bundle();
bundle.putString("spinner1", spi1);
bundle.putString("spinner2", spi2);
fragment.setArguments(bundle);
ft.replace(R.id.content_frame, fragment);
ft.commit();
break;
}
}
});
和
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.subjectandquarter, container,
false);
String spi1,spi2;
Bundle args = getArguments();
if (args != null && args.containsKey("spinner1"))
spi1 = args.getString("spinner1");
if (args != null && args.containsKey("spinner2"))
spi2 = args.getString("spinner2");
TextView tvSubject = (TextView) view.findViewById(R.id.tvSubject);
TextView tvQuarter = (TextView) view.findViewById(R.id.tvQuarter);
tvSubject.setText(spi1);
tvQuarter.setText(spi2);
return view;
}
答案 1 :(得分:1)
简单地使用它来设置
final TextView tvSubject= (TextView) findViewById(R.id.tvSubject);
final TextView tvQuarter= (TextView) findViewById(R.id.tvQuarter);
//然后在微调器函数
中调用这些textviewtvSubject.setText(spi1);
tvQuarter.setText(spi2);