在Firebase中,calss适配器没有任何填充

时间:2017-07-24 13:45:39

标签: android android-recyclerview recycler-adapter

在FirebaseHelper类中我声明检索ArrayList方法返回spacecrafts ArrayList并调用fetchData方法在spacecrafts中添加新Item,在MainActivity中我在onCreate方法上用helper.retrieve填充我的适配器,并使Dialog在添加用户项时点击saveBtn使用helper.retrieve填充我的适配器,但是适配器没有任何填充,我让Toast检测到问题,发现每次使用new Item时都会填充spacecrafts.size,但每次都是helper.retrieve()。size = 0,有谁能告诉我我的错误?

这是FirebaseHelper类:

class FirebaseHelper {
private DatabaseReference db;
private ArrayList<Spacecraft> spacecrafts=new ArrayList<>();
int x;

public FirebaseHelper(DatabaseReference db) {
    this.db = db;
}
public Boolean save(Spacecraft spacecraft)
{
    Boolean saved ;
    if(spacecraft==null)
    {
        saved =false;
    }else
    {
    try
        {
            db.child("Spacecraft").push().setValue(spacecraft);
            saved =true;
        }catch (DatabaseException e)
        {
            e.printStackTrace();
            saved =false;
        }
    }
    return saved;
} 
private void fetchData(DataSnapshot dataSnapshot)
{
     spacecrafts.clear();
    for (DataSnapshot ds : dataSnapshot.getChildren())
    {
       Spacecraft spacecraft=ds.getValue(Spacecraft.class);
       spacecrafts.add(spacecraft);
       x=spacecrafts.size();
    }
} 

ArrayList<Spacecraft> retrieve()
{
    db.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            fetchData(dataSnapshot);
        }
        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {
            fetchData(dataSnapshot);
        }
        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {
        }
        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
   return spacecrafts;
}
}

这里是Main Activity类:

    public class MainActivity extends AppCompatActivity {
    DatabaseReference db;
    FirebaseHelper helper;
    MyAdapter adapter;
    private RecyclerView rv;
    EditText nameEditTxt,propTxt,descTxt;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.content_main);

    rv= (RecyclerView) findViewById(R.id.rv);
    rv.setLayoutManager(new LinearLayoutManager(this));

    db= FirebaseDatabase.getInstance().getReference();
    helper=new FirebaseHelper(db);

    adapter=new MyAdapter(this,helper.retrieve());
    rv.setAdapter(adapter);
    Toast.makeText(MainActivity.this, "helper.retrieve().size()  =  " + helper.retrieve().size(), Toast.LENGTH_SHORT).show();
    Toast.makeText(MainActivity.this, "spacecrafts.size()  =  " + helper.x, Toast.LENGTH_SHORT).show();

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            displayInputDialog();
            Toast.makeText(MainActivity.this, "spacecrafts.size()  =  " + helper.x, Toast.LENGTH_SHORT).show();
            Toast.makeText(MainActivity.this, "helper.retrieve().size()  =  " + helper.retrieve().size(), Toast.LENGTH_SHORT).show();
        }
    });
}

private void displayInputDialog()
{
    final Dialog d=new Dialog(this);
    d.setTitle("Save To Firebase");
    d.setContentView(R.layout.input_dialog);
    nameEditTxt= (EditText) d.findViewById(R.id.nameEditText);
    propTxt= (EditText) d.findViewById(R.id.propellantEditText);
    descTxt= (EditText) d.findViewById(R.id.descEditText);
    Button saveBtn= (Button) d.findViewById(R.id.saveBtn);

    saveBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //GET DATA
            String name=nameEditTxt.getText().toString();
            String propellant=propTxt.getText().toString();
            String desc=descTxt.getText().toString();
            //SET DATA
            Spacecraft s=new Spacecraft();
            s.setName(name);
            s.setPropellant(propellant);
            s.setDescription(desc);

            if(name.length() > 0)
            {
                if(helper.save(s))
                {
                    nameEditTxt.setText("");
                    propTxt.setText("");
                    descTxt.setText("");
                    adapter=new MyAdapter(MainActivity.this,helper.retrieve());               
                    Toast.makeText(MainActivity.this, "spacecrafts.size()  =  " + helper.x, Toast.LENGTH_SHORT).show();
                    Toast.makeText(MainActivity.this, "helper.retrieve().size()  =  " + helper.retrieve().size(), Toast.LENGTH_SHORT).show();
                    rv.setAdapter(adapter);
                    adapter.notifyDataSetChanged();
                    d.hide();
                }
            }else
            {
                Toast.makeText(MainActivity.this, "Name Must Not Be Empty", Toast.LENGTH_SHORT).show();
            }
        }
    });
    d.show();
}
}

2 个答案:

答案 0 :(得分:0)

Firebase操作是异步运行的,这意味着它们在一个单独的线程上运行,因此您的方法ArrayList<Spacecraft> retrieve()会在填充之前返回列表。

答案 1 :(得分:0)

我尝试了这个及其工作

     private void fetchData(DataSnapshot dataSnapshot)
     {
    //   spacecrafts.clear();
    for (DataSnapshot ds : dataSnapshot.getChildren())
    {
       Spacecraft spacecraft=ds.getValue(Spacecraft.class);
       spacecrafts.add(spacecraft);
       x=spacecrafts.size();
    }
  }

ArrayList<Spacecraft> retrieve()
{
    db.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
             // try to clear spacecrafts here
            spacecrafts.clear();
            fetchData(dataSnapshot);
        }
        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {
            // and here   
            spacecrafts.clear();
            fetchData(dataSnapshot);
        }
        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {
        }
        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
   return spacecrafts;

   }
   }