我有一个包含微调器的片段。微调器将显示“ 90天”,“ 180天”和“ 365天”。我的问题是如何将微调器选择的项目传递到片段中。
我的片段代码部分是
void PrevReqList(String enquiryStatusCode, final View rootView){
final ListView enquiriesList=(ListView)rootView.findViewById(R.id.req_list);
lists.add(enquiriesList);
ArrayList<String> statisticsDuration = new ArrayList<String>();
statisticsDuration.add(getResources().getString(R.string.days90));
statisticsDuration.add(getResources().getString(R.string.days180));
statisticsDuration.add(getResources().getString(R.string.days365));
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this.getActivity(), R.layout.item_spinner_small_white, statisticsDuration);
dataAdapter.setDropDownViewResource(R.layout.item_spinner);
listDurationSpinner =(Spinner)rootView.findViewById(R.id.list_duration);
listDurationSpinner.setAdapter(dataAdapter);
listDurationSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String item = parent.getItemAtPosition(position).toString();
**How to pass from here to below?**
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
SharedPreferences preferences = this.getActivity().getSharedPreferences(Common.preferenceName, Context.MODE_PRIVATE);
String userName=preferences.getString(Common.userName,"");
final TextView noItems=(TextView)rootView.findViewById(R.id.no_items);
noItems.setVisibility(View.GONE);
final String reqtype= this.getActivity().getIntent().getExtras().getString(Common.REQUISITIONTYPE);
enquiriesList.setVisibility(View.GONE);
int duration=0;
if(listDurationSpinner.getSelectedItem().toString().equals(getResources().getString(R.string.days90))){
duration = 90;
}
else if(listDurationSpinner.getSelectedItem().toString().equals(getResources().getString(R.string.days180))){
duration=180;
}
else if(listDurationSpinner.getSelectedItem().toString().equals(getResources().getString(R.string.days365))){
duration=365;
}
// My remaining code
}
如果我有任何错误请原谅。我是新手。
答案 0 :(得分:1)
您可以像这样创建片段中的数据成员:
String spinnerSelectedItem;
然后,您可以像这样在onItemSelected()
中初始化该成员:
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
spinnerSelectedItem = statisticsDuration.get(position);
}
此后,spinnerSelectedItem
将包含用户选择的值。
答案 1 :(得分:1)
尝试此代码。 制作一种如下所示的方法...
private void getData(String value){
SharedPreferences preferences = this.getActivity().getSharedPreferences(Common.preferenceName, Context.MODE_PRIVATE);
String userName=preferences.getString(Common.userName,"");
final TextView noItems=(TextView)rootView.findViewById(R.id.no_items);
noItems.setVisibility(View.GONE);
final String reqtype= this.getActivity().getIntent().getExtras().getString(Common.REQUISITIONTYPE);
enquiriesList.setVisibility(View.GONE);
int duration=0;
if(value.equals(getResources().getString(R.string.days90))){
duration = 90;
}
else if(value.equals(getResources().getString(R.string.days180))){
duration=180;
}
else if(value.equals(getResources().getString(R.string.days365))){
duration=365;
}
}
之后,选择微调器视图的项目。
listDurationSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
getData(parent.getSelectedItem().toString());// check this
//or
getData(spiner.getSelectedItem().toString());
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});