Reputation: 5
I'm having a problem with returning my view to a listview with my custom adapter. This is what I have in my main class which extends ListActivity.
lv = getListView();
ListAdapater itemAdapter = new ItemAdapter(MainActivity.this,itemList)
lv.setAdapter(itemAdapter);
And here's what I have for my custom adapter named ItemAdapter:
public class ItemAdapter extends BaseAdapter {
private static LayoutInflater inflater;
private Context context;
private ArrayList<HashMap<String,String>> newsItem;
public ItemAdapter(Context a, ArrayList<HashMap<String, String>> item){
super();
this.context = a;
this.newsItem = item;
}
@Override
public int getCount() {
return 0;
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
View vi = view;
if(view==null){
inflater = ( LayoutInflater )context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
vi = inflater.inflate(R.layout.news_item_layout,parent,false );
}
TextView title = (TextView)vi.findViewById(R.id.title); // title
TextView date = (TextView)vi.findViewById(R.id.date); // date
HashMap<String,String> nItem = newsItem.get(position);
title.setText(nItem.get(MainActivity.TITLE));
date.setText(nItem.get(MainActivity.DATE));
//UrlImageViewHelper.setUrlDrawable(thumb_image, nItem.get(MainActivity.IMAGE));
return vi;
}
I'm not sure why this adapter does not return any view at all? I've tried it with a SimpleAdapter with the same data and it works. I need to create a custom adapter to handle image data.
Any help would be greatly appreciated. Thanks
Upvotes: 0
Views: 315
Reputation: 24848
// try this way,hope this will help you...
public class ItemAdapter extends BaseAdapter {
private Context context;
private ArrayList<HashMap<String, String>> newsItem;
public ItemAdapter(Context a, ArrayList<HashMap<String, String>> item) {
super();
this.context = a;
this.newsItem = item;
}
@Override
public int getCount() {
return newsItem.size();
}
@Override
public Object getItem(int i) {
return newsItem.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
ViewHolder holder;
if (view == null) {
holder = new ViewHolder();
view = LayoutInflater.from(context).inflate(R.layout.news_item_layout,null,false);
holder.title = (TextView) convertView.findViewById(R.id.title);
holder.date = (TextView) convertView.findViewById(R.id.date);
view.setTag(holder);
}else{
holder = (ViewHolder) view.getTag();
}
holder.title.setText(newsItem.get(position).get(MainActivity.TITLE));
holder.date.setText(newsItem.get(position).get(MainActivity.DATE));
//UrlImageViewHelper.setUrlDrawable(thumb_image, nItem.get(MainActivity.IMAGE));
return view;
}
class ViewHolder{
TextView title;
TextView date;
}
}
Upvotes: 0
Reputation: 6836
Here in your adapter you return 0 value.
@Override
public int getCount() {
return 0;
}
change it to
@Override
public int getCount() {
return item.size();
}
and in getView method change
if(view==null)
to
if(vi==null)
Upvotes: 1
Reputation: 748
@Override
public int getCount() {
return newsItem.size();
}
@Override
public Object getItem(int i) {
return newsItem.get(i);
}
@Override
public long getItemId(int i) {
return newsItem.get(i).hashCode();
}
and
if(vi==null)
Upvotes: 0
Reputation: 15515
Based on count only your views will added to your listview, So change your getCount()
method as
@Override
public int getCount() {
return newsItem.size();
}
And change
if(view==null)
to
if(vi==null)
I hope this will help you.
Upvotes: 2