期望的行为:尝试显示单个用户信息,即登录后来自Firebase的所有项目标题和说明“ protitle”和“ prodesc”数据。
注意:我正在使用intent.putExtra()从登录页面推送用户名
主要类别:
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
public class BeginStart extends AppCompatActivity {
FirebaseDatabase rootNode;
DatabaseReference ref;
private RecyclerView recyclerView;
String Receive_username;
private FirebaseRecyclerOptions<model> options;
private FirebaseRecyclerAdapter<model, MyViewHolder> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.testlayout);
//receive username to connect data
Intent intent = getIntent();
Receive_username = intent.getStringExtra("push_username");
rootNode = FirebaseDatabase.getInstance();
ref = rootNode.getReference("Users").child(Receive_username);
recyclerView =findViewById(R.id.testrecycle);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
options = new FirebaseRecyclerOptions.Builder<model>().setQuery(ref, model.class).build();
adapter = new FirebaseRecyclerAdapter<model, MyViewHolder>(options) {
@Override
protected void onBindViewHolder(@NonNull final MyViewHolder holder, int position, @NonNull final model model) {
//Toast.makeText(getApplicationContext(), "On Bind!", Toast.LENGTH_SHORT).show();
holder.textViewtitle.setText(model.getProtitle());
holder.textViewdesc.setText(model.getProdesc());
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_view_layout,parent,false);
return new MyViewHolder(v);
}
};
adapter.startListening();
recyclerView.setAdapter(adapter);
}
}
模型类别:
public class model {
String protitle, prodesc;
public model() {
}
public model(String protitle, String prodesc) {
this.protitle = protitle;
this.prodesc = prodesc;
}
public String getProtitle() {
return protitle;
}
public void setProtitle(String protitle) {
this.protitle = protitle;
}
public String getProdesc() {
return prodesc;
}
public void setProdesc(String prodesc) {
this.prodesc = prodesc;
}
}
查看处理程序类:
import android.view.View;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
class MyViewHolder extends RecyclerView.ViewHolder {
TextView textViewtitle, textViewdesc;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
textViewtitle = itemView.findViewById(R.id.textviewtitle);
textViewdesc = itemView.findViewById(R.id.textviewdesc);
}
}
RecyclerView布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/testrecycle"
android:layout_marginRight="16dp"
android:layout_marginLeft="16dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</androidx.recyclerview.widget.RecyclerView>
</LinearLayout>
RecyclerView的单视图布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textviewtitle"
android:text="title"
android:textSize="30dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textviewdesc"
android:text="email"
android:textSize="30dp"/>
</LinearLayout>
答案 0 :(得分:1)
这是处理这种情况的更好方法。 Firebase实时数据库无法那样工作。因此,您必须将它们分开(创建新的根/节点)。例如,您可以这样做。
User
-Jim
-username
-password
-others
Project
-Jim
-pro1
-title
-desc
-pro2
-title
-desc
然后,在您的Java代码处更改此行,以与该节点连接
ref = rootNode.getReference("Project").child(Receive_username);
答案 1 :(得分:0)
*while True:
print('Who are you?')
name = input()
name = Joe
if name != 'Joe': #if name equal to 'Joe', print('Hello, Joe. What is the password? (It is a fish.)')
continue #if name not equal to 'Joe', continue executing while-loop
print('Hello, Joe. What is the password? (It is a fish.)')
password = swordfish
if password == 'swordfish': #if password equal to 'swordfish', exit the while-loop
break #if password not equal to 'swordfish', execute the while-loop
print('Access granted.')*