0

我有一个列表,显示从我的 SQLite 数据库中获取的信息,一切正常。我想在列表顶部放置一个广告横幅,并将其放在 XML 布局中,就像我对其他活动所做的那样,但是广告被视为列表文本视图,并在每个列表元素中与数据库信息一起重复。

我已经用 XML 布局代码试验了几个小时并阅读了其他解决方案,但似乎没有任何效果,我很难过。这是我的课程代码:

@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);
}

这是我的 XML 布局代码:

<?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>

我尝试在布局中嵌套布局以分隔广告和文本视图,但我实际上不确定我是否能够完成我需要做的事情,任何建议都值得赞赏。

4

1 回答 1

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! :)

于 2013-05-06T11:09:16.430 回答