在Firebase中存储数据

时间:2016-08-23 21:22:57

标签: android firebase firebase-realtime-database

我在Firebase中存储一些数据时遇到问题。我基本上想在我的Firebase数据库中存储链接和日期(来自之前的片段)。

我可以存储链接但是点击FAB时似乎没有将日期存储在我的数据库中。任何帮助将不胜感激,相关代码如下:

public class Details extends AppCompatActivity {

private static final String KEY_LINK = "link";

RequestQueue requestQueue;

public static final int CONNECTION_TIMEOUT = 10000;
public static final int READ_TIMEOUT = 15000;

private RecyclerView rv;
private CardView cardView;
FloatingActionButton fab;
private boolean isChecked = true;

private List<Detail> detailList = new ArrayList<>();

private Vibrator vib;

private Firebase firebaseRef;

private static final String FIREBASE_URL = "https://athenajs.firebaseio.com";


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_details);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

        Firebase.setAndroidContext(this);

        Intent in = getIntent();

        firebaseRef = new Firebase(FIREBASE_URL).child("favourites");

    //         Get JSON values from previous intent
        final String link = in.getStringExtra(KEY_LINK);
        final String date = in.getStringExtra("date");

        final Favclick mFav = new Favclick(link, date);

        final String pushId = firebaseRef.push().getKey();

        vib = (Vibrator) this.getSystemService(VIBRATOR_SERVICE);
        cardView = (CardView) findViewById(R.id.cv);
        rv = (RecyclerView) findViewById(R.id.rv);


        fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setTag(1);
        fab.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {

                final int status = (Integer) view.getTag();
                if (status == 1) {
                    fab.setImageResource(R.drawable.ic_favorite_white_48dp);
                    Snackbar.make(view, "Added to favourites!", Snackbar.LENGTH_LONG)
                            .setAction("Action", null).show();
                    view.setTag(0);

                    firebaseRef.child(pushId).setValue(mFav);

                } else {
                    fab.setImageResource(R.drawable.ic_favorite_border_white_48dp);
                    Snackbar.make(view, "Removed from favourites!", Snackbar.LENGTH_LONG)
                            .setAction("Action", null).show();
                    view.setTag(1);
                    firebaseRef.child(pushId).removeValue(null);
                }


            }

        });

1 个答案:

答案 0 :(得分:2)

试试这个:

Firebase favRef = firebaseRef.child("favourites");

Map<String, String> data = new HashMap<String, String>();
data.put("date", date);
data.put("link", link);
favRef.push().setValue(data);

在您的fab onClick方法中。