为什么我的sqlite插入方法不起作用

时间:2018-11-19 05:54:37

标签: java android android-studio sqliteopenhelper

使用SQLite保存数据

大家好,我是Android的新手,我在Notes应用程序中工作,直到现在我已经完成了主要的相间操作,并从Edittext接收了输入,但是当涉及到将数据保存到SQLite DB时,我的代码出现故障,请您告诉我我做错了什么吗?。

FormActivity方法

public void saveToDB(View view) {

    try {

        String subjet = asunto.getText().toString();
        String body = cuerpo.getText().toString();

        int day = myCalendar.get(Calendar.DAY_OF_MONTH);
        int month = myCalendar.get(Calendar.MONTH);
        int year = myCalendar.get(Calendar.YEAR);

        Note note = new Note(subject,body,day,month,year); // Create an instance of Note class
        DBHelper db = new DBHelper(this,"TaskList.db",null,1); // New Instance of DBHelper class
        db.InsertIntoTable(note); // pass the newly created Note instance to DBHelper insert method

        db.close(); // we close Database

        Toast.makeText(this,"Note saved !!",Toast.LENGTH_SHORT).show(); // Notify the user of the operation success

        Intent intent = new Intent(FormActivity.this,MainActivity.class); // Lets return to MainActivity again
        startActivity(intent);
    }

    catch (Exception e){
        Toast.makeText(this,"Unable to save note",Toast.LENGTH_SHORT).show();
    }
}

DBHelper插入方法

public Long InsertIntoTable(Note note){

    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();

    /* We organize the values in the respective rows */

    values.put(col_subject,note.getSubject());
    values.put(col_note,note.getBody());
    values.put(col_day,note.getDay());
    values.put(col_month,note.getMonth());
    values.put(col_year, note.getYear());

    // Time to insert data to Database
    long rowID = db.insert(table_name,null,values);
    return rowID;
}

我放了一个Toast,以防万一我的应用崩溃,并查看操作是否成功,但是每次都会失败。

RecyclerAdapter类

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.itemview,null);
    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {

    Note note = noteList.get(i);

    viewHolder.asunto.setText(note.getAsunto());
    viewHolder.nota.setText(note.getCuerpo());
    viewHolder.fecha.setText(note.getDia() + "/" + note.getMes() + "/" + note.getAno());
    viewHolder.etiqueta.setText(note.getTag());
}

这是在我的堆栈跟踪中:

E / RecyclerView:未连接适配器;跳过布局

0 个答案:

没有答案