6

我会在主/详细模式中修改 eclipse 生成的项目。我找不到办法做到这一点。特别是我会从 xml (res/values/arrays) 资源文件中获取项目。

这是java文件:

package com.ga.termeapp.dummy;

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


public class DummyContent {

    public static class DummyItem {

        public String id;
        public String content;

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

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

    public static List<DummyItem> ITEMS = new ArrayList<DummyItem>();
    public static Map<String, DummyItem> ITEM_MAP = new HashMap<String, DummyItem>();

    static {

        addItem(new DummyItem("1", "Le terme"));
        addItem(new DummyItem("2", "Le cure termali"));
        addItem(new DummyItem("3", ""));
    }

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

3 回答 3

6

一个可能的解决方案:

用你自己的替换静态类。在我的情况下,DummyItem成为ProfileItem并具有不同的属性和DummyContent成为ProfileListContent

然后用static { addItem ... }静态方法替换静态块。在以下情况下,我需要从数据库中加载项目:

public static void setContext(Context c) {
    if (db == null) db = new MyDbAdapter(c); // SQLiteOpenHelper + SQLiteDatabase manager
    if (db.isOpen() == false) {
        db.open();
        Cursor c = db.getProfiles(); // database query
        if (c.moveToFirst()) {
            do {
                ProfileItem item = new ProfileItem(c.getString(0), c.getString(1),
                    c.getString(2));
                addItem(item);
            } while (c.moveToNext());
        }
    }
}

setContext我在方法开始时从我的主要活动中调用该onCreate方法,然后再进行任何其他操作。

public void onCreate(Bundle savedInstanceState) {
    ProfileListContent.setContext(this);
    ...

如果要动态添加项目:

public static void insertProfile(ProfileItem profile) {
    db.insertProfile(profile); // add item to the database
    addItem(profile); // the same addItem provided with the eclipse wizard
}

您当然可以更改 ListView 项目的布局,我在这里提供了一个示例

于 2012-10-18T14:22:59.143 回答
0

I had the same problem but insted of taking all the values from an array I changed

addItem(new DummyItem("1", "Le terme"));
addItem(new DummyItem("2", "Le cure termali"));
addItem(new DummyItem("3", ""));

I edited those addItem and then if you want to add string to your items, you have to edit all this part of code.

    public String id;
    public String content;

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

Hope it will help you.

于 2012-08-29T09:59:18.167 回答
0

这对我有用,不确定这是否是最佳实践。删除静态块并在ItemListActivity.javaOncreate添加此代码

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    if (DummyContent.ITEMS.isEmpty())
    {
        DummyContent.addItem(new DummyItem("1", getResources().getString(R.string.menu1)));
        DummyContent.addItem(new DummyItem("2", getResources().getString(R.string.menu2)));
        DummyContent.addItem(new DummyItem("3", getResources().getString(R.string.menu3)));
    }

希望这可以帮助

于 2013-02-06T14:31:16.020 回答