如何将主要细节主题从静态数据更改为动态数据?

时间:2013-09-06 14:38:58

标签: java android

当你使用主细节主题创建一个新的android项目时,你会得到一个带有虚拟静态数据的DummyContent类:

package com.test.masterdetail.dummy;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Helper class for providing sample content for user interfaces created by
 * Android template wizards.
 * <p>
 * TODO: Replace all uses of this class before publishing your app.
 */
public class DummyContent {

    /**
     * An array of sample (dummy) items.
     */
    public static List<DummyItem> ITEMS = new ArrayList<DummyItem>();

    /**
     * A map of sample (dummy) items, by ID.
     */
    public static Map<String, DummyItem> ITEM_MAP = new HashMap<String, DummyItem>();

    static {
        // Add 3 sample items.
        addItem(new DummyItem("1", "Item 1", "http://www.yahoo.com"));
        addItem(new DummyItem("2", "Item 2", "http://www.fb.com"));
        addItem(new DummyItem("3", "Item 3", "http://www.google.com"));
    }

    private static void addItem(DummyItem item) {
        ITEMS.add(item);
        ITEM_MAP.put(item.id, item);
    }

    /**
     * A dummy item representing a piece of content.
     */
    public static class DummyItem {
        public String id;
        public String content;
        public String url;

        public DummyItem(String id, String content, String url) {
            this.id = id;
            this.content = content;
            this.url = url;
        }

        @Override
        public String toString() {
            return content;
        }
    }
}

但我不知道如何将这些数据从静态更改为动态,我将从数据库中提供。任何人都可以给我一些指示?

1 个答案:

答案 0 :(得分:0)

您可以使用此方法添加内容:

/**
 * call the method setContext from my main activity at the beginning of the onCreate method, before any other operation.
 */
public static void setContext() {
    addItem(new DummyItem(var1, var2, var3,...));
}

但请记得调用notifyDataSetChanged:

// Update the list of the fragment
((YourListFragment) getSupportFragmentManager().findFragmentById(R.id.your_fragment_id)).notifyDataSetChanged();
电话结束后

DummyContent.setContext();