IDE在setAdapter
和setDropDownViewResource
中说“我无法解析符号”,我不知道为什么:
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);
答案 0 :(得分:2)
这是因为那些行应该在某个方法中。你应该把它写成:
@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);
}