Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to modify dummy content in android master/detail activity?

Tags:

android

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.

this is the java file:

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);
    }
}
like image 945
user1624267 Avatar asked Aug 25 '12 08:08

user1624267


1 Answers

A possible solution :

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

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

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);
    ...

If you want to dynamically add an item :

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

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

like image 120
Solostaran14 Avatar answered Nov 10 '22 00:11

Solostaran14