gpt4 book ai didi

java - 我如何在下面的上下文中干燥我的代码(android)

转载 作者:行者123 更新时间:2023-12-01 16:13:52 24 4
gpt4 key购买 nike

我是 Android 开发新手,并使用共享首选项在点击多次按钮后显示广告。我有几个按钮(如下面的示例),每个按钮都有不同的用途。下面我的单一逻辑的最佳实现是什么,这样我的代码对于具有不同 Intent 的所有其他按钮来说都是 DRY(不要重复自己)。我的适配器类扩展了 RecyclerView

public class ImageAdaptor extends RecyclerView.Adapter<ImageAdaptor.MyViewHolder> {
#####################################
######################################
########################################
public class MyViewHolder extends RecyclerView.ViewHolder {
public MyViewHolder(View v) {
MobileAds.initialize(acontext, "ca-app-pub-3940256099942544~3347511713");
final InterstitialAd mInterstitialAd = new InterstitialAd(acontext);
mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
mInterstitialAd.loadAd(new AdRequest.Builder().build());


//Here is my working example on first button
//I want to achieve something like this on other clicks with different intents
//I don't want to repeat the logic over and over again

**Button1**.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final ModelStatus modelStatus = arrayList.get(getAdapterPosition());
final Intent intent = new Intent(acontext, Activity1.class);
mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdClosed() {
acontext.startActivity(intent);
}
});
SharedPreferences preferences = acontext.getSharedPreferences("PREFRENCE",
Context.MODE_PRIVATE);
int clickCount = preferences.getInt("KEY_CLICK_COUNT", 1);

if (clickCount % 6 == 0) {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
acontext.startActivity(intent);
};
}else {
acontext.startActivity(intent);
}
clickCount++;
preferences.edit().putInt("KEY_CLICK_COUNT", clickCount).apply();

}

});

**Button2**.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Intent intent = new Intent(acontext, Activity2.class);

//Some code with an intent
// I want my logic to go in here for 5 clicks per ad.
// I don't want to repeat myself since i have several such
//buttons with a different intent
}
});



}
}

最佳答案

就像馅饼一样简单

public void handleClick(Class destination, int buttonType){
final ModelStatus modelStatus = arrayList.get(getAdapterPosition());
final Intent intent = new Intent(getAppContext(), destination);
mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdClosed() {
acontext.startActivity(intent);
}
});
SharedPreferences preferences = acontext.getSharedPreferences("PREFRENCE",
Context.MODE_PRIVATE);
int clickCount = preferences.getInt("KEY_CLICK_COUNT"+buttonType, 1);

if (clickCount % 6 == 0) {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
acontext.startActivity(intent);
};
}else {
acontext.startActivity(intent);
}
clickCount++;
preferences.edit().putInt("KEY_CLICK_COUNT"+buttonType, clickCount).apply();
}

Button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
handleClick(Activity1.class, 1);
}

Button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
handleClick(Whatever.class, 2);
}

关于java - 我如何在下面的上下文中干燥我的代码(android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62453662/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com