2014-12-02 99 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。

相关问题