Updating Listview when I come back from a screen

2

I have a ListView that has a button that goes to another screen that when filling that screen and clicking the save button, it returns the ListView but I did not find a way to update this ListView.

Follow 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 revenue and returns to 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();


}
    
asked by anonymous 02.02.2015 / 17:52

2 answers

4

If I understand your problem, you should pass the call to refreshLista(); to the% method of Activity :

public class ListReceitaActivity extends ActionBarActivity {

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

    //refreshLista();
}

@Override
public void onResume(){

    refreshLista();
    super.onResume();
}
    
02.02.2015 / 18:16
1

The problem happens because when you go to the next activity, EditaReceitaActivity.class , you are not destroying the previous activity, which is not recommended, if you do not want to take another time to recreate it. However, your problem requires the destruction of the core activity as one of the solutions. It would look something like this, in the Oncreate of EditaReceitaActivity.class activity:

Intent.FLAG_ACTIVITY_CLEAR_TOP

About FLAG_ACTIVITY_CLEAR_TOP:

link

The other option would be to use onResume (), because when you get back, it will be "summarizing the main activity AND NOT recreating, as you thought":

@Override
protected void onResume(){

refreshLista();
super.onResume();
}

About onResume ():

link

    
02.02.2015 / 18:16