Это мой Java класс, который хранит новости массива известия класса, а затем выводить их в ListViewNullPointerException при реализации пользовательского макета строки
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 на линии:
txtime.setText((currentNews).getBody());
txtcena.setText((currentNews).getTitle());
В чем может быть проблема с этой структурой кода? Я реализую пользовательскую раскладку строк!
вы можете описать, почему использование 'ViewHolder'? –
@Shayanpourvatan http://developer.android.com/training/improving-layouts/smooth-scrolling.html – Raghunandan