Android功能在MainActivity OnCreate中被调用了两次

时间:2015-05-21 18:18:41

标签: android database android-recyclerview

我在OnCreate中有一个initDatabase()函数,它打开一个数据库并将数据插入数据库,该数据库用于主活动布局中的Recyclerview的适配器。但我不明白为什么在onCreate中被调用两次。有人可以给我一些建议吗?很多人都很欣赏。

Protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    initDatabase();
    setContentView(R.layout.activity_main);
    rv= (RecyclerView) findViewById(R.id.recyclerView);
    cursor=mdb.getCursor();
    adapter=new MyAdapter(cursor,this, 1);
    lm=new LinearLayoutManager(this);
    rv.setLayoutManager(lm);
    rv.setAdapter(adapter);
}

public void initDatabase(){
    Log.e("Main activity", "initDatabase");
    mdb=new MyDatabase(this);
    mdb.addCategory("House");
    mdb.addCategory("Car");
    mdb.addCategory("Life Insurance");
    mdb.addCategory("Banks");
    mdb.addCategory("Investments");
    mdb.addCategory("Kid");
    mdb.addCategory("Mis");

    mdb.addItem("House","Mortgage");
    mdb.addItem("House","HOA");
    mdb.addItem("House","Property Tax");

    mdb.addItem("Car","Registration");
    mdb.addItem("Car","Insurance");
    mdb.addItem("Car","Maintenence");

    Event e=new Event();
    Calendar date = Calendar.getInstance();
    date.set(2015, 5, 3);

    e.category="House";
    e.item="HOA";
    e.date=date;
    e.content="do something";

    mdb.addEvent(e);

    Event e1=new Event();
    Calendar date1 = Calendar.getInstance();
    date.set(2015, 5, 4);

    e1.category="House";
    e1.item="HOA";
    e1.date=date;
    e1.content="do something";

    mdb.addEvent(e1);





    cursor=mdb.getCursor();
}

0 个答案:

没有答案