Error editing data from a Sqlite Database

1

I explain, I can add data to my database Sqlite but I can not edit it, I comment.

In my RecordatoriosAdapter I have the following method:

private void editTaskDialog(final Recordatorios recordatorios) {
    LayoutInflater inflater = LayoutInflater.from(context);
    View subView = inflater.inflate(R.layout.add_recordatorio_layout, null);
    final EditText nameField = (EditText) subView.findViewById(R.id.enter_name);
    final EditText previaField = (EditText) subView.findViewById(R.id.enter_previa);
    final EditText favField = (EditText) subView.findViewById(R.id.enter_fav);
    if (recordatorios != null) {
        nameField.setText(recordatorios.getName());
        previaField.setText(recordatorios.getPrevia());
    }
    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setTitle("Edit recordatorios");
    builder.setView(subView);
    builder.create();
    builder.setPositiveButton("EDIT PRODUCT", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            final String name = nameField.getText().toString();
            final String previa = previaField.getText().toString();
            final String fav = favField.getText().toString();
            if (TextUtils.isEmpty(name) || TextUtils.isEmpty(previa) || TextUtils.isEmpty(fav)) {
                Toast.makeText(context, "Algo salió mal", Toast.LENGTH_LONG).show();
            } else {
                mDatabase.updateProduct(new Recordatorios(recordatorios.getId(), name, previa, fav));
                ActualizaRecyclerView();
            }
        }
    });
    builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(context, "Task cancelled", Toast.LENGTH_LONG).show();
        }
    });
    builder.show();
}

And from the edit button I call it like this:

editTaskDialog(singleRecordatorios);

And it works perfectly, I can edit the data, but, the problem comes now , I'm trying to change that method to Activity but I can not get it to work correctly, my code in Activity

public class EditarNota extends AppCompatActivity {

    public static SqliteDatabase mDatabase;
    public static EditText nameField, previaField, favField;
    Button btn1;
    Recordatorios recordatorios;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_editar_nota);

        btn1 = (Button) findViewById(R.id.btn1);

        nameField = (EditText)findViewById(R.id.enter_name);
        previaField = (EditText)findViewById(R.id.enter_previa);
        favField = (EditText)findViewById(R.id.enter_fav);
        mDatabase = new SqliteDatabase(this);

// cargo los datos
        if (recordatorios != null) {
            nameField.setText(recordatorios.getName());
            previaField.setText(recordatorios.getPrevia());
            favField.setText(recordatorios.getFav());
        }

        // edito
        btn1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                final String name = nameField.getText().toString();
                final String previa = previaField.getText().toString();
                final String fav = favField.getText().toString();
                if(TextUtils.isEmpty(name) || TextUtils.isEmpty(previa) || TextUtils.isEmpty(fav)){
                    Toast.makeText(EditarNota.this, "Hubo un error", Toast.LENGTH_LONG).show();
                }
                else{
                    mDatabase.updateProduct(new Recordatorios(recordatorios.getId(), name, previa, fav));
                    ActualizaRecyclerView();
                    finish();
                }
            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(mDatabase != null){
            mDatabase.close();
        }
    }
}

And from the edit button I call the Activity

Intent intent= new Intent(context, EditarNota.class);
context.startActivity(intent);

The problem comes when from the Activity EditarNotas I click on the save button ( btn1 ) the edition closes the application:

  

java.lang.NullPointerException: Attempt to invoke virtual method 'int other test.dos.RecyclerViewAdapter.Records.getId ()' on a null object reference

It seems that it does not load the ID when starting Activity EditarNota

Can someone help me? Thanks:)

    
asked by UserNameYo 20.06.2017 в 00:54
source

1 answer

1

In the editTaskDialog(final Recordatorios recordatorios) method you are passing the Recordatorios object as a parameter. However, in the activity EditarNota the object Recordatorios you are not starting it. Pass as a parameter when starting the activity the object to then get it and assign it.

Start the activity

Intent intent= new Intent(context, EditarNota.class);
intent.putExtra("ClaseRecordatorios", singleRecordatorios);
context.startActivity(intent);

Get the object in the activity

Recordatorios recordatorios;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_editar_nota);

    //obtienes el objeto y lo asignas 
    recordatorios = (Recordatorios) getIntent().getSerializableExtra("ClaseRecordatorios");
    //....

And the Reminders class, implement serializable

public class Recordatorios implements Serializable {
//...

Obviously extending your adapter, only implement Serializable

    
answered by 20.06.2017 / 08:25
source