Search code examples
androidlistviewlistactivity

Drug dictionary android


I want to make this type of app

  • list item
  • filter search
  • list activity

I have problem at list activity. I don't know how to start a new activity when a list item is clicked. I can do the same activity for all list items, but I cannot choose a particular activity for each item.

For example, I have list items with drug names, such as "Paracetamol", "Digoxin", "Adrenaline", and so on. When I click on "Paracetamol", I want to start a new activity for parcetamol, and when I click on "Digoxin", I want to start a new activity for this drug. I want to make this type of app.


Solution

  • When launching the detail activity you can receive information with the intent so that you are able to display the item accordingly.

    from the list activity :

    Intent intent = new Intent(getApplicationContext(), DetailActivity.class);
    intent.putExtra("OBJ_ID", drugObj.id);
    startActivity(intent);
    

    from the detail activity in the onCreate method :

    int drugID = getIntent().getIntExtra("OBJ_ID", 0);
    

    Please note the above is an example based on a drug object that has its unique id an integer

    After you get the ID from the intent you are good to go..loop through your drug objects and display data accordingly.

    Hope it helps