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

前端 未结 3 694
春和景丽
春和景丽 2021-01-02 21:23

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/array

相关标签:
3条回答
  • 2021-01-02 21:55

    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

    0 讨论(0)
  • 2021-01-02 22:10

    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.

    0 讨论(0)
  • 2021-01-02 22:20

    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.

    0 讨论(0)
提交回复
热议问题