当您创建一个具有主要细节主题的新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;
}
}
}
但是我不知道如何将数据库提供的数据从静态更改为动态。有人可以给我一些指示吗?
最佳答案
您可以使用此方法添加内容:
/**
* 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();