gpt4 book ai didi

java - setAdapter和setDropDownViewResource是IDE用红色写的

转载 作者:行者123 更新时间:2023-12-01 21:24:19 26 4
gpt4 key购买 nike

IDE 说我在 setAdaptersetDropDownViewResource 中“无法解析符号”,我不知道为什么:

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

先谢谢大家了!

我发布我的代码:

public class MyListFragment extends Fragment implements
android.widget.CompoundButton.OnCheckedChangeListener {

ListView lv;
ArrayList<Planet> planetList;
static PlanetAdapter plAdapter;
private TextView txtName;
private TextView txtEmail;
private Button btnLogout;
private Spinner spinner1;
private Spinner spinner2;

private SQLiteHandler db;
private SessionManager session;
BirraAdapter biAdapter;
PlanetAdapter.PlanetHolder holder;
private static Context context = null;
private static FragmentActivity mInstance;

Integer[] imageId = {
R.mipmap.androtuto,
R.mipmap.ic_launcher,
R.mipmap.ic_launcher,
R.mipmap.ic_launcher,
R.mipmap.ok,
/*R.drawable.image6,
R.drawable.image7*/

};
String[] countryNames={"India","China","Australia","Portugle","America","New Zealand"};

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the custom_spinner_items for this fragment
//super.onDestroy();

SharedPreferences settings = getContext().getSharedPreferences("states", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.clear();
editor.commit();
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_list2, container, false);
context = getActivity();
mInstance = getActivity();
txtName = (TextView) rootView.findViewById(R.id.name);
txtEmail = (TextView) rootView.findViewById(R.id.numero_telefonico);
btnLogout = (Button) rootView.findViewById(R.id.btnLogout);

// SqLite database handler
db = new SQLiteHandler(context.getApplicationContext());


// session manager
session = new SessionManager(context.getApplicationContext());
/*

if (!session.isLoggedIn()) {
logoutUser();
}
*/

// Fetching user details from SQLite
HashMap<String, String> user = db.getUserDetails();

String name = user.get("name");
//String email = user.get("email");
// String email = user.get("email");
String email = user.get("numero_telefonico");


// Displaying the user details on the screen
System.out.println(name + email);

txtName.setText(name);
txtEmail.setText(email);
/* Button mButton = (Button) rootView.findViewById(R.id.button);
mButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showResult(v);


}
});*/
//return inflater.inflate(R.custom_spinner_items.fragment_list2, container, false);
btnLogout.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
logoutUser();
}
});

/* List<String> spinnerArray = new ArrayList<String>();
spinnerArray.add("item1");
spinnerArray.add("item2");

ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this.getContext(), android.R.layout.simple_spinner_item, spinnerArray);

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner sItems = (Spinner) rootView.findViewById(R.id.simpleSpinner);
sItems.setAdapter(adapter);*/
return rootView;
}
Spinner spinner = (Spinner) getView().findViewById(R.id.simpleSpinner);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this.getContext(),
R.array.country_arrays, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
//adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);

最佳答案

这是因为这些行应该位于某个方法内。你应该这样写:

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Spinner spinner = (Spinner) getView().findViewById(R.id.simpleSpinner);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this.getContext(),
R.array.country_arrays, android.R.layout.simple_spinner_item);

spinner.setAdapter(adapter);

}

关于java - setAdapter和setDropDownViewResource是IDE用红色写的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38546109/

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