【问题标题】:notifiydatasetchanged not showing my list [duplicate]notifydatasetchanged 未显示我的列表 [重复]
【发布时间】:2015-06-21 16:01:43
【问题描述】:

这是我的代码 我想在列表视图中显示我的事件,但它什么也没显示

这是我的代码

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();
            }

这是我的 customAdapter 类

    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;
        }
    }

我尝试过类似的代码,在其中我从在线 Web 服务获取事件。但我不知道为什么这段代码不起作用。请帮忙

【问题讨论】:

  • getCount 不能返回 0。stackoverflow.com/questions/16338281/…
  • 不敢相信我犯了这么愚蠢的错误:/浪费了 3 个小时,以为我在其他地方有问题 -_- 非常感谢先生 :) 希望我能支持你的答案,但这只是一个评论跨度>

标签: android


【解决方案1】:

问题在于适配器中的这些行:

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

这会将列表中的原始值(为空)复制到仅在适配器内部的新列表中。

然后,当您在活动中添加值时,会将它们添加到原始列表中,这与适配器中的列表不同。

当您执行 .addAll() 时,两个列表之间的引用会丢失,因为它会按值复制而不是按引用复制。

要解决这个问题,只需:

1) 将 arraylist 重构为 List 而不是 ArrayList
2) 设置this.arraylist = yeList 而不是我上面发布的2行。

这将正确地保留 2 个列表之间的引用,因此当您在 Activity 中向列表中添加新元素并调用 notifyDataSetChanged() 时,它将在列表中显示它们。

【讨论】:

  • 对不起,我得到了第 2 点,但没有得到第 1 点,你为什么不让我这样做?
  • @blackbelt 的回答解决了我的问题,我在 getcount 中返回 0 -_- 愚蠢的错误:/ 但也感谢您的解决方案 :)
【解决方案2】:

只需交换这两行:

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

 elist.setAdapter(adapter);
 getEvents(); 

【讨论】:

  • 我试过了,没用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-03-01
  • 1970-01-01
  • 2021-06-26
  • 1970-01-01
  • 1970-01-01
  • 2020-07-30
  • 2020-01-24
相关资源
最近更新 更多