我想爲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;
}
}
任何想法?
錯誤是什麼? – Mus 2014-12-02 23:17:25