views:

13

answers:

1

hi,

I'm passing data to a ListView to display some restaurant names. Now when clicking on an item I'd like to start another activity to display more restaurant data. I'm not sure about how to do it. Shall I pass all the restaurant data in a bundle through the intent object? Or shall I just pass the restaurant id and get the data in the other activity? In that case, how can I access my restaurantList from the other activity? In any case, how can I get data from the restaurant I clicked on (the view only contains the name)?

Any help, pointers welcome!

Thanks

Jul

ListView lv= (ListView)findViewById(R.id.listview);

lv.setAdapter( new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,restaurantList.getRestaurantNames()));

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

        Intent i = new Intent(Atable.this, RestaurantEdit.class);
        Bundle b = new Bundle();
        //b.putInt("id", ? );
        startActivityForResult(i, ACTIVITY_EDIT);
    }
});

RestaurantList.java

package org.digitalfarm.atable;
import java.util.ArrayList;
import java.util.List;

public class RestaurantList {

    private List<Restaurant> restaurants = new ArrayList<Restaurant>();

    public List<Restaurant> getRestaurants() {
        return this.restaurants;
    }

    public void setRestaurants(List<Restaurant> restaurants) {
        this.restaurants = restaurants;
    }

    public List<String> getRestaurantNames() {      

        List<String> restaurantNames = new ArrayList<String>();

        for (int i=0; i<this.restaurants.size(); i++) {
            restaurantNames.add(this.restaurants.get(i).getName());
        }

        return restaurantNames;
    }
}

Restaurant.java

package org.digitalfarm.atable;

public class Restaurant {

    private int id;
    private String name;        
    private float latitude;
    private float longitude;

    public int getId() {
        return this.id; 
    }

    public void setId(int id) {
        this.id = id;
    }       

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

    public void setName(String name) {
        this.name = name;
    }

    public float getLatitude() {
        return this.latitude;
    }

    public void setLatitude(float latitude) {
        this.latitude = latitude;
    }

    public float getLongitude() {
        return this.longitude;
    }

    public void setLongitude(float longitude) {
        this.longitude = longitude;
    }
}
+1  A: 

You can not access data from other activities. If you need data in the activity that is started through clicking on the item in the list pass it to the new activity through the message bundle.

If you pass only the id to the next activity you could reload the restaurant from a database or from the internet but you can not retrieve it from the list used in the first activity.

If the restaurants are heavy to create objects you could implement you own application subclass and attach the restaurant list to this application subclass. Now you can access the list like this:

shopList = (YourSubclass)getApplication().getRestaurantList()

This would result in your shoplist being in the memory the wohl runtime of you application even if the app is in the background and all activities are paused.

Janusz