单击listView项后如何启动意图?

时间:2017-06-12 15:44:39

标签: android listview firebase android-intent firebase-realtime-database

我有一个listView,显示正在进行的锦标赛。我的目的是在点击listView中的锦标赛时显示每个锦标赛中组织的运动(在单独的活动中,也以listView的形式)。

我正在使用firebase。我主要担心的是如何发送点击锦标赛的锦标赛ID以及意图。

这就是我的数据库:

enter image description here

这是代码:

   //tournament_list view to be populated with tournament data
    final ListView tournamentListView = (ListView) findViewById(R.id.tournament_list);

    //finding and setting the empty view in the listView when the tournament_list has 0 items
    View emptyView = findViewById(R.id.empty_view);
    tournamentListView.setEmptyView(emptyView);

    final ArrayList<String> tournamentNamesList = new ArrayList<String>();
    final DatabaseReference tournamentRef = mDatabase.child("Tournaments");

    final HashMap<String,String> nameIdMatcher = new HashMap<>();

    tournamentRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot tournament : dataSnapshot.getChildren()) {
                TournamentInformation tournamentInformation = new TournamentInformation();
                tournamentInformation.setTournamentName(tournament.getValue(TournamentInformation.class).getTournamentName());
                tournamentNamesList.add(tournamentInformation.getTournamentName());

                nameIdMatcher.put(tournamentInformation.getTournamentName(),tournament.getKey());
                Log.d("Tournament Id:",tournament.getKey());
                Log.d("Main Activity", tournamentInformation.getTournamentName());
            }

            StringAdapter stringAdapter = new StringAdapter(MainActivity.this,tournamentNamesList);
            tournamentListView.setAdapter(stringAdapter);

            tournamentListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Intent intent = new Intent(MainActivity.this,SportsListActivity.class);
                    String tournamentName = tournamentNamesList.get(position);
                    tournamentId = nameIdMatcher.get(tournamentName);
                    intent.putExtra("tournamentId",tournamentId);
                    startActivity(intent);
                }
            });
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });`

我读到putExtra()方法将通过intent发送数据,但我仍然不确定如何检索点击的textView的锦标赛ID。我不太确定&#39; id&#39; onItemClick()方法中的参数成立。

任何帮助都将受到极大的赞赏!

提前致谢!

1 个答案:

答案 0 :(得分:0)

使用此选项从listview中获取锦标赛名称,然后单击

tournamentListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

String tournamentName = tournamentNamesList.get(position);