this is my activity i want to insert a native ads into the list view. I'm trying to follow this guide https://github.com/StartApp-SDK/Documentation/wiki/android-advanced-usage But I find it hard to understand. can you give me a hand, maybe making examples of code? thank you
ACTIVITY
public class EpisodiActivity extends Activity {
private StartAppAd startAppAd = new StartAppAd(this);
public class ViewModel {
private String url;
private String name;
public ViewModel(String url, String name) {
this.url = url;
this.name = name;
}
public String getUrl() {
return this.url;
}
public void setUrl(String url) {
this.url = url;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String toString() {
return this.name;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// creazione fullscreen activity
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.episodi_activity);
String[] episodi = getIntent().getStringArrayExtra("Product");
String[] urls = getIntent().getStringArrayExtra("urls");
ListView mylist = (ListView) findViewById(R.id.listView1);
// And in this loop we create the ViewModel instances from
// the name and url and add them all to a List
List<ViewModel> models = new ArrayList<ViewModel>();
for (int i = 0; i < episodi.length; i++) {
String name = episodi[i];
String url = "No value";
if (i < urls.length) {
url = urls[i];
}
ViewModel model = new ViewModel(url, name);
models.add(model);
}
// Here we create the ArrayAdapter and assign it to the ListView
// We pass the List of ViewModel instances into the ArrayAdapter
final ArrayAdapter<ViewModel> adapter = new ArrayAdapter<ViewModel>(
this, android.R.layout.simple_list_item_1, models);
mylist.setAdapter(adapter);
mylist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View v, int position,
long id) {
// Here we get the ViewModel at the given position
ViewModel model = (ViewModel) arg0.getItemAtPosition(position);
// And the url from the ViewModel
String url = model.getUrl();
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
}
});
}
@Override
public void onResume() {
super.onResume();
startAppAd.onResume();
startAppAd.showAd();
}
@Override
public void onPause() {
super.onPause();
startAppAd.onPause();
}
}
Recently I stucked with the same question but for a new Admob native ads. Then I decided to post my solution for that to admobadapter. Hope it will help you. I believe you could use the
AdmobAdapterWrapper
after some customizations for the StartApp-SDK...Kindly look at the AdmobFetcher.java. The result could appear like thisI've been working on this for a while now. I'm using a
BaseAdapter
and a LinearLayout holder for the ad. These are my global variables:I also have a few other global variables:
Here is my
BaseAdapter
, please read the commentsYou will notice in my
BaseAdapter
, that I have a method calledsetUpAd
:From that you need a few other methods:
And Finally, If you end up using a
GridView
, here is one last thing to add in order to load the right size ad after orientation changes:A listView
BaseAdapter
first calls the methodgetCount()
to retrieve the number of rows to show and then for each row it calls thegetView(int position, View convertView, ViewGroup parent)
method to create and return a View object for the given position.The approach is to extend
BaseAdapter
and create your own custom Adapter, instead of using the defaultArrayAdapter
. Then you need to return an “Ad” View instead of your usual normal View when you want to display an ad. This means that you need to overridegetCount()
to return more rows (for example if you have 10 rows, that means you need to return 11 = 10 actual content + 1 ad)Then you need to decide in which position to create this View, I think you can do it by simply checking the
position
variable:Anyway, this whole thing is really tricky as things can go easily out of hand (positions mismatching with Views etc). StartApp should be able to control your listView adapter to do all this for you. My guess is that your adapter is not communicating properly with StartApp (maybe you are not initialising correctly?).
Try to dig into the documentation or find an example by them. If you can’t figure it out, there are other alternatives you can use such as Avocarrot, Namomedia, Inmobi, etc
I have used Avocarrot which has an open source example in github for inserting ads in listViews. You can run it and use it if it fits you: https://github.com/Avocarrot/android-demo-app
I do not know the StartApp framework, but basically you have to do the following:
Write your own adapter and do not use
ArrayAdapter
. Here is a basis class you can use to simplify the adapters view recycling: https://github.com/sockeqwe/appkit/blob/master/adapter/src/main/java/com/hannesdorfmann/appkit/adapter/SimpleAdapter.javaSpecify own View cells by overriding
getItemViewType(int position)
andgetViewTypeCount()
Specify a own view type and view holder for your banners that will appear in the ListView.
From what I have seen, the banner needs to save something in
onSaveInstanceState()
and to restore something inonRestoreInstanceState()
. Im not sure if this is needed or makes sence in a ListView. Simply try it with or without this calls. If this is needed, than you have to keep a List of Banner Items in you adapter and you have to do something like this in your Activities code: