listView dynamic add item

前端 未结 4 1089
攒了一身酷
攒了一身酷 2020-11-28 12:46

I used ListView to dynamic add item,but there is a problem about not Smooth add. there are textView and button in my listActivity,Iwant to Press button ,then

4条回答
  •  被撕碎了的回忆
    2020-11-28 13:31

    Yes!!, the method notifyDataSetChanged() applied in the ArrayAdapter before you fulling him, was the solution for me. Reading from Firebase.

    Objects

        private DatabaseReference myRef;
        ArrayList lista;
        ArrayAdapter adapter;
    

    OnCreate

        FirebaseDatabase database = FirebaseDatabase.getInstance();
        myRef = database.getReference("chat");
    
        //GETTIN MY DATA TO SHOW IN CHAT
        lista = new ArrayList();
    

    OnResume

        myRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for (DataSnapshot postSnappy : dataSnapshot.getChildren()){
                    for (DataSnapshot rePostSnappy : postSnappy.getChildren()){
                        // Defined Array values to show in ListView
                        lista.add(new String(rePostSnappy.getValue().toString()));
                        adapter.notifyDataSetChanged();//Notyfing adapter that will goes to change
                    }
                }
            }
    
            @Override
            public void onCancelled(DatabaseError databaseError) {
    
            }
        });
    
        adapter = new ArrayAdapter(this,
                android.R.layout.simple_list_item_1, lista);
    
        ListView listVista = (ListView) findViewById(R.id.list);
        listVista.setAdapter(adapter);
    

提交回复
热议问题