如何将Firebase的值传递给下一个活动

时间:2018-11-19 13:47:09

标签: android firebase firebase-realtime-database

我们已经从Firebase获得了列表视图的密钥。 在选择键时查看下一个Acitivity中的值 我该如何声明putExtra?

示例)

  • 1:aaa

  • 2:bbb

  • 3:ccc

  • 4:ddd

列表视图

1

2

3

4 <<选择

我想在下一个活动中看到ddd。

LoadActivity.java

package com.dogeun.auth;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;


import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.FirebaseDatabase;


public class LoadActivity extends AppCompatActivity implements 
View.OnClickListener{

private FirebaseDatabase mFirebaseDatabase;
private FirebaseAuth mFirebaseAuth;
private FirebaseUser mFirebaseUser;
private ListView mListview;


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


    mFirebaseDatabase = FirebaseDatabase.getInstance();
    mFirebaseAuth = mFirebaseAuth.getInstance();
    mFirebaseUser = mFirebaseAuth.getCurrentUser();
    mListview = (ListView) findViewById(R.id.listview);

    final ArrayAdapter<String> adapter =
            new ArrayAdapter<String>(this, android.R.layout.two_line_list_item, android.R.id.text1);
    mListview.setAdapter(adapter);

    mFirebaseDatabase.getReference("Book/" + mFirebaseUser.getUid())
            .addChildEventListener(new ChildEventListener() {
                @Override
                public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
                    adapter.add(dataSnapshot.getKey());
                    adapter.add((String) dataSnapshot.getValue());
                }

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

                }

                @Override
                public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {

                }

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

                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {

                }
            });

在此部分,值是 如何将其放入putExtra?

    mListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView parent, View v, int position, long id) {
            Intent intent = new Intent(LoadActivity.this, LoadtextActivity.class);
            intent.putExtra("text",?????);
            startActivity(intent);
        }
    }) ;

}

@Override
public void onClick(View v) {

}
}

请帮助我

1 个答案:

答案 0 :(得分:1)

我认为您在适配器中为同一商品/子项增加了两次价值。

@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
    adapter.add(dataSnapshot.getKey());
    adapter.add((String) dataSnapshot.getValue());

    // you should add it only once and get value by using adapter.getItem(position) and send that to other activity
}

在上面的代码中以注释的形式查看我的答案