Android / Firebase:如何从用户点击列表视图中检索数据并将其显示在另一个活动上?

时间:2017-09-23 14:39:20

标签: android listview firebase

我创建了一个listview,它显示了我的firebase中所有事件的标题,日期,时间和位置;我还在listview上放了一个onclick监听器。我的问题是,一旦用户点击特定事件,我想向他们显示他们点击的事件的完整信息(标题,时间,日期,位置,描述)。如何显示用户在其他活动中点击的特定事件?顺便说一下,我正在使用firebase作为我的数据库。

我希望你能帮助我。谢谢!

这是我的代码:

_8_ViewEventMember.java

public class _8_ViewEventMember extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

SharedPreferences settings;
List<EventInfo> eventInfos;
DatabaseReference databaseReference;
ListView listViewEvents;
FirebaseAuth auth;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity__8__view_event_member);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setTitle("E(n)venter: Events");


    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
            //    .setAction("Action", null).show();
            Intent i = new Intent(_8_ViewEventMember.this, _11_EventAdd.class);
            startActivity(i);
        }
    });

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    final NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    auth = FirebaseAuth.getInstance();
    String userUid = auth.getCurrentUser().getUid();

    databaseReference = FirebaseDatabase.getInstance().getReference("events_users").child(userUid);
    listViewEvents = (ListView) findViewById(R.id.listViewEvents);
    eventInfos = new ArrayList<>();

    // listViewEvents.setLongClickable(true);

    listViewEvents.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {

            EventInfo eventInfo = eventInfos.get(i);
            showUpdateDeleteDialog(eventInfo.getEventId(), eventInfo.getEventTitle());
            return true;
        }
    });

    listViewEvents.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent i = new Intent(_8_ViewEventMember.this, EventDetails.class);


            //startActivity(new Intent(_8_ViewEventMember.this, EventDetails.class));
        }
    });
}

private void showUpdateDeleteDialog(final String eventId, String eventTitle){
    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
    LayoutInflater inflater = getLayoutInflater();
    final View dialogView = inflater.inflate(R.layout.update_delete_dialog, null);
    dialogBuilder.setView(dialogView);

    final Button buttonUpdate = (Button) dialogView.findViewById(R.id.buttonUpdateEvent);
    final Button buttonDelete = (Button) dialogView.findViewById(R.id.buttonDeleteEvent);

    dialogBuilder.setTitle(eventTitle);
    final AlertDialog b = dialogBuilder.create();
    b.show();

    buttonDelete.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            deleteEvent(eventId);
            b.dismiss();
        }
    });

}

private boolean deleteEvent (String id) {
    //getting the specified artist reference
    auth = FirebaseAuth.getInstance();
    String userUid1 = auth.getCurrentUser().getUid();
    DatabaseReference dR = FirebaseDatabase.getInstance().getReference("events_users").child(userUid1).child(id);
    DatabaseReference dR1 = FirebaseDatabase.getInstance().getReference("events").child(id);

    //removing artist
    dR.removeValue();
    dR1.removeValue();

    Toast.makeText(getApplicationContext(), "Event Deleted", Toast.LENGTH_LONG).show();

    return true;
}

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

    databaseReference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            eventInfos.clear();

            for (DataSnapshot postSnapshot:  dataSnapshot.getChildren()){
                EventInfo eventInfo = postSnapshot.getValue(EventInfo.class);

                eventInfos.add(eventInfo);
            }

            EventInfo_Adapter eventInfoAdapter = new EventInfo_Adapter(_8_ViewEventMember.this, eventInfos);
            listViewEvents.setAdapter(eventInfoAdapter);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }

    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_HOME);
    startActivity(intent);
}

}

1 个答案:

答案 0 :(得分:0)

首先,让你的EventInfo类实现Serializable。

public class EventInfo implements Serializable {
    ...
}

然后,

listViewEvents.setOnItemClickListener(new AdapterView.OnItemClickListener() 
 {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent i = new Intent(_8_ViewEventMember.this, EventDetails.class);
        EventInfo eventInfo = eventInfos.get(i);
        i.putExtra("KEY_EVENT", eventInfo); // passing the clicked event details as intent-extra

        startActivity(i);
    }
});

最后,在EventDetails活动中,您可以收到之前按过的eventInfo信息。

EventInfo eventInfo = (EventInfo) getIntent().getSerializableExtra("KEY_EVENT");