从Firebase数据库检索值

时间:2018-10-31 18:11:16

标签: android firebase

我正在尝试从Firebase数据库中为Android应用程序检索一些值。我可以正确上传,但无法将数据显示在列表视图中。我没有收到任何错误,但数据没有显示。

enter image description here

上传AddPostActivity.class

private EditText mPostTitle;
private EditText mPostDescription;
private Button mPostSend;

private String mPhoneNo;
private String mUsername;

private FirebaseDatabase mFirebaseDatabase;
private DatabaseReference mMessageDatabaseReference;
private ChildEventListener mMessageChildEventListener;

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

    mPhoneNo = loadSignIn();

    mFirebaseDatabase = FirebaseDatabase.getInstance();
    mFirebaseAuth = FirebaseAuth.getInstance();
    mFirebaseStorage = FirebaseStorage.getInstance();

    mMessageDatabaseReference = mFirebaseDatabase.getReference().child("message");

    mPostTitle = findViewById(R.id.title_appPostActivity);
    mPostDescription = findViewById(R.id.description_addPostActivity);
    mPostSend = findViewById(R.id.sendPost_appPostActivity);

    mPostSend.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String key = mMessageDatabaseReference.push().getKey();
            PostContent postContent = new PostContent(mPostTitle.getText().toString(), mPostDescription.getText().toString(), key);
            mMessageDatabaseReference.push().setValue(postContent);
        }
    });
}

PartyListActivity.class

检索
private String mPhoneNo;
private PostAdapter mPostAdapter;
private DatabaseReference mDatabaseReferance;
private ListView mListView;

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

    mDatabaseReferance = FirebaseDatabase.getInstance().getReference();
    mDatabaseReferance = mDatabaseReferance.child("message");

    mListView = findViewById(R.id.post_ListView);

    List<PostContent> postContents = new ArrayList<>();
    mPostAdapter = new PostAdapter(this, R.layout.post_content, postContents);
    mListView.setAdapter(mPostAdapter);

    mDatabaseReferance.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {

            PostContent content = dataSnapshot.getValue(PostContent.class);
            mPostAdapter.add(content);
        }

        @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) {

        }
    });
}

自定义适配器类 PostAdapter.class

public class PostAdapter extends ArrayAdapter<PostContent> {

public PostAdapter(Context context, int resource, List<PostContent> object) {
    super(context, resource, object);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        convertView = ((Activity) getContext()).getLayoutInflater().inflate(R.layout.post_content, parent, false);
    }

    TextView title = convertView.findViewById(R.id.title_postContent);
    TextView description = convertView.findViewById(R.id.description_postContent);

    PostContent object = getItem(position);

    title.setText(object.getTitle());
    description.setText(object.getDescription());
    return convertView;
}

列出项目内容类 PostContent.class

public class PostContent {

private String mTitle;
private String mDescription;
private String mKey;

public PostContent(){}

public PostContent(String title, String description, String key) {
    mTitle = title;
    mDescription = description;
    mKey = key;
}

public String getTitle(){ return mTitle; }

public String getDescription() {
    return mDescription;
}

public String getKey() { return mKey; }
}

2 个答案:

答案 0 :(得分:2)

更改类似于Firebase属性的成员变量

public class PostContent {

    private String title;
    private String description;
    private String key;

    //...
    }

答案 1 :(得分:0)

DatabaseReference databaseRef = FirebaseDatabase.getInstance().getReference("[ your dbreference]");

    String input = "G";

    DatabaseReference messages = databaseRef.child("message");
    Query messageQuery = cities.orderByKey().startAt(input).endAt(input + "\uf8ff");
    messageQuery.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            List<[your class]> messages = new ArrayList<your class>();

            for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                message.add("[write your classs here] get your object value here");
            }
            System.out.println(messages);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });