简体   繁体   English

如何通过Android中的任何活动访问未存储在数据库中的数据?

[英]How can data not stored in a DB be accessed from any activity in Android?

I'm passing data to a ListView to display some restaurant names. 我正在将数据传递到ListView以显示一些餐厅名称。 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? 还是我只需要传递餐厅ID并在其他活动中获取数据? In that case, how can I access my restaurantList from the other activity? 在这种情况下,如何从其他活动访问我的restaurantList? In any case, how can I get data from the restaurant I clicked on (the view only contains the name)? 无论如何,如何从单击的餐厅中获取数据(视图仅包含名称)?

Any help, pointers welcome! 有任何帮助,欢迎指点!

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 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 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;
    }
}

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. 如果仅将ID传递给下一个活动,则可以从数据库或Internet重新加载餐厅,但无法从第一个活动中使用的列表中检索它。

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() 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. 即使该应用程序在后台并且所有活动都已暂停,这也会导致您的购物清单在应用程序的wohl运行时内存中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用于存储数据的变量,可以由任何类访问 - Variable to store data, that can be accessed by any class 如何从适配器获取数据并在Android中的Activity中显示 - how can i get data from adapter and show in Activity in Android 如何从Android Activity中提取API和数据库调用? - How to extract API and DB calls from Android Activity? 如何存储/访问数据并防止地图中的竞争条件,java - How data is stored/accessed and preventing race conditions in maps, java 如何将数据从 BroadcastReceiver 传递到 Android 中的活动 - How to Pass Data From a BroadcastReceiver to an Activity in Android 如何将类中的数据发送到Android中的Activity - How to send data from a class to an Activity in Android 如何将数据从活动传递到Android上的另一个活动 - How to pass data from activity to another activity on Android 如何让小吃店可以从安卓中的任何活动中访问? - how to make a snackbar accessible from any activity in android? 我正在尝试从简单的listView中的.db文件导入数据,但我的应用程序未显示任何文本。如何解决这个问题? - I am trying to import data from a .db file in a simple listView but my application is not displaying any text.How can i resolve this? 如何在我的活动中获取 viewpager 数据 android java - How can I get viewpager data in my activity android java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM