Search code examples
androidandroid-layoutandroid-listviewadmobandroid-xml

Listview Admob Layout


My Layout XML for my Admob is causing some errors.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

<com.google.ads.AdView
    android:id="@+id/adView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    ads:adSize="BANNER"
    ads:adUnitId="ca-app-pub-xxxxxxxxxxxxxxxxxxxxx"
    ads:loadAdOnCreate="true" />

<ListView
    android:id="@+id/list_view"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/sailor" />

<TextView
    android:id="@+id/row"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/trans"
    android:padding="10dp"
    android:textColor="@color/menu_text"
    android:textSize="22sp"
    android:textStyle="normal" />

</LinearLayout>

Activity

public class ListViewOne extends Activity {

// we use a string to hold the name of our extra,
// it must include the full package name
public final static String ID_EXTRA = "com.faarn.navyslang._ID";

private DatabaseHelper dbDataBaseHelper = null;
private Cursor ourCursor = null;
private DataBaseAdapter adapter = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    try {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // this is our ListView element, obtained by id from our XML Layout
        ListView myListView = (ListView) findViewById(R.id.list_view);

        // create our database Helper
        dbDataBaseHelper = new DatabaseHelper(this);        
        // we call the create right after initializing the helper, just in
        // case
        // they have never run the app before
        dbDataBaseHelper.createDatabase();
        //
        // open the database!! Our helper now has a SQLiteDatabase database
        // object
        dbDataBaseHelper.openDataBase();
        // get our cursor. A cursor is a pointer to a dataset, in this case
        // a set of results from a database query
        ourCursor = dbDataBaseHelper.getCursor();
        // tell android to start managing the cursor
        // we do this just incase our activity is interrupted or ends, we
        // want the activity
        // to close and deactivate the cursor, as needed
        startManagingCursor(ourCursor);
        // create our adapter
        adapter = new DataBaseAdapter(ourCursor);
        // set the adapter!!!
        myListView.setAdapter(adapter);

        // this is how we know what to do when a list item is clicked
        myListView.setOnItemClickListener(onListClick);
    } catch (Exception e) {

        // this is the line of code that sends a real message to the Log
        Log.e("ERROR", "ERROR IN CODE: " + e.toString());

        // this is the line that prints out the location
        // the code where the error occurred.
        e.printStackTrace();
    }

}

private AdapterView.OnItemClickListener onListClick = new                    AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        // Create our intent, as per usual
        Intent i = new Intent(ListViewOne.this, ListViewTwo.class);

        i.putExtra(ID_EXTRA, String.valueOf(id));
        TabGroupActivity parentActivity = (TabGroupActivity)getParent();
        parentActivity.startChildActivity("ListViewTwo", i);

    }
};

class DataBaseAdapter extends CursorAdapter {
    DataBaseAdapter(Cursor c) {
        super(ListViewOne.this, c);
    }

    @Override
    // this is a CursorAdapter
    // instead of Using a getView and if(row=null)
    // we use a bindView and newView calls
    // we can get away with this because CursorAdapters have
    // a default implementation of getView that calls bindView and newView
    // as needed. This makes our code a bit cleaner, and is the better way
    // to
    // do this
    public void bindView(View row, Context ctxt, Cursor c) {
        DataBaseHolder holder = (DataBaseHolder) row.getTag();
        holder.populateFrom(c, dbDataBaseHelper);
    }

    @Override
    public View newView(Context ctxt, Cursor c, ViewGroup parent) {
        LayoutInflater inflater = getLayoutInflater();
        View row = inflater.inflate(R.layout.main, parent, false);
        DataBaseHolder holder = new DataBaseHolder(row);
        row.setTag(holder);
        return (row);
    }
}

static class DataBaseHolder {
    private TextView name = null;

    DataBaseHolder(View row) {
        name = (TextView) row.findViewById(R.id.row);

    }

    void populateFrom(Cursor c, DatabaseHelper r) {
        name.setText(r.getName(c));
    }

}

}

The Admob is sitting at the top as expected but is also appearing between every row of my list. I have looked at lots of other questions about this but have not been able to correct it. Any help appreciated.


Solution

  • Finally sorted out the problem after 3 days trial and error. For some reason having the TextView and ListView in the same XML threw the adMob off. I removed the TextView and put it in its own XML Layout and the code now works. I then needed to change the view inflator from the XML containing the admob code to the XML containing the TextView code in all of my list activities.

    New Code

    Main XML

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    
    <com.google.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ads:adSize="BANNER"
        ads:adUnitId="ca-app-pub-9438516449663554/1376593424"
        ads:loadAdOnCreate="true"
        ads:testDevices="TEST_EMULATOR, 37ae14af62d03e19" />
    
    <ListView
        android:id="@+id/list_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/sailor" />
    
    </LinearLayout>
    

    main_row XML

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    
    
    <TextView
        android:id="@+id/row"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@color/trans"
        android:padding="10dp"
        android:textColor="@color/menu_text"
        android:textSize="22sp"
        android:textStyle="normal" />
    </LinearLayout>
    

    Activity Line

    View row = inflater.inflate(R.layout.main, parent, false);

    changed to

    View row = inflater.inflate(R.layout.main_row, parent, false);