这是我的java类,它将新闻存储在类新闻的数组中,然后在listview中输出
public class News extends Activity {
List<news> myNews = new ArrayList<news>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(com.example.makkosarka.R.layout.activity_news);
populateNewsList();
populateListView();
}
private void populateListView() {
ArrayAdapter<news> adapter = new MyListAdapter();
ListView list = (ListView) findViewById(com.example.makkosarka.R.id.listview_news);
list.setAdapter(adapter);
}
public void populateNewsList() {
//tuka ja polnis listata
myNews.add(new news("title1", 1,"something1"));
myNews.add(new news("title2", 2,"something2"));
myNews.add(new news("title3", 3,"something3"));
}
private class MyListAdapter extends ArrayAdapter<news>{
public MyListAdapter(){
super(News.this,com.example.makkosarka.R.layout.item_news, myNews);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//make sure that we have a View to work with
View row = convertView;
if (row == null){
row = getLayoutInflater().inflate(com.example.makkosarka.R.layout.item_news, parent,false);
}
//Find the product to work with
news currentNews = myNews.get(position);
//Fill the view
TextView txtcena = (TextView)findViewById(com.example.makkosarka.R.id.item_txtCena);
TextView txtime = (TextView)findViewById(com.example.makkosarka.R.id.item_txtName);
txtime.setText((currentNews).getBody());
txtcena.setText(( currentNews).getTitle());
return row;
}
}
private class news{
private String Title;
private int ImageID;
private String Body;
public news(String title, int imageID, String body){
this.Title=title;
this.ImageID=imageID;
this.Body=body;
}
public String getTitle() {
return Title;
}
public int getImageID() {
return ImageID;
}
public String getBody() {
return Body;
}}}
错误nullpointerexception acour at the line:
txtime.setText((currentNews).getBody());
txtcena.setText(( currentNews).getTitle());
这个代码结构可能会出现什么问题? 我正在实现自定义行布局!
答案 0 :(得分:4)
更改为
TextView txtcena = (TextView)row.findViewById(com.example.makkosarka.R.id.item_txtCena);
TextView txtime = (TextView)row.findViewById(com.example.makkosarka.R.id.item_txtName);
视图属于膨胀的布局。
考虑使用ViewHolder
。
class ViewHolder
{
TextView txtcena,txtime;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null){
holder = new VieHolder();
convertVIew = getLayoutInflater().inflate(com.example.makkosarka.R.layout.item_news, parent,false);
holder.txtcena = (TextView)convertView.findViewById(com.example.makkosarka.R.id.item_txtCena);
holder.txtime = (TextView)convertVIew.findViewById(com.example.makkosarka.R.id.item_txtName);
convertView.setTag(holder);
} else{
holder = (ViewHolder)convertView.getTag();
}
news currentNews = myNews.get(position);
holder.txtime.setText((currentNews).getBody());
holder.txtcena.setText(( currentNews).getTitle());
return convertView;
}
答案 1 :(得分:1)
当您访问列表视图中的其他视图项时,您应该从该视图中找到ID,所以更改为
TextView txtcena = (TextView)row.findViewById(com.example.makkosarka.R.id.item_txtCena);
TextView txtime = (TextView)row.findViewById(com.example.makkosarka.R.id.item_txtName);