2014-12-02 56 views
0


我想爲ListView設置BaseAdapter,但運行時出現錯誤。錯誤發生在getCount方法的NewsAdapter.java文件中:如果我寫「return 0」,我沒有錯誤,但是如果寫「return list.size()」,我在運行時出錯。
這裏是所有的文件:

News.javaBaseAdapter android getCount()方法

public class News extends Activity { 

TextView txt1; 
ArrayList<NewsArray> listaArray; 
NewsAdapter baseAdapter; 
ListView lista; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_news); 

    txt1=(TextView) findViewById(R.id.textView1); 
    lista= (ListView) findViewById(R.id.listView1); 

    baseAdapter= new NewsAdapter(this, R.layout.newsframe, listaArray); 
    lista.setAdapter(baseAdapter); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 

    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.news, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 
    if (id == R.id.action_settings) { 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
    } 
    } 


NewsAdapter.java

public class NewsAdapter extends BaseAdapter { 

ArrayList<NewsArray> list=new ArrayList<NewsArray>(); 
Context c; 
int layoutResourceId; 

public NewsAdapter(Context c, int layoutResourceId, ArrayList<NewsArray> list) { 
    // TODO Auto-generated constructor stub 
    this.layoutResourceId = layoutResourceId; 
    this.c = c; 
    this.list = list; 

} 

@Override 
public int getCount() { 
    // TODO Auto-generated method stub 
    return list.size(); 
    //return 0; 
} 

@Override 
public Object getItem(int arg0) { 
    // TODO Auto-generated method stub 
    return list.get(arg0); 

} 

@Override 
public long getItemId(int arg0) { 
    // TODO Auto-generated method stub 
    return arg0; 

} 

@Override 
public View getView(int arg0, View arg1, ViewGroup arg2) { 
    // TODO Auto-generated method stub 

    return null; 
} 
} 


NewsArray.java

public class NewsArray { 
String name, nickname, date, description, ncomment, nlike; 

public NewsArray(String name, String nickname, String date, String description, String ncoment, String nlike) { 
    // TODO Auto-generated constructor stub 
    this.name=name; 
    this.nickname=nickname; 
    this.date=date; 
    this.description=description; 
    this.ncomment=ncoment; 
    this.nlike=nlike; 
} 
} 


任何想法?

+0

錯誤是什麼? – Mus 2014-12-02 23:17:25

回答

0

你需要調用ArrayList<NewsArray> listaArray的構造函數之前創建適配器:

listaArray = new ArrayList<NewsArray>(); 

這是因爲如果你傳遞一個ArrayList作爲參數,你逝去的僅僅是一個參考。您應該將內容複製到BaseAdapter構造函數中:

this.list = new ArrayList<NewsArray>(list); 
+0

哈哈哈耶!正是這樣。謝謝!! – Sabbo 2014-12-02 23:32:52

0

嘗試更改getView的返回語句。除非需要,否則您也可以嘗試使用ArrayAdapter而不是重新發明輪子。

另外DavidGSola在他的回答中說,你沒有正確初始化你的ArrayList。

相關問題