如何在android master/detail活动中修改虚拟内容? [英] How to modify dummy content in android master/detail activity?

查看:116
本文介绍了如何在android master/detail活动中修改虚拟内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将在主/详细模式中修改由eclipse生成的项目.我找不到执行此操作的方法.特别是,我将从xml(res/values/arrays)资源文件中获取项目.

I would modify the items generated by eclipse in the master/detail schema. I can't find a way to do this. In particular I would take the items from an xml (res/values/arrays) resource file.

这是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);
    }
}

推荐答案

可能的解决方案:

由您自己替换静态类.就我而言, DummyItem 变为 ProfileItem 并具有不同的属性,并且 DummyContent 变为 ProfileListContent .

Replace the static class by your own. In my case, DummyItem becomes ProfileItem and has different attributes and DummyContent becomes ProfileListContent.

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

Then replace the static block static { addItem ... } by a static method. In the following case, I need to load items from a database :

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

我在 onCreate 方法开始时从我的主要活动中调用方法 setContext ,在进行任何其他操作之前.

I call the method setContext from my main activity at the beginning of the onCreate method, before any other operation.

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项的布局,我提供了一个示例这里.

You can of course change the ListView Items' layout, I provided an example here.

这篇关于如何在android master/detail活动中修改虚拟内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆