1

Here is my code i want to show my events in the listview but it shows nothing

here is my code

public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_show_events_by_date);
            Intent i = getIntent();
            dateString = i.getExtras().getString("Date");
            elist = (ListView) findViewById(R.id.eventList);                    
            arraylist = new ArrayList<YourEvent>();
            adapter = new CustomAdapter(ShowEventsByDateActivity.this, arraylist);
            getEvents();                    
            elist.setAdapter(adapter);
        }

        private void getEvents() {
                    for(int a=0;a<StaticVariables.eventIds.size(); a++){                    
                        if(StaticVariables.eventDates.get(a).equals("6-12-2015")){
                            eId.add(StaticVariables.eventIds.get(a));
                            eTitle.add(StaticVariables.eventTitles.get(a));
                            eDetail.add(StaticVariables.eventDetails.get(a));
                            eType.add(StaticVariables.eventTypes.get(a));
                            eDate.add(StaticVariables.eventDates.get(a));
                            eTime.add(StaticVariables.eventTimes.get(a));
                            eLocation.add(StaticVariables.eventLocations.get(a));
                            eChoice.add(StaticVariables.eventChoices.get(a));
                        }                   
                    }               
                arraylist.clear();
                for (int i = 0; i < eId.size(); i++) {
                    YourEvent ye = new YourEvent(eId.get(i), eTitle.get(i),
                            eType.get(i), eDetail.get(i), eDate.get(i),
                            eTime.get(i), eLocation.get(i), eChoice.get(i));
                    arraylist.add(ye);
                }
                adapter.notifyDataSetChanged();
            }

and here is my customAdapter class

    class CustomAdapter extends BaseAdapter {
        Context context;
        LayoutInflater inflater;
        private List<YourEvent> yeList = null;
        private ArrayList<YourEvent> arraylist;
        String title1[], detail1[], location1[], id1[];

        public CustomAdapter(Context context, List<YourEvent> yeList) {
            this.yeList = yeList;

            this.context = context;
            inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            this.arraylist = new ArrayList<YourEvent>();
            this.arraylist.addAll(yeList);
        }

        public class ViewHolder {
            ImageView iv;
            // CheckBox cb;
            TextView title, location, date, time, choiceText;
        }

        @Override
        public View getView(final int position, View view, ViewGroup parent) {
            final ViewHolder holder;
            if (view == null) {
                holder = new ViewHolder();
                view = inflater.inflate(R.layout.event_list, null);
                // Locate the TextViews in listview_item.xml
                holder.iv = (ImageView) view.findViewById(R.id.eIv);
                holder.title = (TextView) view.findViewById(R.id.eTitle);
                holder.date = (TextView) view.findViewById(R.id.eDate);
                holder.location = (TextView) view.findViewById(R.id.eLocation);
                holder.time = (TextView) view.findViewById(R.id.eTime);
                holder.choiceText = (TextView) view.findViewById(R.id.choiceText);
                // holder.cb = (CheckBox) view.findViewById(R.id.checkbox);
                // holder.cb.setVisibility(View.GONE);
                view.setTag(holder);
            } else {
                holder = (ViewHolder) view.getTag();
            }
            // Set the results into TextViews
            // holder.id.setText(yeList.get(position).getId());
            holder.title.setText(yeList.get(position).getTitle());
            holder.date.setText(yeList.get(position).getDate());
            holder.location.setText(yeList.get(position).getLocation());
            holder.time.setText(yeList.get(position).getTime());
            holder.choiceText.setText(eChoice.get(position));

            // Listen for ListView Item Click
            view.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    Intent intent = new Intent(context, ViewEventActivity.class);
                    intent.putExtra("title", eTitle.get(position));
                    intent.putExtra("type", eType.get(position));
                    intent.putExtra("detail", eDetail.get(position));
                    intent.putExtra("date", eDate.get(position));
                    intent.putExtra("time", eTime.get(position));
                    intent.putExtra("location", eLocation.get(position));
                    startActivity(intent);
                }
            });
            return view;
        }
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return 0;
        }


        @Override
        public Object getItem(int arg0) {
            // TODO Auto-generated method stub
            return null;
        }


        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return 0;
        }
    }

i've tried similar code in which i get events from an online webservice. But i dn't know why this code is not working . Plz help

  • 1
    getCount can't return 0. http://stackoverflow.com/questions/16338281/custom-adapter-getview-method-is-not-called/16338380#16338380 – Blackbelt Jun 21 '15 at 16:05
  • cannot believe i made such a stupid mistake :/ wasted 3 hours thinking i had problem in some other place -_- thanks alot sir :) wish i could upvote your answer but it's just a comment – Muhammad Oan Jun 21 '15 at 16:59

2 Answers2

1

The problem lies in these lines in your adapter:

    this.arraylist = new ArrayList<YourEvent>();
    this.arraylist.addAll(yeList);

This copies the original values from your list (which is empty), to a new list that is ONLY inside your adapter.

Then when you add values in your activity, you add them to the original list, which isn't the same as the one in your adapter.

The reference between the 2 lists is lost when you do .addAll(), as it does copy-by-value and not copy-by-reference.

To fix this, simply:

1) Refactor arraylist into a List instead of an ArrayList
2) set this.arraylist = yeList instead of the 2 lines i posted above.

This will correctly keep the reference between the 2 lists, so when you add new elements to the list in your activity and call notifyDataSetChanged(), it will show them in the list.

Moonbloom
  • 6,529
  • 2
  • 21
  • 36
0

Just swap these two lines:

getEvents();      //this would come after setAdapter              
 elist.setAdapter(adapter);

To

 elist.setAdapter(adapter);
 getEvents(); 
Pankaj
  • 7,380
  • 6
  • 39
  • 60