gpt4 book ai didi

android - Fragment 中微调器的 setOnItemSelectedListener

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:27:43 30 4
gpt4 key购买 nike

我在 setOnItemSelectedListener 中有这个错误:

The method setOnItemSelectedListener(AdapterView.OnItemSelectedListener) in the type AdapterView is not applicable for the arguments (FragmentMain)"

fragment 类:

public class FragmentMain extends Fragment  {

private Spinner countriesSpinner;
private Activity rootView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState){
View rootView =inflater.inflate(R.layout.activity_main, container, false);
return rootView;
}
@Override
public void onStart() {
super.onStart();

addItemsOnSpinner();
}
public void addItemsOnSpinner() {

countriesSpinner = (Spinner) rootView.findViewById(R.id.team_list_spinner);
countriesSpinner.setOnItemSelectedListener(new CustomOnItemSelectedListener ()) ;

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(),
R.array.team_list, android.R.layout.simple_spinner_item);

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
countriesSpinner.setAdapter(adapter);

countriesSpinner.setOnItemSelectedListener(this);

}

public class CustomOnItemSelectedListener extends Activity implements
OnItemSelectedListener {

@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos,
long id) {
// TODO Auto-generated method stub
if (parent.getItemAtPosition(pos).toString()
.equals("San Antonio Spurs")) {
Intent i = new Intent(getApplicationContext(), Spurs_games.class);
startActivity(i);
finish();}
if (parent.getItemAtPosition(pos).toString()
.equals("Los Angeles Lakers")) {
Intent i = new Intent(getApplicationContext(), Lakers_games.class);
startActivity(i);
finish();}
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub

}

}

public void onItemSelected(AdapterView<?> parent, View view, int pos,
long id) {
if (parent.getItemAtPosition(pos).toString()
.equals("San Antonio Spurs")) {
Intent i = new Intent(getActivity(), Spurs_games.class);
startActivity(i);
finish();}
if (parent.getItemAtPosition(pos).toString()
.equals("Los Angeles Lakers")) {
Intent i = new Intent(getActivity(), Lakers_games.class);
startActivity(i);
finish();}
}

private void finish() {
// TODO Auto-generated method stub

}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub

}
}

这是我在这里的第一篇文章,所以请原谅/纠正我。对不起我的英语..

最佳答案

setOnItemSelectedListener

如果您想在 Fragment 内的微调器上使用监听器,则必须在您的 FragmentMain 中实现,而不是在您的 Activity 中实现

在你的 FragmentMain onCreateView 中

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

输入这段代码

countriesSpinner = (Spinner) rootView.findViewById(R.id.team_list_spinner);
countriesSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
}

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

}
});

关于android - Fragment 中微调器的 setOnItemSelectedListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23449270/

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