-1

This is my first question in stack overflow .

I am creating one project with spinner , When dropdown list is clicked it displays 25 states , If we click any one state it takes us to new activity with LISTVIEW of hotels in that city , If we click any hotel it shows the details .

For this I am creating 2to3 activity , IF my project is small its ok , but i am adding 25 States and each states consists of 10-15 Hotels . It takes too much of time to create it ..

I use New --> Other --> Android --> Android Activity ,

My question is :- Can I create activity with contents (Image , ImageButton & details) using Java code ?

  • 1
    I'm not sure I understand the question correctly. Do you mean the load time takes too long or it takes too long to create an app that does this? – Chackle Mar 10 '15 at 12:31
  • 1
    you do not need to create activities for each of them, just get your data from source (say db) and fill your activity controls accordingly – karan Mar 10 '15 at 12:34
  • You don't need multiple activities for that. Change your design approach to reuse one activity/fragment :) – waqaslam Mar 10 '15 at 12:37
  • @Chackle It takes too long to create an app – Jai Ganesh Mar 10 '15 at 12:41
  • @KaranMer Can you provide some links how to work on it ? if so it will be helpful – Jai Ganesh Mar 10 '15 at 12:42
  • Why you creating a whole different activity for each hotel? Just create one and set your data according to your clicked Hotel item. – Oğuzhan Döngül Mar 10 '15 at 12:42
  • you could find commonsware book for android tutorials there is similar restaurant project for learning in it, i dont remember correctly but the edition was for android 3.1 – karan Mar 10 '15 at 12:44

2 Answers2

2

Do not create so many activities for each city and then for each hotel.
You can create only one activity to show city hotels just by passing that city name in EXTRAS in "intent" and show hotels of that city only in new activity.
To send name of city in intent use

        Intent intent = new Intent(MAIN_ACTIVITY.this, HOTELS_ACTIVITY.class);
        intent.putExtra("city", city_name);
        startActivity(intent);

After that in new activity use

        String passedCity = getIntent().getExtras().getString("city");

Now you have city name and you can retrieve hotels name of that city accordingly.
In this way you don't have to create so many activities.
Similarly you can use this to show hotel details by passing just its name and retrieving it in next activity.
Good Luck

Chackle
  • 2,249
  • 18
  • 34
Kunal
  • 432
  • 1
  • 4
  • 14
0

You should be reusing activities rather than building from scratch each time.

Your data model could possibly look like this:

public class State
{
    private String name;
    private ArrayList<Hotel> hotels;

    public State(String name, ArrayList<Hotel> hotels)
    {
        this.name = name;
        this.hotels = hotels;
    }

    public ArrayList<Hotel> getHotels()
    {
        return this.hotels;
    }

    public String getName()
    {
        return this.name;
    }
}

public class Hotel
{
    private String name;
    // Add in your other variables here
    // Examples: Location, Id, Address

    public Hotel(String name)
    {
        this.name = name;
    }
}

Build your data however you please, but you should use objects similar to these to pass through to your 2/3 activities and build the user interface dynamically.

Chackle
  • 2,249
  • 18
  • 34