想請問如何在定義actionbar時使用sqlite

作者: c310759 (撥撥~)   2015-06-05 23:52:56
我在menu_main中使用actionProviderClass去設定actionbar的內容,
(android:actionProviderClass="com.kermitlin.imha.FilterActionProvider")
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<group android:id="@+id/gp2">
<item
android:id="@+id/tb_InstaChat"
android:icon="@drawable/ic_autorenew_white_24dp"
android:actionProviderClass="com.kermitlin.imha.FilterActionProvider"
android:orderInCategory="2"
android:showAsAction="ifRoom"
android:title="tt2" />
</group>
</menu>
以下是FilterActionProvider
public class FilterActionProvider extends ActionProvider {
public FilterActionProvider(Context context) {
super(context);
}
@Override
public View onCreateActionView() {
return null;
}
@Override
public void onPrepareSubMenu(SubMenu subMenu) {
subMenu.clear();
DBOperator db;
db = new DBOperator(this.getApplication().getPackageName(), this);
db.open(); //我在這邊就會錯誤,無法使用getApplication()
subMenu.add("Reminders").setIcon(R.drawable.ic_launcher)
.setOnMenuItemClickListener(new
MenuItem.OnMenuItemClickListener()
{
@Override
public boolean onMenuItemClick(MenuItem item) {
return true;
}
});
}
@Override
public boolean hasSubMenu() {
return true;
}
}
我想要請問應該如何在MyactionProvider中使用sqlite,
或是說應該要如何解決getApplication()的問題,
抱歉我是初學者如果問題太愚蠢請多包涵,謝謝<(_ _)>
作者: KeySabre (KeySabreur)   2015-06-06 00:07:00
從你建構子由外部傳入的context拿
作者: y3k (激流を制するは静水)   2015-06-06 22:16:00
Activity鏡射成Context丟給Class

Links booklink

Contact Us: admin [ a t ] ucptt.com