如何在Android listview中阻止每个列表项重复我的广告

如何在Android listview中阻止每个列表项重复我的广告,android,android-layout,xml-layout,Android,Android Layout,Xml Layout,我有一个列表,其中显示了来自SQLite数据库的信息,一切正常。我想在列表的顶部放置一个广告横幅,并像对待其他活动一样将其放在XML布局中,但广告被视为列表文本视图,并与数据库信息一起在每个列表元素中重复 我已经用XML布局代码做了几个小时的实验,并阅读了其他解决方案,但似乎什么都不管用,我被难住了。这是我的班级代码: @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInsta

我有一个列表,其中显示了来自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>


我尝试将版面嵌套在版面中,以分离广告和文本视图,但我不确定我是否能够完成我需要做的事情,任何建议都非常感谢。

我想你正在使用ListActivity

简单的答案是永远不要使用ListActivity和ListFragment

使用常规活动,为setContentView指定布局(将横幅放在布局顶部,列表视图上方),并使用findViewbyId获取列表视图并添加适配器

@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
}
和布局

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


因此,正如您所看到的,代码之间确实没有太大的差异,现在您没有任何愚蠢的限制,并且您现在完全了解ListView实际上是如何工作的!:)

是的,我使用的是ListActivity,我不知道它们是一个坏选项?我会调查你的想法,看看我能不能做到。非常感谢。这纯粹是我的观点,我只是发现使用ListActivity和ListFragment产生的问题比解决的问题多。在您的案例中,您将广告添加到活动的itemview布局,而不是contentView布局。当然,如果您使用的是ListActivity,那么您没有setContentView,因此您要么不得不求助于丑陋的黑客,要么干脆放弃ListActivity,永远不要回头。最后还会遇到一些问题,比如,如何向ListActivity添加按钮,如何在一个ListActivity中有两个列表等。基本上,ListActivity最终出现在生产代码中是非常令人惊讶的,因为它的局限性没有任何好处。它隐藏了一些不应该隐藏的东西(视图)。这是完全有道理的,绝对有道理,但我不知道从哪里开始这个解决方案,你能提供一些例子吗?非常感谢。您在问题中指定的布局应该保持不变,我假设它名为res/layout/highscores.xml,但看看它,您已经将其设置为标题。。。无论如何,我认为您的“字段”需要包含要从游标中提取的两列。无论如何,如果我的答案足够的话,我希望你能接受。