Update Listview with notifyDataSetChanged()

Asked

Viewed 368 times

1

Good afternoon guys, I’m not getting the use notifyDataSetChanged() to update my Listview.

My application takes the data entered in the database and lists in the registration page, soon after, if the user clicks on an item it can be changed, however, after being changed it is necessary to exit the page and enter again for the List View to be updated.

I tried to use the notifyDataSetChange() but I couldn’t. Someone could help me?

Main Activity with Listview:

public ArrayList<modelListprod> listadeprod;
public static AdapterProd itensprod;
private ListView listproduto;

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView( layout.activity_main);
        layoutContentMain = (ConstraintLayout)findViewById( id.layoutContentMainMenu);
        conectarBanc();
        selecionaemitente(conexao);

        listadeprod = new ArrayList<modelListprod>();
        itensprod = new AdapterProd(this, listadeprod);

        listproduto = (ListView) findViewById(id.listproduto);
        listproduto.setAdapter(itensprod);

    }

public void atualizalistaprod(){
        itensprod.notifyDataSetChanged();
    }



public void lprod(SQLiteDatabase db){
        ArrayList<modelListprod> prod = new ArrayList<modelListprod>();

        //Passa para dentro do while
        //listprod = new modelListprod();

        String descricao = "";
        String ean = "";
        String status = "";
        Double precoprod;
        String categoria;
        int cod;

        final SQLiteDatabase d5 = dadosOpenHelper.getReadableDatabase();
        final Cursor cursor5 = d5.query("produto" , new String[]{"descricao", "ean", "status", "precoprod", "codigocateg", "cod"},null, null, null   ,null,null,null);
        if (cursor5 != null) {
            if (cursor5.moveToFirst()) {

                do {

                    //uma nova instância por cada registo
                    listprod = new modelListprod();

                    descricao = cursor5.getString(0);
                    ean = cursor5.getString(1);
                    status = cursor5.getString(2);
                    precoprod = cursor5.getDouble(3);
                    categoria = cursor5.getString(4);
                    cod = cursor5.getInt(5);
                    listprod.setDescricao(descricao);
                    listprod.setCategoria(categoria);
                    listprod.setEan(ean);
                    listprod.setPreco(precoprod);
                    listprod.setStatus(status);
                    listprod.getId(cod);

                    //Adiciona ao array
                    listadeprod.add(listprod);

                } while (cursor5.moveToNext());
            }

            listproduto.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    modelListprod c = (modelListprod) parent.getItemAtPosition(position);
                    Intent intent = new Intent(getApplicationContext(), AlteraProd.class);
                    intent.putExtra("Dados", (Serializable) c);
                    startActivity(intent);
                }
            });
        }
        itensprod.notifyDataSetChanged();
        //Passa para dentro do while
        //listadeprod.add(listprod);

        AdapterProd adapterProd = new AdapterProd(this, listadeprod);
        listproduto.setAdapter(adapterProd);
    }

My Listview Adapter:

public class AdapterProd extends ArrayAdapter<modelListprod> {

    MainActivity m = new MainActivity();

    private Context context;
    private ArrayList<modelListprod> lista;



    public AdapterProd(Context context, ArrayList<modelListprod> lista){
        super(context, 0, lista);
        this.context = context;
        this.lista = lista;


    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
       final modelListprod itemposicao = this.lista.get(position);

        if(convertView == null){
            convertView = LayoutInflater.from(this.context).inflate(R.layout.list_prod, null);
        }

        TextView textView1 = (TextView) convertView.findViewById(R.id.tvdescricao2);
        textView1.setText(itemposicao.getDescricao());

        TextView textView2 = (TextView) convertView.findViewById(R.id.tvcategoria2);
        textView2.setText(itemposicao.getCategoria());

        TextView textView3 = (TextView) convertView.findViewById(R.id.tvpreco2);
        textView3.setText(itemposicao.getPreco().toString());

        TextView textView4 = (TextView) convertView.findViewById(R.id.tvean2);
        textView4.setText(itemposicao.getEan());

        TextView textView5 = (TextView) convertView.findViewById(R.id.tvstatus2);
        textView5.setText(itemposicao.getStatus());

        Button grava = (Button) convertView.findViewById( R.id.gravaalteraprod );
        grava.setOnClickListener( new View.OnClickListener() {


            public void onClick(View v) {
                itemposicao.setDescricao( itemposicao.getDescricao());
                itemposicao.setCategoria( itemposicao.getCategoria());
                itemposicao.setPreco( itemposicao.getPreco());
                itemposicao.setEan( itemposicao.getEan());
                itemposicao.setStatus( itemposicao.getStatus());
                m.atualizalistaprod();

            }
        } );



        return convertView;
    }
}
  • That method lprod is being called where?

  • The lprod method is on another button of my Main, it just calls the screen with the listview filled, the same problem ta be after changing the item. It changes normally, but listview does not update after being changed, it is necessary to close the screen again.

  • 1

    In your case you want to update only 1 line of your Listview neh? Unfortunately I don’t know how to answer your case, but maybe this link help you. Your question is very good.

  • Yes, every time I changed something in my database I wanted you to update my listview without having to go out and enter the screen again. But thank you very much for your help, I will study deeply and as soon as I have a solution I will post here.

  • 1

    Did you get my answer? I tested it here and it worked well for me

  • 1

    I’m sorry it took me so long to answer. Your code helped me a lot, but it didn’t work in my application, I identified the error. In my "convertview" I put that it has to fetch the layout of "list_prod" and in my setOnClickListener I put an event in the "gravaralteraprod" button that is in another layout, in case the layout "alteraprod". Mine continues with this little problem, but I am identifying a way to solve it. On the other hand, your answer is very correct, I ended up not paying much attention, thank you very much for the help.

Show 2 more comments

1 answer

2


On your Adapter inside the Onclick leave the following:

        itemposicao.setDescricao(itemposicao.getDescricao());
        itemposicao.setCategoria(itemposicao.getCategoria());
        itemposicao.setPreco(itemposicao.getPreco());
        itemposicao.setEan(itemposicao.getEan());
        itemposicao.setStatus(itemposicao.getStatus());
        //remove o item do seu model
        lista.remove(position);
        //e adiciona na mesma posição o item editado
        lista.add(position, itemposicao);
        this.notifyDataSetChanged();

I think that will enable your listview to be updated.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.