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:)