Updating values in a ListView

3

I add values that are within 3 arrays in hashMap to insert in listview :

 ArrayList<HashMap<String, String>> lista = new ArrayList<HashMap<String,String>>();

    for(int i=0; i<tema.length; i++ ) {
        HashMap<String,String> item = new HashMap<String,String>();
        item.put("tema", tema[i]+": "+tempo[i]);
        item.put("palavras", palavras[i]);
        lista.add(item);
    }

What I want to do is when the user clicks the SUBIR button, for example. The item that is in position 2 (if it has been clicked) goes up to position 1, and that which was in position 1 occupies position 2.

Replacing:

 up.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        if(posicao!=0 && tema.length>1) {

              auxtema = tema[posicao-1];
              auxpalavras = palavras[posicao-1];
              auxtempo = tempo[posicao-1];

              tema[posicao-1] = tema[posicao];
              palavras[posicao-1] = palavras[posicao];
              tempo[posicao-1] = tempo[posicao];

              tema[posicao] = auxtema;
              palavras[posicao] = auxpalavras;
              tempo[posicao] = auxtempo;


        }

Inside the array I could do. I replace, but how do I update this within the listView , and update it?

    
asked by anonymous 06.02.2014 / 18:07

2 answers

2

Since your code is not showing how you instantiated the ListView you can not be sure if you are using a adapter default or your own adapter . Assuming you've implemented your own ListAdapter (inheriting directly or indirectly from BaseAdapter ) or used some adapter able to handle dynamic updates (eg, ArrayAdapter ), the solution is to call the notifyDataSetChanged after updating the template:

To do this (assuming that listView is your ListView )

((BaseAdapter) listView.getAdapter()).notifyDataSetChanged();

Update : For the structure of your code you should be using a SimpleAdapter .

If my answer and that of @ R3oLoN did not make sense I strongly recommend reading the Lars Vogel tutorial: Using lists in Android

    
06.02.2014 / 18:36
2

I do not know if I understand your question well, but if your listview has a Adapter you can implement a method that updates ArrayList within Adapter and calls method notifyDataSetChanged

Ex:

public void atualizarLista(ArrayList<HashMap<String, String>> lista) {
    this.lista = lista;
    notifyDataSetChanged();
}
    
06.02.2014 / 18:32