在我的房间应用程序中,我从工作良好的new_pb字段开始。我现在正在尝试添加pb_value和pb_details,但是这些字段未在模拟器上显示。另外,当我尝试输入新的pb时,什么也没有出现。它仅显示已在数据库中创建的数据库。已添加迁移并更新了版本号。
MainActivity
class MainActivity : AppCompatActivity() {
private val newPbActivityRequestCode = 1
private lateinit var pbViewModel: PbViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val toolbar = findViewById<Toolbar>(R.id.toolbar)
setSupportActionBar(toolbar)
val recyclerView = findViewById<RecyclerView>(R.id.recyclerview)
val adapter = PbListAdapter(this)
recyclerView.adapter = adapter
recyclerView.layoutManager = LinearLayoutManager(this)
pbViewModel = ViewModelProviders.of(this).get(PbViewModel::class.java)
pbViewModel.allPbs.observe(this, Observer { pbs -> pbs?.let { adapter.setPbs(it)}})
val fab = findViewById<FloatingActionButton>(R.id.fab)
fab.setOnClickListener {
val intent = Intent(this@MainActivity, NewPbActivity::class.java)
startActivityForResult(intent, newPbActivityRequestCode)
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, intentData: Intent?) {
super.onActivityResult(requestCode, resultCode, intentData)
if (requestCode == newPbActivityRequestCode && resultCode == Activity.RESULT_OK) {
intentData?.let { data ->
val new_pb = NewPb(data.getIntExtra(
NewPbActivity.EXTRA_REPLY, 0),"", 0.0, "")
pbViewModel.insert(new_pb)
}
} else {
Toast.makeText(
applicationContext,
R.string.empty_not_saved,
Toast.LENGTH_LONG
).show()
NewPbActivity
class NewPbActivity : AppCompatActivity() {
private lateinit var editNewPb: EditText
public override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_new_pb)
editNewPb = findViewById(R.id.edit_new_pb)
val button = findViewById<Button>(R.id.button_save)
button.setOnClickListener {
val replyIntent = Intent()
if (TextUtils.isEmpty(editNewPb.text)) {
setResult(Activity.RESULT_CANCELED, replyIntent)
} else {
val new_pb = editNewPb.text.toString()
replyIntent.putExtra(EXTRA_REPLY, new_pb)
setResult(Activity.RESULT_OK, replyIntent)
}
finish()
}
}
companion object {
const val EXTRA_REPLY = "com.example.android.NewPblistsql.REPLY"
}
}
答案 0 :(得分:0)
如果您的数据库已经使用table创建,那么要将新字段添加到现有表中,您需要在会议室迁移块中触发等效于onUpgrade()的alter query。 如果需要的话,请转至Android开发者网站进行房间迁移。
答案 1 :(得分:0)
更改数据库版本,以便您调用并执行类似的操作
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TBL_GPS_DATA);
this.onCreate(db);//OP1
//OR OP2 alter table
try {
if (newVersion > oldVersion) {
// db.execSQL("ALTER TABLE " + TBL_GPS_DATA + " ADD COLUMN " + COUNTRY + " TEXT DEFAULT 'INDIA'");
// db.execSQL("ALTER TABLE " + TBL_GPS_DATA + " ADD COLUMN " + COUNTRY_FLAG + " TEXT DEFAULT '0' ");
db.execSQL("ALTER TABLE " + TBL_GPS_DATA + " ADD COLUMN " + COUNTRY + " TEXT ");
db.execSQL("ALTER TABLE " + TBL_GPS_DATA + " ADD COLUMN " + COUNTRY_FLAG + " TEXT ");
}
} catch (Exception e) {
System.out.println("this is error due to onupgrade v ");
}
}