0
I’m trying to edit a contact within my schedule. But when I save the edit it creates a new one.
public class MainActivity extends AppCompatActivity {
public static final String CONTATO = "contato";
public static final int REQUEST_NOVO = 1;
private ListView listaV;
private List<Contato> lista = new ArrayList<>();
private AdapterContato adapter;
private ContatoDAO dao;
private Contato contatoselecionado;
private static Bitmap imagem;
public static Bitmap getImagem() {
return imagem;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
private void atualizacao(){
dao = new ContatoDAO(this);
lista = dao.getLista();
dao.close();
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
MenuItem editar = menu.add("Editar");
editar.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
Intent intent = new Intent(MainActivity.this,NovoActivity.class);
intent.putExtra(CONTATO, contatoselecionado);
startActivity(intent);
return false;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;}
public void novo(MenuItem item) {
Intent intent = new Intent(this, NovoActivity.class);
startActivityForResult(intent, REQUEST_NOVO);
}
@Override
protected void onResume() {
super.onResume();
listaV = findViewById(R.id.Contatos);
adapter = new AdapterContato(this, lista);
listaV.setAdapter(adapter);
atualizacao();
listaV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int posicao, long l) {
contatoselecionado = (Contato) adapterView.getItemAtPosition(posicao);
Intent itent = new Intent(MainActivity.this, DetalheActivity.class);
itent.putExtra(CONTATO,contatoselecionado);
startActivity(itent);
}
});
listaV.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int posicao, long l) {
contatoselecionado = (Contato) adapterView.getItemAtPosition(posicao);
return false;
}
});
registerForContextMenu(listaV);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == REQUEST_NOVO){
switch(resultCode){
case RESULT_OK:
/*
Contato contato = (Contato) data.getSerializableExtra(MainActivity.CONTATO);
contato.setImagem(NovoActivity.getimagem());
*/
atualizacao();
adapter.notifyDataSetChanged();
break;
case RESULT_CANCELED:
Toast.makeText(this,"Cancelado.", Toast.LENGTH_LONG).show();
break;
}
}}
}
The only code I see to alter the data is
dao = new ContatoDAO(this);
In what cases would this edit rather than add ? I’m actually merely guessing because this class is not in the question.– Isac
The onCreateContextMenu part has this intention.
– Ygor Costa