首页 > 解决方案 > 微调器未在设备上显示,但在模拟器上

问题描述

我在我的 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);
}

标签: androidspinneremulationdevicemissing-data

解决方案


推荐阅读