通过单击按钮将数据保存在Realm数据库中

时间:2018-07-26 14:57:51

标签: android realm

我想通过单击按钮将数据保存到Realm数据库中
我尝试过,但是没用

课程是:

final     EditText name = (EditText) findViewById(R.id.nameET);

final EditText in_stock = (EditText) findViewById(R.id.avibleET);

final     EditText price = (EditText) findViewById(R.id.priceET);

final         EditText size = (EditText) findViewById(R.id.sizeET);

final EditText color = (EditText) findViewById(R.id.colorET);

   Button buadd=(Button)findViewById(R.id.button4);

 buadd.setOnClickListener(new View.OnClickListener() {

        @Override

        public void onClick(View view) {

            stuff s=new stuff();

            s.setName(name.getText().toString());

            s.setPrice(price.getText().toString());

            s.setIn_stock(in_stock.getText().toString());

            s.setColor(color.getText().toString());

            s.setSize(size.getText().toString());

        //working with database

        Realm.init(getApplicationContext());

        Realm realm=Realm.getDefaultInstance();

        realm.beginTransaction();

        realm.copyToRealmOrUpdate(s);

        realm.commitTransaction();

  Toast.makeText(context.class,"the stuff has been added",Toast.LENGTH_LONG).show;

但是当我运行它并按下按钮时,它显示:不幸的是,该应用程序已停止

1 个答案:

答案 0 :(得分:0)

将您的时间转换为Unix时间并保存到Realm。

或将时间解析为对象

data class Date(val year, val day, val hours, val minutes, val seconds)