[英]How to make text from EditText in AlertDialog available in MainActivity class?
[英]Make UI changes in MainActivity from helper class
我正在嘗試在我的android應用中添加應用帳單。 當前,我被困在如何回叫我的MainActivity上,以在發現用戶以前購買過東西后從助手計費類執行UI更改。
我已經搜索了,但是找不到我想要的東西,我確定我需要實現回調或監聽器,或者兩者都實現?
我的代碼如下:
主要活動
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
MyBilling bill;
private Menu nav_Menu;
@Override
protected void onCreate(Bundle savedInstanceState) {
bill = new MyBilling(this);
bill.onCreate();
}
private void displaySelectedScreen(int itemId) {
///calls fragment and inflates
}
public void UpdateUI(){
//Make some changes to UI
nav_Menu.findItem(R.id.remove_ad_button).setVisible(false);
//Recall fragment
displaySelectedScreen(R.id.distance_check);
}
}
結算助手類(Google In App結算)
public class MyBilling {
Activity activity;
public MyBilling(Activity launcher) {
this.activity = launcher;
}
IabHelper.QueryInventoryFinishedListener mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener() {
public void onQueryInventoryFinished(IabResult result,
Inventory inventory) {
Log.d(TAG, "Query inventory finished.");
// Have we been disposed of in the meantime? If so, quit.
if (mHelper == null)
return;
// Is it a failure?
if (result.isFailure()) {
// complain("Failed to query inventory: " + result);
return;
}
Log.d(TAG, "Query inventory was successful.");
/*
* Check for items we own. Notice that for each purchase, we check
* the developer payload to see if it's correct! See
* verifyDeveloperPayload().
*/
// Do we have the premium upgrade?
Purchase removeAdsPurchase = inventory.getPurchase(SKU_REMOVE_ADS);
AdCheck = (removeAdsPurchase != null && verifyDeveloperPayload(removeAdsPurchase));
//Yes there has been purchases!
if(AdCheck == true){
removeAds(); // sets global flag
//// Want to call UI change here....
MainActivity main = new MainActivity();
main.UpdateUI();
}
}
};
}
我從調試中了解到的是IabHelper.QueryInventoryFinishedListener在計費類的oncreate過程中被調用,它是異步的,因此我的主要活動繼續進行編譯。 完成購買設置和讀取操作后,我需要返回MainActivity進行更改。
如果有人可以在這里為我指明正確的方向-我已嘗試通過僅提供所需的代碼片段來使代碼直截了當。
使用Interface
。 在Interface
添加UpdateUI()
方法聲明。 然后在MyBilling
類中創建Interface
實例,並使用構造函數進行初始化。 在Interface
實例中調用UPdateUI()
方法。 然后在MainActivity
實現Interface
。 如果您不清楚要實現什么,我很樂意發布代碼。
編碼 :
在您的MyBilling
類中,在類中添加以下內容
public interface Update {
void UpdateUI();
}
使用它從MyBilling
類調用UpdateUI()
方法。
myActivity.UpdateUI();
將此用作MyBilling
類中的構造函數
Update myActivity;
public MyBilling (Update activity) {
myActivity = activity;
}
現在在MainActivity
更改為以下內容
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener, MyBilling.Update
如果在其下方出現紅線,請按住Alt鍵並輸入它以在MainActivity
實現Update
接口。 我猜因為您的MainActivity
已經有UpdateUI()
,所以不會出現紅線錯誤。 希望能幫助到你!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.