android - notifydatasetchanged 不显示我的列表

标签 android

<分区>

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

这是我的代码

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

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

最佳答案

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

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

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

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

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

要解决这个问题,只需:

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

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

关于android - notifydatasetchanged 不显示我的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30966485/

相关文章:

android - 拍照后恢复 camera.preview()

java - Android DataBinding可以使用内部类吗?

android - 如何将初始 Prop (或启动选项)传递给第一个场景?

android - 如何支持 android 中的所有设备(手机和平板电脑)?

php - 通过 PC 名称而不是本地 IP 地址访问 wamp 服务器文件-Android 应用程序

android - https 简单获取请求

android - linux 上的 'ANDROID_HOME' 环境变量,带有 cordova 未定义的 nodejs

Android ProgressDialog 停止旋转

android - Google Play 控制台警告 : unknow validation VALIDATE_APP_MESSAGE_ICON_NOT_UNIFORM

android - Eclipse-CDT 无法在 NDK 项目中找到 stdlib 符号