我在Android应用程序中的初始“家”活动有一个从数据库填充的微调器。在模拟器上,这绝对可以正常工作。当我将手机连接到计算机并运行该应用程序时,它也可以正常工作。但是,当它在我的手机上独立运行(即未连接到Android Studio)时,微调器在我第一次打开该应用程序时不会显示。但是,如果我退出该活动并返回该活动,或者甚至通过更改屏幕方向来重新启动该活动,则微调器会出现并且一直存在,直到关闭该应用程序为止。我试过几部手机,结果相同。我已经将模拟器与手机的API和解析度进行了匹配,但是无法复制该错误。其他人有没有经历过?
public class StartPage extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
String testusername;
String mUser;
int mPosition;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start_page);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle(R.string.app_name);
toolbar.setTitleTextColor(getResources().getColor(R.color.textColor));
setSupportActionBar(toolbar);
// Get the Intent that started this activity
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
//get reference to database helper
SQLiteOpenHelper deadly60DatabaseHelper = new Deadly60DatabaseHelper(this);
//get reference to database (read access)
SQLiteDatabase db = deadly60DatabaseHelper.getReadableDatabase();
//return all records in username table
Cursor cursor = db.query("USERLIST", new String[] {"_id","USERNAME"}, null, null, null, null, null);
//String temp = DatabaseUtils.dumpCursorToString(cursor);
//put contents of cursor into an array list
ArrayList<String> mArrayList = new ArrayList<String>();
cursor.moveToFirst();
while(!cursor.isAfterLast()) {
mArrayList.add(cursor.getString(cursor.getColumnIndex("USERNAME"))); //add the item
cursor.moveToNext();
}
//find position of user just added or played in cursor so that can default to this in drop down spinner
if(bundle != null){
String user = bundle.getString("USERNAME");
mUser = user;
mPosition = mArrayList.indexOf(mUser);
}
else{
mPosition=0;
mUser = mArrayList.get(0);
}
Spinner spinner = (Spinner) findViewById(R.id.usernamespinner);
spinner.setVisibility(View.VISIBLE);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(
this, // context
R.layout.my_spinner, // layout file
cursor, // DB cursor
new String[]{"USERNAME"}, // data to bind to the UI
new int[]{android.R.id.text1}, // views that'll represent the data from `fromColumns`
0
);
adapter.setDropDownViewResource(R.layout.my_spinner_dropdown);
//adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setSelected(true);
spinner.setSelection(mPosition); //set the selected user to the user just added or played
spinner.setOnItemSelectedListener(this);
}