2013-01-18 50 views
0

我正在嘗試使用ListView作爲雜誌閱讀器應用程序的主要導航功能。我希望列表中的每個視圖在與其關聯的圖像下方顯示故事的標題。我已經創建了一個帶有垂直方向LinearLayout的XML佈局,上面有一個ImageView,底部是TextView,並且正在嘗試創建一個自定義適配器,以便在getView方法中擴展此佈局,並將圖像/文本資源放入其中。這是我的代碼:android:爲什麼沒有元素出現在我的列表視圖中?

package com.example.caliber; 
import java.util.List; 
import com.example.caliber.R; 

public class MainActivity extends Activity { 
Spinner issues, sections; 
Button blog; 
ListView mainGal; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    String[] issueList = {"issues", "fall '12", "spring '12", "fall '11", "spring '12", 
          "fall '10", "spring '10"}; 
    String[] sectionList = {"sections", "campus", "entertainment", "lifestyle", "love&sex", "tech&web", "Current" }; 

    ArrayAdapter<String> aaIssues = new ArrayAdapter<String>(this, R.layout.spin_item, issueList); 
    ArrayAdapter<String> aaSections = new ArrayAdapter<String>(this, 
R.layout.spin_item, sectionList); 

    blog = (Button)findViewById(R.id.blogBtn); 

    issues = (Spinner)findViewById(R.id.issueSpin); 
    sections = (Spinner)findViewById(R.id.sectionSpin);    
    issues.setAdapter(aaIssues); 
    sections.setAdapter(aaSections); 


    OnClickListener mbListener = new OnClickListener() { 
     @Override 
      public void onClick(View v){ 
     issues.setAlpha((float) 0.5); 
     sections.setAlpha((float) 0.5); 
     blog.setAlpha((float) 0.5); 
     } 
    }; 


    ImageButton mainbtn = (ImageButton)findViewById(R.id.cbutton);  
    mainbtn.setOnClickListener(mbListener); 

    mainGal = (ListView)findViewById(R.id.mainGal); 
    GalAdapter ga = new GalAdapter(); 
    mainGal.setAdapter(ga); 

} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.activity_main, menu); 
    return true; 
} 

class GalAdapter extends ArrayAdapter<String> { 
    Activity context; 
    String[] headlines = {"Alpha bravo charlie delta", "Echo foxtrot golf 
hotel", "indigo juliette kilo lima"}; 
    int[] images = {R.drawable.img1, R.drawable.img2, R.drawable.img3}; 

    public GalAdapter() { 
     super (MainActivity.this, R.layout.galleryview); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent){ 
     LayoutInflater li = (LayoutInflater) context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE);   
     View story = li.inflate(R.layout.galleryview, parent); 
     ImageView image = (ImageView)story.findViewById(R.id.image); 
     TextView headline = (TextView)story.findViewById(R.id.headline); 

     image.setImageResource(images[position]); 
     headline.setText(headlines[position]); 
     return story;  
} 

該應用程序啓動罰款,我可以看到listview的背景,但沒有列表中的項目。爲什麼是這樣?可能很清楚,我對android開發非常陌生,但對Java的基礎知識有很好的理解。我知道這是由於我在這裏有一個基本的誤解,也許是一個構造函數的錯誤(我對某些參數仍然有點模糊)。無論如何,如果有人能夠幫助我更深入地理解這些問題,我會非常感激。

在此先感謝

p.s.在android和java下對它進行分類可憐的形式嗎?我應該在將來使用android嗎?

---編輯:

class GalAdapter extends ArrayAdapter<String> { 
    Activity context; 
    String[] hlines; 
    int[] imgs; 

    public GalAdapter(String[] hlines, int[] imgs) { 
     super (MainActivity.this, R.layout.galleryview , hlines); 
     this.hlines = hlines; 
     this.imgs = imgs; 
    }  
    @Override 
    public View getView(int position, View convertView, ViewGroup parent){ 
     if (convertView == null){ 
     LayoutInflater li = (LayoutInflater)context. getSystemService (Context.LAYOUT_INFLATER_SERVICE); 
     convertView = li.inflate(R.layout.galleryview, parent); 
     ImageView image = (ImageView)convertView.findViewById(R.id.image); 
     TextView headline = (TextView)convertView.findViewById(R.id.headline); 

     image.setImageResource(imgs[position]); 
     headline.setText(hlines[position]);} 
     return convertView;         
    } 
    } 

現在我得到一個空指針異常在哪裏我叫LayoutInflator行。它指的是什麼指針?我假設我沒有通過數組(字符串[] hlines + INT [] IMGS)到它正確的......這裏是我打這個電話:

String[] headlines ={ "Echo foxtrot golf hotel", "Echo foxtrot golf hotel", "indigo 
juliette kilo lima"}; 
    int[] images = {R.drawable.img1, R.drawable.img2, R.drawable.img3}; 
      .... 
    mainGal = (ListView)findViewById(R.id.mainGal); 
    GalAdapter ga = new GalAdapter(headlines, images); 
    mainGal.setAdapter(ga); 
+1

您可以調用'super.onCreate(savedInstanceState); setContentView(R.layout。activity_main);'兩次,把它拿出來 - 它是多餘的。只是一個代碼提示。 –

+1

哎呀,謝謝你指出這個愚蠢的錯誤! – drewmoore

回答

0

你的適配器沒有正確創建。爲了使用ArrayAdapter,你必須:

  1. 供應項目超執行的列表
  2. 直接添加項目到適配器,或
  3. 覆蓋所有必要進行分類數據的方法適配器持有,如getCount()getItem()

最簡單的解決方法是改變你的構造函數,像這樣:

public GalAdapter() { 
    super(MainActivity.this, R.layout.galleryview, headlines); 
} 

這樣做的作用是允許適配器實現使用其中一個陣列來確定列表中有多少項,以及調用getItem()時應返回哪些數據。由於您優先於getView(),因此第二個參數甚至不是必需的,您也可以使用super(MainActivity.this, 0, headlines)

如果最終實現它來收集來自多個陣列像這樣的數據,你可能最終會直接創建自定義適配器的BaseAdapter子類的更好,因爲你已經繞過大部分功能ArrayAdapter提供便民。

+0

Ahhhhh,謝謝,這真的很有幫助:第二個參數,我認爲必須是這種情況。我改變了我的代碼,但不幸的是,現在我得到一個空指針異常。請參閱我的編輯 – drewmoore

+0

這是因爲代碼中的參考「活動上下文」從未設置爲任何內容,但是您嘗試調用其上的方法。而不是傳遞對Activity的引用,只需刪除該行並使用'getLayoutInflater()。inflate()'來代替。 – Devunwired

+0

convertView = getLayoutInflater()。inflate(R.layout.galleryview,parent); aaaand現在我在同一行上得到一個不受支持的操作異常...再次感謝您的幫助,對不起,在屁股上這麼痛苦。 – drewmoore

1

看一看這裏https://github.com/cplain/custom-list它會能夠幫助你一下,你需要在getView()中使用convertView,我建議將一個列表傳遞給你的適配器而不是在裏面製作一個

+0

這個例子使用了一個不好的模式,你不應該在'inflate()'中傳遞'null'。 'getView()'傳遞給父'ViewGroup'的原因僅僅是爲了讓你使用它來使你的子視圖膨脹,以便在這個過程中正確評估佈局參數。 – Devunwired

相關問題