Steve C. Steve C. - 5 months ago 25
Android Question

How to show ad after user clicks on listview item but not for every item in Android?

I have a listview populated with data from a remote database. When the user clicks on an item they are taken to a detail screen. My question is how do I show and ad when the user clicks on a list item but not everytime they click on a list item. I don't want to show an ad with every list item click just like after every 5 clicks or so. How would I go about doing that? I'm assuming some sort of logic within the listview onItemClickListener. I just have no idea what that logic would look like.

EDIT

Per user8's answer, I've added a counter and shared prefs to the onItemClickListener. Nothing happens when I click on an item though. Not even to see the DetailActivity. Here's my code:

list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Get item at position
RowData item = (RowData)parent.getItemAtPosition(position);

final Intent intent = new Intent(MainActivity.this, DetailActivity.class);

mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdClosed() {
requestNewInterstitial();
startActivity(intent);
}
});

SharedPreferences prefs = getPreferences(MODE_PRIVATE);


int clickCount = prefs.getInt(KEY_CLICK_COUNT, 1);
if (clickCount % 5 == 0) {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
startActivity(intent);
}
}
clickCount++;
prefs.edit().putInt(KEY_CLICK_COUNT, clickCount).apply();

//Start details activity
//startActivity(intent);
}
});


EDIT 2

Ok. I see what was going wrong. Per user8's answer, when the user clicks 5 times it shows an ad yet if the user clicks less than 5 times nothing happens. So I added an else statement so now the user can click a list item and go to the detail activity. An ad will still show after every 5 clicks but won't be "stale" in between ads. Here's the working code:

list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Get item at position
RowData item = (RowData)parent.getItemAtPosition(position);

final Intent intent = new Intent(MainActivity.this, DetailActivity.class);

mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdClosed() {
requestNewInterstitial();
startActivity(intent);
}
});

SharedPreferences prefs = getPreferences(MODE_PRIVATE);


int clickCount = prefs.getInt(KEY_CLICK_COUNT, 1);
if (clickCount % 5 == 0) {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
startActivity(intent);
}
}else{
startActivity(intent);
}
clickCount++;
prefs.edit().putInt(KEY_CLICK_COUNT, clickCount).apply();

//Start details activity
//startActivity(intent);
}
});

Answer

I would do something like this in your ListView's onItemClickListener:

SharedPreferences prefs = getPreferences(MODE_PRIVATE);

int clickCount = prefs.getInt(KEY_CLICK_COUNT, 1);
if (clickCount % 5 == 0) {
    showAd();
}
clickCount++;
prefs.edit().putInt(KEY_CLICK_COUNT, clickCount).apply();
Comments