my code is running without error but the custom Listview does not display the values .
my code is running without error but the custom Listview does not display the values .
this is my code:
the Adapter
public class Adapter extends BaseAdapter{
ArrayList<mealItems> itemList;
public Activity context;
public LayoutInflater inflater;
public DiseasesAdapter(Activity context,int x,ArrayList<mealItems> itemList) {
super();
this.context = context;
this.itemList = itemList;
this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public Object getItem(int position) {
return itemList.get(position);
}
public long getItemId(int position) {
return 0;
}
public static class ViewHolder
{
ImageView img;
TextView Title;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView==null)
{
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.disease_row, null);
holder.Title = (TextView) convertView.findViewById(R.id.Name);
holder.img = (ImageView) convertView.findViewById(R.id.image);
convertView.setTag(holder);
}
else
holder=(ViewHolder)convertView.getTag();
Items bean = (Items) itemList.get(position);
holder.Title.setText(bean.getTitle());
holder.img.setImageResource(bean.getImage());
return convertView;
}
public int getCount() {
// TODO Auto-generated method stub
return 0;
}
}
Your getCount() method returns a 0, this should return the size of the itemList instead.
i.e return itemList.size()
Try this :
this.itemList=new ArrayList<mealItems>();
this.itemList.addAll(itemList);
And of course, the getCount() method to return itemList.size()
Related
I am displaying a listview in which, the row item has two textview. The second textview is for the name of companies and the first textview is for the starting letter of the companies. How this can be achieved ? Need help !!
public class ExampleAdapter extends BaseAdapter {
Context context;
ArrayList<ExamplePojo> items = new ArrayList<>();
public ExampleAdapter(Context context, ArrayList<ExamplePojo> items) {
this.context = context;
this.items = items;
}
#Override
public int getCount() {
return items.size();
}
#Override
public Object getItem(int position) {
return items.get(position);
}
#Override
public long getItemId(int position) {
return items.indexOf(items.get(position));
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null){
convertView = inflater.inflate(R.layout.example_row_item, null);
holder = new ViewHolder();
holder.txtSentence = (TextView) convertView.findViewById(R.id.txtSentence);
holder.txtInitialLetter = (TextView) convertView.findViewById(R.id.txtInitialLetter);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
final ExamplePojo pojo = items.get(position);
holder.txtSentence.setText(pojo.getSentence());
holder.txtInitialLetter.setText(pojo.getSentence().charAt(0));
return convertView;
}
public class ViewHolder{
TextView txtSentence, txtInitialLetter;
}
}
Here is code for showing first character in first textview and complete name in other textview.Also put below lines in if(convertView== null) block
final ExamplePojo pojo = items.get(position);
holder.txtSentence.setText(pojo.getSentence());
holder.txtInitialLetter.setText(pojo.getSentence().substring(0, 1));
When I scroll my GridView up and down, the gridview will start lose its children and eventually (intermittently) the whole gridview will dissapear on bottom of page if I done too many scroll.
Here is my adapter code:
public class Adapter extends BaseAdapter {
private Context context;
private LayoutInflater inflater;
private List<Item> list;
public Adapter(Context _context, List<Item> _list){
this.context = _context;
this.list = _list;
this.inflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
return this.list.size() - 1;
}
#Override
public Object getItem(int position) {
return this.list.get(position);
}
#Override
public long getItemId(int arg0) {
return 0;
}
public class ViewHolder {
ImageView imageView;
TextView itemLabel;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = View.inflate(context, .., null);
holder = new ViewHolder();
holder.imageView = (ImageView)convertView.findViewById(..);
holder.itemLabel = (TextView)convertView.findViewById(..);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Item item = list.get(position + 1);
holder.itemLabel.setText(item.getTitle());
holder.imageView.setImageDrawable(item.getImage());
return convertView;
}
}
What could be wrong, as my adapter is a standard adapter?
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
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.
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)
#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)
// 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;
}
}
How would I go about inserting this code into a ListView in android?
List<YouTubeVideo> videos = ym.retrieveVideos(textQuery, maxResults, filter, timeout);
for (YouTubeVideo youtubeVideo : videos) {
System.out.println(youtubeVideo.getWebPlayerUrl());
System.out.println("Thumbnails");
for (String thumbnail : youtubeVideo.getThumbnails()) {
System.out.println("\t" + thumbnail);
}
System.out.println(youtubeVideo.getEmbeddedWebPlayerUrl());
System.out.println("************************************"); }
}
}
Would I create a custom adapter and use getView()? If so how would it look?
Yes, I would create a custom adapter and use getView(). Here is some sample code for a row layout with a TextView and an ImageView:
private static class MyListAdapter extends BaseAdapter
{
private Context context;
private ArrayList<Item> items;
private LayoutInflater mInflater;
public MyListAdapter(Context context, ArrayList<Item> items)
{
this.context = context;
this.items = items;
mInflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
return items.size();
}
#Override
public Object getItem(int position) {
return items.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
if (convertView == null) {
convertView = mInflater.inflate(R.layout.row_layout, parent, false);
}
Item item = items.get(position);
TextView tv = (TextView) convertView.findViewById(R.id.data_item);
tv.setText(item.getItem());
ImageView iv = (ImageView) convertView.findViewById(R.id.imageView1);
iv.setImageBitmap(item.getBitmap());
return convertView;
}
I have a problem. Currently i'm using Lazyloading to load images and data into my listview using arraylist. However it only display the last item of the arraylist.
Below is the LazyLoading adapter class:
public class LazyAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<Hashtable<String,String>> data;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader;
public LazyAdapter(Activity a, ArrayList<Hashtable<String,String>> d) {
activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new ImageLoader(activity.getApplicationContext());
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public static class ViewHolder{
public TextView title;
public TextView author;
public TextView format;
public TextView call_number;
public ImageView cover_image;
public Button next;
public Button previous;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
ViewHolder holder;
if (convertView == null){
vi = inflater.inflate(R.layout.search_result_display_list , null);
holder=new ViewHolder();
holder.next = (Button) vi.findViewById(R.id.next);
holder.previous = (Button) vi.findViewById(R.id.previous);
holder.title = (TextView) vi.findViewById(R.id.item_title);
holder.author = (TextView) vi.findViewById(R.id.item_author);
holder.format = (TextView) vi.findViewById(R.id.item_format);
holder.call_number = (TextView) vi.findViewById(R.id.item_call_number);
holder.cover_image=(ImageView) vi.findViewById(R.id.cover_image);
vi.setTag(holder);
}
else {
holder=(ViewHolder)vi.getTag();
vi = convertView;
}
for(int i = 0; i < getCount(); i++) {
holder.title.setText(data.get(i).get("title"));
holder.author.setText(data.get(i).get("author"));
holder.format.setText(data.get(i).get("format"));
holder.call_number.setText(data.get(i).get("callNumber"));
holder.cover_image.setTag(data.get(i).get("coverImage"));
imageLoader.DisplayImage(data.get(i).get("coverImage"), activity, holder.cover_image);
}
return vi;
}
I'm thinking that the problem might actually be with my for-loop. Where it is being generated infinitely. But I don't know where did I go wrong in typing this code. Please help.
Remove the for loop and in place of i in data.get(i) use position