2013-05-06 26 views
0

我有一個列表顯示從我的SQLite數據庫採取的信息,一切工作正常。我想在列表的頂部放置一個廣告橫幅,並將其放在XML佈局中,就像我處理其他活動一樣,但廣告被視爲列表文本視圖,並在每個列表元素中重複以及數據庫信息。我怎樣才能停止我的廣告重複每個列表項在我的Android列表視圖

我已經嘗試了幾個小時的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> 

我在佈局中的廣告和文本視圖分離嘗試嵌套的佈局,但我實際上並不相信我能能夠完成我需要做的事情,任何建議表示讚賞。

回答

1

我認爲你使用的是ListActivity?

簡單的答案是停止使用ListActivity和ListFragment的任何事情。

使用常規性活動,有指定的setContentView佈局(把旗幟上的佈局的頂部,一個ListView以上)和使用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實際工作! :)

+0

是的我使用ListActivity,我不知道他們是一個不好的選擇?我會研究你的想法,看看我能否做到這一點。謝謝! – deucalion0 2013-05-06 11:10:16

+0

這完全是我的看法,我發現創建的問題比使用ListActivity和ListFragment解決的問題要解決得多。在你的情況下,你要將廣告添加到項目視圖佈局,而不是活動的contentView佈局。當然如果你使用的是ListActivity,你沒有setContentView,所以你不得不求助於醜陋的黑客,或者只是溝通ListActivity,而不要回頭。 – 2013-05-06 11:12:06

+1

最後還有一些問題,如何向ListActivity添加按鈕,如何在ListActivity中添加2個列表等等。低調地看到ListActivity最終會出現在生產代碼中,這僅僅是因爲它沒有好處的限制。它隱藏了一些不應該隱藏的東西(視圖)。 – 2013-05-06 11:14:30

相關問題