Re: [問題] 請問如何在另一個class中使用alertdialog

看板AndroidDev作者 (.。oO)時間11年前 (2013/05/31 13:47), 編輯推噓0(0015)
留言15則, 3人參與, 最新討論串3/3 (看更多)
※ 引述《laugher (.。oO)》之銘言: : public void showItemCategory(Context ct) { : String[] stra_item_category = : getResources().getStringArray(R.array.stra_item_category); : AlertDialog.Builder dlg_item_category = new AlertDialog.Builder(ct); : dlg_item_category.setTitle(getString(R.string.str_dlg_item_category_title)); : dlg_item_category.setNeutralButton(getString(R.string.str_cancel), null); : dlg_item_category.setItems(stra_item_category, : new DialogInterface.OnClickListener() { : public void onClick(DialogInterface dialog, int index) { : showItemList(index); : } : } : ); : dlg_item_category.show(); : } 我找到問題了 上面的函式裡的getResources或是getString等會讀取xml資源的都要改成 ct.getResources或ct.getString 因為目前的activity仍是RoamingMap 感謝版友的幫忙 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.116.72.155

05/31 14:15, , 1F
我比較好奇你的c2是class要怎麼找到getString...
05/31 14:15, 1F

05/31 14:15, , 2F
應該會直接報錯誤吧
05/31 14:15, 2F

05/31 14:16, , 3F
還是你的c2有繼承什麼東西?
05/31 14:16, 3F

05/31 14:23, , 4F
c2我繼承Activity
05/31 14:23, 4F

05/31 14:59, , 5F
你的寫法一整個怪,Activity用intent不是用new吧
05/31 14:59, 5F

05/31 15:00, , 6F
你要在RoamingMap顯示alert應該code寫在RoamingMap吧
05/31 15:00, 6F

05/31 15:00, , 7F
我一開始還以為你寫在一個class方便其他activity使用
05/31 15:00, 7F

05/31 15:31, , 8F
我分成另一個class是因為我想把他弄成函式庫的樣子
05/31 15:31, 8F

05/31 15:31, , 9F
比如管理"item"的所有功能都放在同一個class裡
05/31 15:31, 9F

05/31 15:41, , 10F
那你就不該extends任何東西 單純都做class使用就好
05/31 15:41, 10F

05/31 15:43, , 11F
但那些功能仍是會需要像AlertDialog的能力啊
05/31 15:43, 11F

05/31 16:11, , 12F
這...你的程式觀念似乎問題滿大的
05/31 16:11, 12F

05/31 19:04, , 13F
若你方便的話,可否請教你上述那些的論點跟原因何在?
05/31 19:04, 13F

06/02 19:16, , 14F
要用dialog的activity傳context參數進去、傳dialog回來就
06/02 19:16, 14F

06/02 19:16, , 15F
好,工具類別繼承activity意義上不對。
06/02 19:16, 15F
文章代碼(AID): #1Hg3c9P3 (AndroidDev)
討論串 (同標題文章)
文章代碼(AID): #1Hg3c9P3 (AndroidDev)