ListFragment 未使用 notifydatasetChanged 更新

ListFragment is not updated with notifydatasetChanged

我被 listfragment 困住了,因为片段列表视图没有随着 allData arrayalist 中的新项目添加而更新。数据来自服务器,在接口的帮助下传递给 listfragment class,结果到达 passData ArrayList<SparseArray<String>> allData。我检查了大小,在 passData() 方法中没问题,但 listfragemtn 仍未更新。 我已经尝试了使 listfragment 与 notifyDataSetChanged()notifydatasetinvalidated() 一起工作的每一种工作方法,但它没有用。

片段:

public class ViewFeedback extends ListFragment implements PassDataToFeedback
{
    ArrayList<SparseArray<String>> allData;
    CustomAdapter customAdapter;

    @Override
    public void onAttach(Activity activity) {
        // TODO Auto-generated method stub
        super.onAttach(activity);
        Context context=activity;
        ((Activity_ViewFeedback)context).setListener(this);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    /*  db = new DatabaseManager(getActivity());
        lists = db.getAllLists();
        pAdapter = new PayListAdapter(getActivity(), lists);
        setListAdapter(pAdapter);

        registerForContextMenu(getListView());*/
        allData=new ArrayList<SparseArray<String>>();
        customAdapter=new CustomAdapter(allData);
        setListAdapter(customAdapter);
        setListShown(true);
        setListShownNoAnimation(true);

    }

适配器:

    public class CustomAdapter extends BaseAdapter
    {
        ArrayList<SparseArray<String>> allData;

        public CustomAdapter(final ArrayList<SparseArray<String>> allData) 
        {
            // TODO Auto-generated constructor stub
            if(allData!=null && allData.size()>0)
                this.allData=allData;
            else
                this.allData=new ArrayList<SparseArray<String>>();
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return allData.size();
        }

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

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

        @Override
        public View getView(int arg0, View arg1, ViewGroup arg2) {
            // TODO Auto-generated method stub
            ViewHolder viewHolder=null;
            if(arg1==null)
            {
                arg1=LayoutInflater.from(arg2.getContext()).inflate(R.layout.row_layout_view_feedback, arg2, false);
                viewHolder=new ViewHolder(arg1);
                arg1.setTag(viewHolder);
            }else
                viewHolder=(ViewHolder)arg1.getTag();

            viewHolder.tv_msg.setText(allData.get(arg0).get(0));
            viewHolder.tv_from.setText(allData.get(arg0).get(1));
            viewHolder.tv_from.setSelected(true);
            viewHolder.tv_when.setText(allData.get(arg0).get(2));
            viewHolder.tv_when.setSelected(true);
            return arg1;
        }

        public class ViewHolder
        {
            TextView tv_msg;
            TextView tv_from;
            TextView tv_when;

            public ViewHolder(final View view)
            {
                tv_msg=(TextView)view.findViewById(R.id.avf_tv_email);
                tv_from=(TextView)view.findViewById(R.id.avf_tv_from);
                tv_when=(TextView)view.findViewById(R.id.avf_tv_when);
            }
        }
    }

    @Override
    public void passData(ArrayList<SparseArray<String>> allData) {
        // TODO Auto-generated method stub
        if(allData!=null)
        {
            if(this.allData.size()>0)
            {
                this.allData.clear();
                customAdapter.notifyDataSetInvalidated();
            }
            this.allData.addAll(allData);
            customAdapter.notifyDataSetChanged();
        }
    }
}

片段布局:

<fragment
                android:id="@+id/myfragment"
                android:name="android.support.v4.app.ListFragment"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@id/avf_layout_linear"
                class="com.fragments.ViewFeedback" />

问题是您通过调用 this.allData=new ArrayList<SparseArray<String>>();

将数据源设置为适配器内的新引用

初始化 allData 列表并将其设置为适配器的数据源后,您绝不能更改对新列表的引用。

没有必要做空检查,因为你在将它传递给适配器之前初始化它,你应该设置 allData 数据源,即使它是一个空列表,它将在创作的时候。

它保证不会为 null,因为您在将它提供给 CustomAdapter 构造函数之前在这里对其进行了初始化:

 allData=new ArrayList<SparseArray<String>>();
 customAdapter=new CustomAdapter(allData);

此外,从一个空列表开始作为适配器的数据源是完全有效的(只是此时不会显示任何行)。一旦您将数据添加到 allData 并调用 notifyDataSetChanged(),列表将在此时填充。

    public CustomAdapter(final ArrayList<SparseArray<String>> allData) 
    {
        // TODO Auto-generated constructor stub
        //if(allData!=null && allData.size()>0) //no need for this

            this.allData=allData;

        //This was your problem, remove this code:
        //else
            //this.allData=new ArrayList<SparseArray<String>>();
    }

然后,您可以根据需要修改allData,并调用notifyDataSetChanged()更新ListView。