6

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);
    }
}
user1624267
  • 61
  • 1
  • 1
  • 2

3 Answers3

6

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.

Community
  • 1
  • 1
Solostaran14
  • 1,656
  • 20
  • 22
  • Since this gets generated every time you make a list or master/detail activity, a shortcut I use is: click on packages and class files and use `shift + F6` to refactor the names then I open the files and just replace `ctl + R` the DummyContent and DummyItems with whatever I want and begin from there. – mkrinblk Apr 14 '17 at 01:01
  • Does the list of items remain static in your database backed implementation? – Charles Oct 27 '18 at 11:38
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.

Seïfane Idouchach
  • 620
  • 2
  • 5
  • 21
0

This is something that worked for me, not sure if it is the best practice. Remove the static block and on the ItemListActivity.java on the Oncreate add this code

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

Hope this helps

Ivan Ferić
  • 4,725
  • 11
  • 37
  • 47
Mario
  • 1