如何在fireview中显示listview中的数据(包含与用户电子邮件相同的电子邮件的块)

问题描述 投票:0回答:1

我想在ListView中显示使用他的邮件登录存储在firebase中的用户的数据。例如登录用户的邮件是123@mail.com,ListView显示块块中的所有项目表示存储在firebase中的用户或项目的完整详细信息。例如,有一本书,firebase包含它的书名,内容,描述,doi,作者姓名等。我想要的是,如果作者只登录他的书将显示书名,描述,内容和doi(书籍参考只是一个例子)。我的问题是我的列表视图显示所有关于工作的项目,包括属于其他用户的项目,我希望用户只需编辑自己发布的工作。那么我应该写什么firebase查询来实现这一点,这是我的代码。

火力

Firebase

java类

public class WorkActivity extends AppCompatActivity {

    ListView lv;
    Button ShowButton;
    // Define a String ArrayList for the job
    private ArrayList<String> jobdes=new ArrayList<>();

    // Define an ArrayAdapter for the list
    private ArrayAdapter<String> arrayAdapter;

    // Declaring String variable ( In which we are storing firebase serverURL).
    public static final String Firebase_Server_URL = "https://jobaps70.firebaseio.com/";
    public static final String Firebase_Server_URL1 = "https://jobaps70.firebaseio.com/jobs";

    // Declaring String variables to store name & phone number get from EditText.

    String PriceHolder, JobDescriptionHolder;
    FirebaseHelper helper;

    // Declaring Firebase object.
    Firebase firebase,firebase1;

    // Creating FirebaseAuth.
    FirebaseAuth firebaseAuth ;

    // Creating FirebaseAuth.
    FirebaseUser firebaseUser;

    // Creating Boolean variable that holds EditText is empty or not status.
    Boolean EditTextStatus ;


    TextView ShowDataTextView ;


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

      //        Actionbar

    getSupportActionBar().setDisplayShowHomeEnabled(true);
    getSupportActionBar().setIcon(R.drawable.logo);
    getSupportActionBar().setBackgroundDrawable(new 
    ColorDrawable(getResources().getColor(R.color.Snow)));

    ShowButton = (Button)findViewById(R.id.show);

    // Adding MainActivity context into Firebase context.
    Firebase.setAndroidContext(WorkActivity.this);

    // Passing firebase Server URL into firebase object.
    firebase = new Firebase(Firebase_Server_URL);
    firebase1=new Firebase(Firebase_Server_URL1);

    firebaseAuth = FirebaseAuth.getInstance();

    // On activity start check whether there is user previously logged in or not.
    if(firebaseAuth.getCurrentUser() == null){

        // Finishing current Profile activity.
        finish();

        // If user already not log in then Redirect to LoginActivity .
        Intent intent = new Intent(WorkActivity.this, LoginActivity.class);
        startActivity(intent);

        // Showing toast message.
        Toast.makeText(WorkActivity.this, "Please Log in to continue", 
        Toast.LENGTH_LONG).show();

    }

    // Adding firebaseAuth current user info into firebaseUser object.
    firebaseUser = firebaseAuth.getCurrentUser();

    // Getting logged in user email from firebaseUser.getEmail() method and set into TextView.
    setTitle( firebaseUser.getEmail());
    setTitleColor(R.color.DarkGray);
    firebase.child("jobs");

    // Associate the jobs' list with the corresponding ListView
    lv = (ListView) findViewById(R.id.lvshow);

    // Set the ArrayAdapter to the ListView
    arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, jobdes);
    lv.setAdapter(arrayAdapter);

    // Attach a ChildEventListener to the teacher database, so we can retrieve the job entries
    firebase.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {

            // Get the value from the DataSnapshot and add it to the jobs' list
            JobDetails jo = (JobDetails) dataSnapshot.getValue(JobDetails.class);
            String joString = String.valueOf(jo);
            arrayAdapter.add(joString);
 //

            // Notify the ArrayAdapter that there was a change
            arrayAdapter.notifyDataSetChanged();
        }

        @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(FirebaseError databaseError) {

        }
    });
}

}

的JobDetail

 public class JobDetails {

    private String price;
    private String jobDescription;
    private String jobSpinner;
    public String email;
    public JobDetails() {
        // This is default constructor.
    }

    public String getPrice() {

        return price;
    }

    public void setPrice(String pri) {

        this.price = pri;
    }

    public String getjobDescription() {

        return jobDescription;

    }

    public void setjobDescription(String jobd) {

        this.jobDescription = jobd;
    }

    public void setjob(String jry) {

        this.jobSpinner = jry ;

    }

    public String getjob() {

        return jobSpinner;

    }

    public void setemail(String jel) {

        this.email = jel ;

    }

    public String getemail() {

        return email;

    }
   @Override
   public String toString() {
    return this.jobSpinner + ": " + this.jobDescription+ ": " +price;
}

}

XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.gumption.zeeshanahmed.jobaps.WorkActivity">
<TextView
    android:id="@+id/textdispjobs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/text_display_jobs"
    android:textSize="40sp"
    android:textAlignment="center"/>

<ListView
    android:id="@+id/lvshow"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentStart="true"
    android:layout_below="@+id/show" />


</RelativeLayout>

目前它还显示来自其他用户的项目。如果有人可以帮助我,这将是非常有帮助的。谢谢!

android listview firebase firebase-realtime-database firebase-authentication
1个回答
0
投票

如果您在用户首次创建帐户时设置了电子邮件,则仅使用带Firebase的getEmail()函数返回电子邮件。要设置他们的电子邮件,您必须这样做:

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
user.updateEmail("user@example.com");

如果你想要更多,this is where I got this code from

然后,您可以使用getEmail函数。否则,如果您想从JobDetails类获取电子邮件,请在childEventListener中获取电子邮件:

JobDetails jo = (JobDetails) dataSnapshot.getValue(JobDetails.class);
//This getEmail function is provided in the JobDetails class
String email = jo.getEmail();

编辑:

由于您只想获得当前用户显示/添加的作业,因此您需要先获取用户的电子邮件。假设您根据Uid在数据库中设置用户信息,您可以执行以下操作以获取当前用户:

private User curr_user;
private String mUserId;

mUserId = FirebaseAuth.getInstance.getUid();
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();


//Replace this path to wherever you stored your users
mDatabase.child("users").child(mUserId).addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            curr_user = dataSnapshot.getValue(User.class);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

现在,您可以遍历作业的子项以获取当前用户的电子邮件发布的作业:

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

        // Get the value from the DataSnapshot and add it to the jobs' list
        JobDetails jo = (JobDetails) dataSnapshot.getValue(JobDetails.class);
        String joString = String.valueOf(jo);

        if (jo.getEmail() == curr_user.getEmail()) {
            arrayAdapter.add(joString);
        }

        // Notify the ArrayAdapter that there was a change
        arrayAdapter.notifyDataSetChanged();
    }

    @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(FirebaseError databaseError) {

    }
});
热门问题
推荐问题
最新问题