可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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); } }
回答1:
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.
回答2:
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.
回答3:
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