基本上我正在制作一个应用,我正在使用import random
import sys
import os
import time
clear = lambda: os.system('cls')
clear()
y = 0
z = 1
x = 0
nNum = 1
uList = []
sList = []
listL = int(raw_input("How many elements would you like to be in your list?"))
clear()
while x < listL:
uList.append(int(raw_input("Num %s:" %(nNum))))
x = x + 1
nNum = nNum + 1
p = len(uList) - 1
clear()
print("Your list was %s!" %(uList))
while z <= p:
while uList[y] > uList[z]:
j = uList[y]
del uList[y]
uList.append(j)
print(uList)
并尝试检索firebase
,就像我在real-time database
中存储图像并复制&#34;下载网址&# 34;链接并将其粘贴到firebase数据库中,以便每当我运行我的应用程序时,应该有一个图像,标题和描述应该在该图像下面。我基本上想要将图像,标题和描述添加到firebase数据库并在应用程序中检索。
Activity_main: -
firebase storage
individual_row: -
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recyclerview">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
Firebase数据库规则: -
{
&#34;规则&#34;:{
&#34; .read&#34;:&#34; true&#34;,
&#34; .write&#34;:&#34; true&#34;
}
}
Firebase存储规则: -
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:layout_height="100dp"
android:src="@mipmap/ic_launcher_round"
android:id="@+id/image"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#000000"
android:text="title"
android:id="@+id/title"/>
<TextView
android:layout_width="match_parent"
android:textColor="#000000"
android:layout_height="wrap_content"
android:text="description"
android:id="@+id/description"/>
</LinearLayout>
</android.support.v7.widget.CardView>
MainActivity: -
service firebase.storage {
match /b/{bucket}/o {
match[enter image description here][1] /{allPaths=**} {
// Allow access by all users
allow read, write;
}
}
}
博客(活动): -
package com.namy86.dtunews;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.squareup.picasso.Picasso;
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private DatabaseReference myref;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView=(RecyclerView)findViewById(R.id.recyclerview);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
myref= FirebaseDatabase.getInstance().getReference().child("/blog");
FirebaseRecyclerAdapter<Blog,BlogViewHolder> recyclerAdapter=new FirebaseRecyclerAdapter<Blog,BlogViewHolder>(
Blog.class,
R.layout.individual_row,
BlogViewHolder.class,
myref
) {
@Override
protected void populateViewHolder(BlogViewHolder viewHolder, Blog model, int position) {
viewHolder.setTitle(model.getTitle());
viewHolder.setDescription(model.getDescription());
viewHolder.setImage(model.getImage());
}
};
recyclerView.setAdapter(recyclerAdapter);
}
public static class BlogViewHolder extends RecyclerView.ViewHolder {
View mView;
TextView textView_title;
TextView textView_decription;
ImageView imageView;
public BlogViewHolder(View itemView) {
super(itemView);
mView=itemView;
textView_title = (TextView)itemView.findViewById(R.id.title);
textView_decription = (TextView) itemView.findViewById(R.id.description);
imageView=(ImageView)itemView.findViewById(R.id.image);
}
public void setTitle(String title)
{
textView_title.setText(title+"");
}
public void setDescription(String description)
{
textView_decription.setText(description);
}
public void setImage(String image)
{
Picasso.with(mView.getContext())
.load(image)
.into(imageView);
}
}
}
Firebase数据库结构: -
demofirebase-1faa
POST1
package com.namy86.dtunews;
public class Blog {
private String title,description,image;
public Blog() {
}
public Blog(String title, String description, String image) {
this.title = title;
this.description = description;
this.image = image;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
}
POST2
-Description1="First Post"
-Image1="https://firebasestorage.googleapis.com/v0/b/demofirebase-1faaa.appspot.com/o/Unknown.jpg?alt=media&token=6f5ade3c-d615-4871-90a0-1b4a55e6e00c"
-Title1="Namy"
答案 0 :(得分:1)
假设您已经正确配置了项目,您可以通过获取引用,创建新节点(如Alex Mamo所说,您无法在没有特定节点的情况下发送它)并将对象拟合来将数据发送到firebase在里面。见下文:
// get your reference
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
// refer your child and use push to generate a new unique node, then put your object
reference.child("blog").push().setValue(blog);
加入firebase和适配器可以通过两种方式完成:显示检索对象或一次显示所有对象。
创建接收项目的接口:
public interface ItemListener<T> {
void onItemAdded(@NonNull final T item);
void onItemRemoved(@NonNull final T item);
}
在DAO中,创建一个方法以从博客节点获取所有对象:
public void getBlogList(@NonNull final ItemListener listener) {
FirebaseDatabase.getInstance().getReference().child("blog").orderByKey()
.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
final Blog blog = dataSnapshot.getValue(Blog.class);
listener.onItemAdded(blog);
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
final Blog blog = dataSnapshot.getValue(Blog.class);
listener.onItemRemoved(blog);
}
@Override public void onChildChanged(DataSnapshot dataSnapshot, String s) {}
@Override public void onChildMoved(DataSnapshot dataSnapshot, String s) {}
@Override public void onCancelled(DatabaseError databaseError) {}
});
}
在适配器内部,在构造函数中调用上面的方法,将收到的博客发送到列表并通知:
public class Adapter extends RecyclerView.Adapter<BlogViewHolder> {
private List<Blog> mBlogList;
public Adapter() {
getBlogList(new ItemListener<Blog>() {
@Override
public void onItemAdded(@NonNull final Blog blog) {
mBlogList.add(blog);
notifyItemInserted(mBlogList.size);
}
@Override
public void onItemRemoved(@NonNull final Blog blog) {
for (Integer i = 0; i < mBlogList.size(); i++) {
final Blog innerBlog = mBlogList.get(i);
// since you don't store a key, you gotta check all attributes
if (innerBlog.getTitle.equals(blog.getTitle) &&
innerBlog.getDescription.equals(blog.getDescription) &&
innerBlog.getImage.equals(blog.getImage)) {
mBlogList.remove(innerBlog);
notifyItemRemoved(i);
}
}
}
});
}
@Override
public BlogViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new BlogViewHolder(LayoutInflater.from(parent.getContext())
.inflate(R.layout.individual_row, parent, false));
}
@Override
public void onBindViewHolder(BlogViewHolder holder, int position) {
final Blog blog = holder.get(position);
holder.setTitle(blog.getTitle());
holder.setDescription(blog.getDescription());
holder.setImage(blog.getImage());
}
@Override
public int getItemCount() {
return mBlogList.size();
}
}
创建接口以接收列表:
public interface ListListener<T> {
void onListRetrieved(@NonNull final List<T> list);
}
在DAO中,创建一个方法,从博客节点一次获取所有对象:
public void getBlogList(@NonNull final ListListener listener) {
final List<Blog> blogList = new ArrayList<>();
FirebaseDatabase.getInstance().getReference().child("blog")
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (final DataSnapshot innerDataSnapshot : dataSnapshot.getChildren()) {
final Blog blog = innerDataSnapshot.getValue(Blog.class);
blogList.add(blog);
}
listener.onListRetrieved(blogList);
}
@Override
public void onCancelled(DatabaseError databaseError) {}
});
}
在适配器内部,在构造函数中调用上面的方法,克隆收到的列表并通知:
public class Adapter extends RecyclerView.Adapter<BlogViewHolder> {
private List<Blog> mBlogList;
public Adapter() {
getBlogList(new ListListener<Blog>() {
@Override
public void onListRetrieved(@NonNull final List<Blog> blogList) {
mBlogList.addAll(blogList);
notifyDataSetChanged();
}
});
}
// same code as first Adapter
}
然后,您可以将此适配器添加到RecyclerView
:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
recyclerView.setAdapter(new Adapter());
}
答案 1 :(得分:0)
您不能将Image1
,Image2
等作为Firebase数据库中的密钥。你的密钥必须在每个节点中始终相同。您的数据库应如下所示:
demofirebase-1faa
|
--- Post1
| |
| --- Description: "First Post"
| |
| --- Image: "https://firebasestorage.googleapis.com/v0/b/demofirebase-1faaa.appspot.com/o/Unknown.jpg?alt=media&token=6f5ade3c-d615-4871-90a0-1b4a55e6e00c"
| |
| --- Title: "Namy"
|
--- Post2
|
--- Description: "Second Post"
|
--- Image: "https://firebasestorage.googleapis.com/v0/b/demofirebase-1faaa.appspot.com/o/Unknown.jpg?alt=media&token=6f5ade3c-d615-4871-90a0-1b4a55e6e00c"
|
--- Title: "Naman"
要根据此数据库架构获取图像,请使用以下代码:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
ValueEventListener eventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String image = ds.child("image").getValue(String.class);
Log.d("TAG", image);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {}
};
rootRef.addListenerForSingleValueEvent(eventListener);