Home > Back-end >  Android ListView with OnClick to display information with the same format
Android ListView with OnClick to display information with the same format

Time:05-05

I'm new to android studio and developing apps.

I currently have a list , in my case of people of type ViewList.

I want to make the app display information of these people whenever the user clicks on him.

So for example, if a user clicks Elon Musk, the app will take the user to a page that will display information about Elon musk. The same with other people.

How can I program this without needing to re create a new activity for every person on the list?

TIA

CodePudding user response:

To do this you would need to pass the data through to another activity with an intent, and changing the data in a activity(e.g. displayPerson.class)

Now there is multiple ways of doing this but generally is close to the same concept.

note: Where is the data coming from, is it from firebase or is it hard coded into the app?

CodePudding user response:

You can create one activity and show detailed information about the clicked user in that activity.

You don't need to create activity for each user. Just one activity is enough.
user.setOnClickListener(v -> {
    Intent intent = new Intent(CurrentClass.this, UserDeatilClass.class);
    intent.putExtra("userName", userName);
    intent.putExtra("userAge", userAge);
    intent.putExtra("userEmail", userEmail);
    startActivity(intent);
});

In the user Detail class

protected void onCreate(...) {
     userNameTV.setText(getIntent().getStringExtra("userName"));
     userageTV.setText(getIntent().getStringExtra("userage"));
     userEmailTV.setText(getIntent().getStringExtra("userEmail"));
}

Also, if you are using Database you can just send only the ID number of the user, and fetch all data about the user from database, finally you can display the whole information about that user from database.
But if you don't want to create an activity, you can create a custom Alert Dialog and display the detailed information of the user in the Alert Dialog.

  • Related