gpt4 book ai didi

android - 如何修复 Spinner 在 fragment 中调用 OnItemSelected 后未正确显示所选项目?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:10:30 30 4
gpt4 key购买 nike

我正在创建一个 SportsApp,在我的 NFL 页面中,用户可以在其中选择他们想要查看的周,它不会显示用户选择的周,但会显示正确的数据。微调器显示默认的第 1 周文本,但加载所选周的正确值。

 public class NFLWeekSpinner extends Fragment {

Spinner NFLspinner;
ArrayAdapter<CharSequence> spinnerAdapter;
String inputWeekFormatted;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.nfl_spinner_frag,container,false);
NFLspinner = view.findViewById(R.id.NFLSpinner);

spinnerAdapter = ArrayAdapter.createFromResource(this.getActivity(),R.array.weekNum,
android.R.layout.simple_spinner_item);
//spinnerAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
NFLspinner.setAdapter(spinnerAdapter);

NFLspinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

boolean user = false;

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

if(user) {
String selectedWeek = (String) parent.getItemAtPosition(position);
if (selectedWeek.length() > 6) {
inputWeekFormatted = selectedWeek.substring(5, 7);
} else {
inputWeekFormatted = selectedWeek.substring(5, 6);

}
((MainActivity) getActivity()).loadNFLscoreboard(inputWeekFormatted);
}
user = true;

// count++;
}

@Override
public void onNothingSelected(AdapterView<?> parent) {

}

});



return view;

}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

}

}

MainActivity.java

public class MainActivity extends AppCompatActivity {

private static final String BACK_ROOT_STACK = "root_fragment";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

DateFormat sdf = new SimpleDateFormat("yyyyMMdd");

//loadMLBScoreboard(defaultDate);
loadNavBar();
}
public void loadNavBar() {

final BottomNavigationView mBottomNav = findViewById(R.id.bottom_navigation);
final String defaultDate = "20180915";
mBottomNav.setItemIconTintList(null);
mBottomNav.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
mBottomNav.setItemIconTintList(null);

switch (item.getItemId()) {
case R.id.menu_nfl:
loadNFLscoreboard("1");
break;
case R.id.menu_baseball:
DateFormat sdf = new SimpleDateFormat("yyyyMMdd");


String today = sdf.format(Calendar.getInstance().getTime());
loadMLBScoreboard(defaultDate);
break;

default:

loadMLBScoreboard(defaultDate);
}
return true;
}
});
}

public void loadMLBScoreboard(String inputDate) {

LoadScoreboardFragment loadScoreboardFragment = new LoadScoreboardFragment();

loadMLBFragment(inputDate, loadScoreboardFragment);
}

private void loadMLBFragment(String inputDate, LoadScoreboardFragment loadScoreboardFragment) {
Bundle bundle = new Bundle();

//Load date Fragment
DateFragment dateFragment = new DateFragment();

bundle.putString("inputDateKey", inputDate);
loadScoreboardFragment.setArguments(bundle);

FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction newFT = fragmentManager.beginTransaction();
newFT.add(loadScoreboardFragment, "Loading");
newFT.replace(R.id.holderFragment, dateFragment)
.addToBackStack(BACK_ROOT_STACK)
.commit();
}
public void loadNFLscoreboard(String nflWeek) {
LoadNFLscoreboardFragment loadNFLscoreboardFragment = new LoadNFLscoreboardFragment();
NFLWeekSpinner weekSpinner = new NFLWeekSpinner();

Bundle bundle = new Bundle();

bundle.putString("inputWeekKey",nflWeek);
loadNFLscoreboardFragment.setArguments(bundle);

FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.main_recycle, loadNFLscoreboardFragment)
.commit();

fragmentManager.beginTransaction()
.replace(R.id.holderFragment,weekSpinner)
.addToBackStack(BACK_ROOT_STACK)
.commit();
}
}

R.array.WeekNum

 <string-array name="weekNum">
<item>Week 1</item>
<item>Week 2</item>
<item>Week 3</item>
<item>Week 4</item>
<item>Week 5</item>
<item>Week 6</item>
<item>Week 7</item>
<item>Week 8</item>
<item>Week 9</item>
<item>Week 10</item>
<item>Week 11</item>
<item>Week 12</item>
<item>Week 13</item>
<item>Week 14</item>
<item>Week 15</item>
<item>Week 16</item>
<item>Week 17</item>
</string-array>

最佳答案

我终于明白了!!!在 NFLSpinner.OnItemSelected 调用中,一旦用户选择了新的一周,我就会调用 MainActivity loadNFLScoreboard 方法,该方法会在每个周上创建 Spinner 的新实例称呼。我将 NFLWeekSpinner 声明移到了 loadNFLScoreboad 方法之外,因为每次调用都没有创建新的微调器,所以所选的星期会正确显示。

感谢您的帮助@lionscribe

关于android - 如何修复 Spinner 在 fragment 中调用 OnItemSelected 后未正确显示所选项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54337359/

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