Sabari Govindarajan
Sabari Govindarajan

Reputation: 101

adding retrieved key value from firebase to the arraylist doesn't work

I'm using a recyclerview to display data from firebase. I used a arraylist to store they key values from firebase and then with that array list i'm populating the recycler view but the problem here is the key values doesn't get added to the list but when i use a toast to display the key values its working fine.What's the mistake i made and how do i fix that?

package com.sabari.inventory;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends AppCompatActivity implements MyAdapter.ClickListener{

RecyclerView recyclerView;
MyAdapter adapter;
List<Info> infos;
DatabaseReference myRef,dr;
List<String> tit,d;
int position;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    recyclerView = (RecyclerView) findViewById(R.id.myRecView);
    tit = new ArrayList<>();
    d = new ArrayList<>();

    myRef = FirebaseDatabase.getInstance().getReference();
    dr = myRef.child("Machines");
    adapter = new MyAdapter(getApplicationContext(),getData());
    adapter.setClickListener(this);
    recyclerView.setAdapter(adapter);
    recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));

}

public List<Info> getData(){

    infos = new ArrayList<>();
    dr.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            tit.add(dataSnapshot.getKey());
            adapter.notifyDataSetChanged();
        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

    for(int i=0;i<tit.size();i++) {
        Info now = new Info();
        now.title = tit.get(i);
        now.desc = "";
        infos.add(now);
    }return infos;
}

@Override
public void itemClicked(View view, int position) {
        MyDialog myDialog = new MyDialog();
        myDialog.show(getSupportFragmentManager(),"");

}
}

Info class

package com.sabari.inventory;



public class Info {

int img;
String title;
String desc;
}

Adapter class

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyVH>  {

LayoutInflater layoutInflater;
Context context;
ClickListener clickListener;
List<Info> infoList = Collections.emptyList();
public MyAdapter(Context context,List<Info> infoList) {
    layoutInflater=LayoutInflater.from(context);
    this.infoList = infoList;
    this.context=context;
}

@Override
public MyVH onCreateViewHolder(ViewGroup parent, int viewType) {
    View view=layoutInflater.inflate(R.layout.single_row,parent,false);
    MyVH myVH = new MyVH(view);
    return myVH;
}

@Override
public void onBindViewHolder(MyVH holder, final int position) {
    Info now = infoList.get(position);
    holder.textView1.setText(now.title);
    holder.textView2.setText(now.desc);
}

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

public void setClickListener(ClickListener clickListener)
{
    this.clickListener=clickListener;
}



class MyVH extends RecyclerView.ViewHolder implements View.OnClickListener{

    ImageView imageView;
    TextView textView1,textView2;
    public MyVH(View itemView) {
        super(itemView);
        imageView = (ImageView) itemView.findViewById(R.id.imageView);
        textView1 = (TextView) itemView.findViewById(R.id.textView);
        textView2 = (TextView) itemView.findViewById(R.id.textView2);

        itemView.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        if(clickListener!=null)
        {
            clickListener.itemClicked(view,getPosition());
        }
    }
}

public interface ClickListener{
    public void itemClicked(View view,int position);

}
}

Modified getData() method

 public List<Info> getData(){

    infos = new ArrayList<>();

    dr.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            Info info= dataSnapshot.getValue(Info.class);
            tit.add(info.getTitle());
            adapter.notifyDataSetChanged();
        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

    for(int i=0;i<1;i++) {
        Info now = new Info();
        now.title = tit.get(i);
        now.desc = "";
        infos.add(now);
    }return infos;
}

Upvotes: 1

Views: 281

Answers (2)

AguThadeus
AguThadeus

Reputation: 330

In your onChildAdded() method do this, tit and d aren't necessary:

    @Override
    public void onChildAdded(DataSnapshot dataSnapshot, String s) {
        Info now = new Info();
        now.title = dataSnapshot.getKey();
        now.desc = "";
        infos.add(now);
        adapter.notifyDataSetChanged();
    }

Upvotes: 1

Milos Lulic
Milos Lulic

Reputation: 627

Create your Info.class as Object:

public class Info {

int img;
String title;
String desc;

public Info() {};

public Info(int img, String title, String desc) {
    this.img = img;
    this.title = title;
    this.desc = desc;
}
public int getImg() { return img; }
public String getTitle() { return title; }
public String getDesc() { return desc; }
}

In your listener

@Override
    public void onChildAdded(DataSnapshot dataSnapshot, String s) {
        Info info = dataSnapshot.getValue(Info.class);
        tit.add(info.getTitle());
        adapter.notifyDataSetChanged();
    }

Upvotes: 1

Related Questions