Firebase数据库列表视图打开另一项活动

时间:2017-07-21 16:03:02

标签: android listview firebase firebase-realtime-database

我正在使用firebase数据库,我手动保存数据并在应用程序中检索数据并且运行良好但我希望当用户单击必须打开另一个活动的listview项时,必须检索另一个表。

以下是我的代码

  private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;

 private ProgressDialog progressDialog;

DatabaseReference mref = 
FirebaseDatabase.getInstance().getReference("users");
ListView mlistview;
ArrayList<String> arrayList=new ArrayList<>();
ArrayAdapter<String> arrayAdapter;

private BottomNavigationView.OnNavigationItemSelectedListener 
 mOnNavigationItemSelectedListener
        = new BottomNavigationView.OnNavigationItemSelectedListener() {

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.navigation_home:
progressDialog.show();
                startActivity(new 
Intent(profilemain.this,profilemain.class));
                return true;
            case R.id.navigation_dashboard:
                Toast.makeText(profilemain.this,"hi 
hello",Toast.LENGTH_SHORT).show();
                return true;
            case R.id.navigation_notifications:
                Toast.makeText(profilemain.this,"hi 
hello",Toast.LENGTH_SHORT).show();
                return true;
            case R.id.navigation_signout:

                Toast.makeText(profilemain.this,"You have successfully 
Signed out",Toast.LENGTH_SHORT).show();
                mAuth.signOut();
                return true;
        }
        return false;
    }

};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_profilemain);
   progressDialog.setMessage("loading");
    progressDialog.setTitle("database is");

    mlistview=(ListView) findViewById(R.id.listview);

    Firebase.setAndroidContext(this);

    DatabaseReference ref = FirebaseDatabase.getInstance()
            .getReference("users");
    //mref=new Firebase("https://stark-1dffd.firebaseio.com/users");
    arrayAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,arrayList);
    mlistview.setAdapter(arrayAdapter);


    mref.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            String value= dataSnapshot.getValue(String.class);

            arrayList.add(value);
            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(DatabaseError databaseError) {

        }
    });


    BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
    navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
    mAuth = FirebaseAuth.getInstance();


    mAuthListener = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            if (firebaseAuth.getCurrentUser()==null){
                startActivity(new
                        Intent(profilemain.this,MainActivity.class));

            }
        }
    };



}

@Override
protected void onStart() {
    super.onStart();

    mAuth.addAuthStateListener(mAuthListener);
}

1 个答案:

答案 0 :(得分:0)

ItemClickListener

上添加ListView
 mlistview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent in = new Intent(MyActivity.this, DetailActivity.class);
                in.putExtra("data", arrayList.get(position);// add the selected String from the ListView
                startActivity(in);
            }
        });

并从其他活动中读取数据:

String data = getIntent().getStringExtra("data");