Solved! Look at the edit below!
I am trying to create a list view to show different type of layouts but am getting this error
(I am getting this error when I scroll up, after scrolling down and seeing both types of layouts)
E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
I can scroll to the bottom and see both types of layouts (couldn't see them both at once (error) when the else statement with the comment "//THIS ONE!" want there)
Code for my adapter is as follows - (edited as with @matiash's suggestions)
public class NewsAdapter extends ArrayAdapter<NewsXMLData> {
ImageLoader imageLoader;
DisplayImageOptions options;
private static final int TYPE_ITEM = 0;
private static final int TYPE_SEPARATOR = 1;
private static final int TYPE_MAX_COUNT = TYPE_SEPARATOR + 1;
private LayoutInflater mInflater;
public NewsAdapter(Context ctx, int textViewResourceId, List<NewsXMLData> sites) {
super(ctx, textViewResourceId, sites);
//Setup the ImageLoader, we'll use this to display our images
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(ctx).build();
imageLoader = ImageLoader.getInstance();
imageLoader.init(config);
//Setup options for ImageLoader so it will handle caching for us.
options = new DisplayImageOptions.Builder()
.cacheInMemory()
.cacheOnDisc()
.build();
}
@Override
public int getItemViewType(int position) {
if(getItem(position).getType().equals("image")) {
return TYPE_ITEM;
}
else{
return TYPE_SEPARATOR;
}
}
@Override
public int getViewTypeCount() {
return TYPE_MAX_COUNT;
}
public static class ViewHolder {
public ImageView iconImg = null;
public TextView contentTxt = null;
public TextView imageTitleTxt = null ;
public ProgressBar imageIndicator = null ;
public TextView textTitleTxt = null ;
public ProgressBar textIndicator = null;
}
String flag;
//@Override
public View getView(int pos, View convertView, ViewGroup parent){
ViewHolder holder =null;
int type = getItemViewType(pos);
Log.i("APP", "getView " + pos + " " + convertView + " type = " + type);
View row = convertView;
mInflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
try {
if (null == row) {
//No recycled View, we have to inflate one.
holder = new ViewHolder();
switch (type){
case TYPE_ITEM:
row = (RelativeLayout) mInflater.inflate(R.layout.row_news, null);
holder.iconImg = (ImageView) row.findViewById(R.id.newsImageView);
holder.imageTitleTxt = (TextView) row.findViewById(R.id.newsTitleText);
holder.imageIndicator = (ProgressBar) row.findViewById(R.id.newsProgressBar);
holder.iconImg.setVisibility(View.INVISIBLE);
holder.imageIndicator.setVisibility(View.VISIBLE);
holder.imageTitleTxt.setVisibility(View.INVISIBLE);
//Setup a listener we can use to swtich from the loading indicator to the Image once it's ready
ImageLoadingListener listener = new ImageLoadingListener() {
@Override
public void onLoadingStarted(String arg0, View arg1) {
// TODO Auto-generated method stub
}
@Override
public void onLoadingCancelled(String arg0, View arg1) {
// TODO Auto-generated method stub
}
@Override
public void onLoadingComplete(String arg0, View arg1, Bitmap arg2) {
}
@Override
public void onLoadingFailed(String arg0, View arg1, FailReason arg2) {
// TODO Auto-generated method stub
}
};
holder.iconImg.setVisibility(View.VISIBLE);
holder.imageIndicator.setVisibility(View.INVISIBLE);
holder.imageTitleTxt.setVisibility(View.VISIBLE);
//Load the image and use our options so caching is handled.
imageLoader.displayImage(getItem(pos).getImgUrl(), holder.iconImg, options, listener);
//Set the relavent text in our TextViews
holder.imageTitleTxt.setText(getItem(pos).getTitle());
break;
case TYPE_SEPARATOR:
row = (RelativeLayout) mInflater.inflate(R.layout.row_news_text, null);
holder.contentTxt = (TextView) row.findViewById(R.id.newsTextContentText);
holder.textTitleTxt = (TextView) row.findViewById(R.id.newsTextTitleText);
holder.textIndicator = (ProgressBar) row.findViewById(R.id.newsTextProgressBar);
/*holder.contentTxt.setVisibility(View.INVISIBLE);
holder.textIndicator.setVisibility(View.VISIBLE);
holder.textTitleTxt.setVisibility(View.INVISIBLE);*/
holder.contentTxt.setVisibility(View.VISIBLE);
holder.textIndicator.setVisibility(View.INVISIBLE);
holder.textTitleTxt.setVisibility(View.VISIBLE);
holder.textTitleTxt.setText(getItem(pos).getTitle());
holder.contentTxt.setText(getItem(pos).getContent());
break;
}
convertView.setTag(holder);
}
else{
holder = (ViewHolder)convertView.getTag();
}
}catch (Exception e){
e.printStackTrace();
}
return row;
}
}
Any suggestions?
Also, is there a better way to do it other than parsing XML and showing the content as a list view?
Edit: The code now works thanks to @Jens and @matiash Code:
public class NewsAdapter extends ArrayAdapter<NewsXMLData> {
ImageLoader imageLoader;
DisplayImageOptions options;
private static final int TYPE_ITEM = 0;
private static final int TYPE_SEPARATOR = 1;
private static final int TYPE_MAX_COUNT = TYPE_SEPARATOR + 1;
private LayoutInflater mInflater;
public NewsAdapter(Context ctx, int textViewResourceId, List<NewsXMLData> sites) {
super(ctx, textViewResourceId, sites);
//Setup the ImageLoader, we'll use this to display our images
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(ctx).build();
imageLoader = ImageLoader.getInstance();
imageLoader.init(config);
//Setup options for ImageLoader so it will handle caching for us.
options = new DisplayImageOptions.Builder()
.cacheInMemory()
.cacheOnDisc()
.build();
}
@Override
public int getItemViewType(int position) {
if(getItem(position).getType().equals("image")) {
return TYPE_ITEM;
}
else{
return TYPE_SEPARATOR;
}
}
@Override
public int getViewTypeCount() {
return TYPE_MAX_COUNT;
}
public static class ViewHolder {
public ImageView iconImg = null;
public TextView contentTxt = null;
public TextView imageTitleTxt = null ;
public ProgressBar imageIndicator = null ;
public TextView textTitleTxt = null ;
public ProgressBar textIndicator = null;
}
String flag;
//@Override
public View getView(int pos, View convertView, ViewGroup parent){
ViewHolder holder =null;
int type = getItemViewType(pos);
//Log.i("APP", "getView " + pos + " " + convertView + " type = " + type);
View row = convertView;
mInflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
try {
if (null == row) {
//No recycled View, we have to inflate one.
holder = new ViewHolder();
switch (type) {
case TYPE_ITEM:
row = (RelativeLayout) mInflater.inflate(R.layout.row_news, null);
break;
case TYPE_SEPARATOR:
row = (RelativeLayout) mInflater.inflate(R.layout.row_news_text, null);
break;
}
//Log.i("APP","holder" + holder);
}
if(type==TYPE_ITEM){
holder = new ViewHolder();
holder.iconImg = (ImageView) row.findViewById(R.id.newsImageView);
holder.imageTitleTxt = (TextView) row.findViewById(R.id.newsTitleText);
holder.imageIndicator = (ProgressBar) row.findViewById(R.id.newsProgressBar);
holder.iconImg.setVisibility(View.INVISIBLE);
holder.imageIndicator.setVisibility(View.VISIBLE);
holder.imageTitleTxt.setVisibility(View.INVISIBLE);
//Setup a listener we can use to swtich from the loading indicator to the Image once it's ready
ImageLoadingListener listener = new ImageLoadingListener() {
@Override
public void onLoadingStarted(String arg0, View arg1) {
// TODO Auto-generated method stub
}
@Override
public void onLoadingCancelled(String arg0, View arg1) {
// TODO Auto-generated method stub
}
@Override
public void onLoadingComplete(String arg0, View arg1, Bitmap arg2) {
}
@Override
public void onLoadingFailed(String arg0, View arg1, FailReason arg2) {
// TODO Auto-generated method stub
}
};
holder.iconImg.setVisibility(View.VISIBLE);
holder.imageIndicator.setVisibility(View.INVISIBLE);
holder.imageTitleTxt.setVisibility(View.VISIBLE);
//Load the image and use our options so caching is handled.
imageLoader.displayImage(getItem(pos).getImgUrl(), holder.iconImg, options, listener);
//Set the relavent text in our TextViews
holder.imageTitleTxt.setText(getItem(pos).getTitle());
}
else if(type==TYPE_SEPARATOR){
holder = new ViewHolder();
holder.contentTxt = (TextView) row.findViewById(R.id.newsTextContentText);
holder.textTitleTxt = (TextView) row.findViewById(R.id.newsTextTitleText);
holder.textIndicator = (ProgressBar) row.findViewById(R.id.newsTextProgressBar);
/*holder.contentTxt.setVisibility(View.INVISIBLE);
holder.textIndicator.setVisibility(View.VISIBLE);
holder.textTitleTxt.setVisibility(View.INVISIBLE);*/
holder.contentTxt.setVisibility(View.VISIBLE);
holder.textIndicator.setVisibility(View.INVISIBLE);
holder.textTitleTxt.setVisibility(View.VISIBLE);
holder.textTitleTxt.setText(getItem(pos).getTitle());
holder.contentTxt.setText(getItem(pos).getContent());
}
}
catch(Exception e){
e.printStackTrace();
}
return row;
}
}