2

当您使用主详细信息主题创建一个新的 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;
        }
    }
}

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

4

1 回答 1

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();
于 2013-09-17T13:21:04.337 回答