2
I have a Listview that has a button that goes to another screen that when filling this screen and clicking the save button, it returns to Listview but I could not find a way to update this Listview.
Follows the codes:
Listactivity
public class ListReceitaActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_receita);
refreshLista();
}
public void refreshLista(){
ReceitaDAO receita = new ReceitaDAO(this);
List<Receita> list = receita.getLista();
ListView listView = (ListView) findViewById(android.R.id.list);
listView.setAdapter(new ReceitaAdapter(this, list, listView));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_list_receita, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
case R.id.action_add_despesa:
Intent intent = new Intent(this, EditaReceitaActivity.class);
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
}
Adapter
public class ReceitaAdapter extends BaseAdapter {
private Context context;
List<Receita> lista;
private ListView listView;
NumberFormat nf = NumberFormat.getCurrencyInstance();
public ReceitaAdapter(Context context, List<Receita> lista, ListView listView) {
this.context = context;
this.lista = lista;
this.listView = listView;
}
@Override
public int getCount() {
return lista.size();
}
@Override
public Object getItem(int position) {
return lista.get(position);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int position, View view, ViewGroup viewGroup) {
final int auxPosition = position;
LayoutInflater inflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final RelativeLayout layout = (RelativeLayout)
inflater.inflate(R.layout.row, null);
listView.setDivider(null);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(context, EditaReceitaActivity.class);
int position = (int) l;
Receita receita = (Receita) getItem(position);
intent.putExtra("id", receita.getId());
context.startActivity(intent);
}
});
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, final long l) {
AlertDialog.Builder alert = new AlertDialog.Builder(context);
final CharSequence[] opcoes = {"Editar", "Apagar"};
alert.setTitle("Escolha a opção");
alert.setItems(opcoes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int item) {
switch (item) {
case 0:
Intent intent = new Intent(context, EditaReceitaActivity.class);
int position = (int) l;
Receita receita = (Receita) getItem(position);
intent.putExtra("id", receita.getId());
context.startActivity(intent);
break;
case 1:
ReceitaDAO receitaDAO = new ReceitaDAO(context);
int pos = (int) l;
Receita rcta = (Receita) getItem(pos);
receitaDAO.deletar(rcta);
break;
}
}
});
alert.show();
return true;
}
});
TextView data = (TextView)
layout.findViewById(R.id.ddata);
data.setText(lista.get(position).getData());
TextView desc = (TextView)
layout.findViewById(R.id.ddesc);
desc.setText(lista.get(position).getDescricao());
TextView valor = (TextView)
layout.findViewById(R.id.vvalor);
valor.setText(nf.format(lista.get(position).getValor()));
return layout;
}
}
Method that saves the recipe and goes back to the list
public void salvarReceita(View view) {
String[] data = etData.getText().toString().split("/");
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
receita.setValor(Float.valueOf(etValor.getText().toString().substring(2, etValor.getText().toString().length()).replace(",", ".")));
try {
receita.setData((data[2] + "-" + data[1] + "-" + data[0]));
} catch (Exception e) {
e.printStackTrace();
}
receita.setDescricao(etDesc.getText().toString());
ReceitaDAO receitaDAO = new ReceitaDAO(this);
if (receita.getId() == 0)
receitaDAO.inserir(receita);
else
receitaDAO.atualizar(receita);
Toast.makeText(this, "Receita adicionada!", Toast.LENGTH_SHORT).show();
finish();
}
When you reopen the application, the saved information appears updated?
– Lollipop
@Lollipop reopens like, I go out and get in the app? if that is so, yes, the app inserts, edits, queries and deletes, it just doesn’t update the listview automatically, it has to go out and back in the list.
– Allan Chrystian
Going out and coming back how? Leaving the app and coming back? OR FROM ONE SCREEN TO ANOTHER(or is this what you wanted?)?
– Lollipop
Actually I have Mainactivity, I call the listview, I need to go back to main and claim the list for the updated data, IE, it only updates when calls onCreate(). @Lollipop
– Allan Chrystian
When I give a Finish() on the add screen the app goes to the list, and this is not updated, You have to go back to main and open the list again to be updated list. @Lollipop
– Allan Chrystian