Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

SOLVED: How do I start an intent once a listView item is clicked?

Yash Chowdhary:

I have a Listview which displays ongoing tournaments. My aim is to display the sports that are organized in each tournament (in a separate activity, and also in the form of a listView) once a tournament in the listView is clicked.

I am using firebase. My main concern is how to send the tournamentID of the clicked tournament along with the Intent.

This is what my database looks like:

This is the code:


//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 tournamentNamesList = new ArrayList();
final DatabaseReference tournamentRef = mDatabase.child("Tournaments");

final HashMap 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) {

}
});`

I read that the putExtra() method will send over data through the intent, but I'm still not sure how to retrieve the tournament ID of the clicked textView. I'm not too sure what the 'id' parameter in the onItemClick() method holds.

Any help would be appreciated greatly!

Thanks in advance!



Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots
This Question have been answered
HERE


This post first appeared on Stack Solved, please read the originial post: here

Share the post

SOLVED: How do I start an intent once a listView item is clicked?

×

Subscribe to Stack Solved

Get updates delivered right to your inbox!

Thank you for your subscription

×