0

I tried to change the database in the console firebase but the data in my adapter unchanged, recylerview was only changed when the application is closed and opened again

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private List<ModelDB> dbList = new ArrayList<>();
    private AdapterDB adapter;
    private RecyclerView recyclerView;
    FirebaseDatabase database;
    DatabaseReference reference;



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

        initFirebase();
        getData();
        initRecylerView();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_menu,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.add_person:
                Intent intent = new Intent(getApplicationContext(),RegisterActivity.class);
                startActivity(intent);
                break;
            default:
                Toast.makeText(getApplicationContext(),"lalalala",Toast.LENGTH_LONG).show();
                break;
        }

        return super.onOptionsItemSelected(item);
    }

    private void initFirebase(){
        database = FirebaseDatabase.getInstance();
        reference = database.getReference("user");

    }


    private void initRecylerView(){
        recyclerView = (RecyclerView)findViewById(R.id.rv_list);
        recyclerView.setHasFixedSize(true);
        adapter = new AdapterDB(dbList);
        recyclerView.setAdapter(adapter);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
        recyclerView.setLayoutManager(layoutManager);
    }


    private void getData() {
        reference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                //loop data all user
                for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) {

                    //instance object to get and set data
                    ModelDB modelDB = postSnapshot.getValue(ModelDB.class);

                    //adding data list from object
                    dbList.add(modelDB);

                }

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }

}

AdapterDB.java

public class AdapterDB extends RecyclerView.Adapter<AdapterDB.MyViewHolder> {

    private List<ModelDB> modelDBList;

    public AdapterDB(List<ModelDB> modelDBList) {
        this.modelDBList = modelDBList;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_data,parent,false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        ModelDB modelDB = modelDBList.get(position);
        holder.tv_nama.setText(modelDB.getNama());
        holder.tv_alamat.setText(modelDB.getAlamat());

    }

    @Override
    public int getItemCount() {
        return modelDBList.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {

        TextView tv_nama, tv_alamat;

        public MyViewHolder(View itemView) {
            super(itemView);

            tv_nama = (TextView) itemView.findViewById(R.id.tv_nama);
            tv_alamat = (TextView) itemView.findViewById(R.id.tv_alamat);
        }
    }
}

1 Answers1

2

You need to kind of let the adapter know that the data has changed and that the UI has to be refreshed or whatever. The OnDataChange in your code should be like this (check the last line within the method) -

@Override
public void onDataChange(DataSnapshot dataSnapshot) {
    //loop data all user
    for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) {

        //instance object to get and set data
        ModelDB modelDB = postSnapshot.getValue(ModelDB.class);

        //adding data list from object
        dbList.add(modelDB);
        adapter.notifyDataSetChanged();
    }

}

This should do the trick assuming there are no other bugs in some other parts of your code

Dibzmania
  • 1,934
  • 1
  • 15
  • 32
  • when I add functionality adapter.notifyDataSetChanged(); the recyclerview will update the data but the data becomes old data + new data – Fahruddin Yusuf Habibi Feb 12 '17 at 04:57
  • That's what i exactly meant in my answer when i said that there are no other bugs in your code :) . By the way I think `dbList` should be a set instead of a list which is why you are seeing duplicate objects in your view. But this is a guess since I have not really executed your code. – Dibzmania Feb 12 '17 at 05:05
  • thank you sir I solved problem by adding dbList.clear when there is a change of data :D – Fahruddin Yusuf Habibi Feb 12 '17 at 05:38