0

I have a list which displays information taken form my SQLite Database, everything works fine. I want to place an advert banner at the top of the List and have it in the XML layout as I do with my other activity's but the adverts are treated like the list text views and repeated inside each list element along with the database information.

I have experimented with the XML layout code for a few hours and read up on other solutions, but nothing seems to be working, I am stumped. Here is my class code:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ScoresDbAdapter.DatabaseHelper helper = new ScoresDbAdapter.DatabaseHelper(this);
    database = helper.getWritableDatabase();
    Cursor data = database.query("scores", fields, null, null, null, null, null);

    dataSource = new SimpleCursorAdapter(this, R.layout.highscores, data, fields, new int[] { R.id.first, R.id.last });

    ListView view = getListView();
    view.setHeaderDividersEnabled(true);
    view.addHeaderView(getLayoutInflater().inflate(R.layout.highscores, null));

    setListAdapter(dataSource);
}

Here is my XML layout code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:id="@+id/rowLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<com.google.ads.AdView
    android:id="@+id/adView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    ads:adSize="BANNER"
    ads:adUnitId="a151868c65661b8"
    ads:loadAdOnCreate="true" />

<TextView
    android:id="@+id/first"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:text="First name" />

<TextView
    android:id="@+id/last"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:gravity="right"
    android:text="Last name" />

</RelativeLayout>

I tried nesting layouts in a layout to separate the ads and text views but I am not actually sure I will be able to accomplish what I need to do, any advice is appreciated.

deucalion0
  • 2,422
  • 9
  • 55
  • 99

1 Answers1

1

I take it you are using ListActivity?

The simple answer is stop using ListActivity and ListFragment for anything ever.

Use a regular activity, have a layout specified for setContentView (put the banner on top of the layout, above a ListView) and use findViewbyId to get the listview and add the adapter.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main)  //need to set layout now that we are no longer using listactivity

    ScoresDbAdapter.DatabaseHelper helper = new ScoresDbAdapter.DatabaseHelper(this);
    database = helper.getWritableDatabase();
    Cursor data = database.query("scores", fields, null, null, null, null, null);

    dataSource = new SimpleCursorAdapter(this, R.layout.highscores, data, fields, new int[]    { R.id.first, R.id.last });

    ListView view = (ListView) findViewbyId(R.id.list_view) //findbyid instead of getListView()
    view.setHeaderDividersEnabled(true);
    view.addHeaderView(getLayoutInflater().inflate(R.layout.highscores, null));

    view.setAdapter(dataSource);  //set the adapter to the listView
}

And for layout

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:gravity="center"
          android:orientation="vertical">

    <com.google.ads.AdView
    android:id="@+id/adView"
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    ads:adSize="BANNER"
    ads:adUnitId="a151868c65661b8"
    ads:loadAdOnCreate="true" />

    <ListView
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:id="@+id/list_view" />
</LinearLayout>

So as you can see, there realy isn't much difference in code and now you don't have any silly limitations and you are now fully aware of how ListView actually works! :)

Pork 'n' Bunny
  • 6,740
  • 5
  • 25
  • 32
  • Yes I am using a ListActivity, I had no idea that they were a bad option? I will look into your idea and see if i can do this. Thank you! – deucalion0 May 06 '13 at 11:10
  • It's purely my opinion, I just find that more problems are created than solved using ListActivity and ListFragment than solved. In your case you are adding the ads to the itemview layouts instead of the contentView layout of the activity. Of course you don't have a setContentView if you are using ListActivity, so you either have to resort to ugly hacks, or just ditch ListActivity and never look back. – Pork 'n' Bunny May 06 '13 at 11:12
  • 1
    Also end up with questions down the line like, how do I add a button to a ListActivity, how can I have 2 lists in a ListActivity etc. Bassically it would be very surprising to see ListActivity end up in production code simply due to it's limitations with no benefit. It hides something that shouldn't be hidden (views). – Pork 'n' Bunny May 06 '13 at 11:14
  • That makes complete sense Pork, absolutely it does, but I am just not sure where to start with this solution, could you provide any examples? Thank you!! – deucalion0 May 06 '13 at 11:25
  • Thank you Pork, I implemented your code, and I think it is nearly there, but one thing I am getting an error with and not really sure how to populate the list, it did it with this line: dataSource = new SimpleCursorAdapter(this, R.layout.highscores, data, fields, new int[] { R.id.first, R.id.last }); But now this line causes an error because first and last textviews do not exist. I will keep trying to figure it out! Thank you! :) – deucalion0 May 06 '13 at 11:43
  • 1
    the layout you specified in your question should remain the same, I assume it's called res/layout/highscores.xml but looking at that, you've set that as your header... Anyway I think your "fields" needs to contain the two columns you want to extract from the cursor. Anyway, if find my answer sufficient I'd appreciate you marking it accepted. – Pork 'n' Bunny May 06 '13 at 11:51
  • Thanks for all your help, I have done it! It looks great and works exactly as I want it to!! Thank you Pork! :) – deucalion0 May 06 '13 at 12:05