为什么我的 listView 不显示适配器的列表?

why is my listView doesn't show the adapter's list?

我有以下代码。

但是在任何调用 notifyDataChanged 的​​流程中都看不到我的 listView。

缺少什么?

public class ItemDetailFragment extends Fragment {


...



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

...


        comments = new ArrayList<>();
        // This is the array adapter, it takes the context of the activity as a
        // first parameter, the type of list view as a second parameter and your
        // array as a third parameter.
        arrayAdapter = new ArrayAdapter<>(
                getActivity(),
                android.R.layout.simple_list_item_1,
                comments);

        listView.setAdapter(arrayAdapter);


...

        return rootView;
    }

    private void refreshList(String comment) {
        //also works: arrayAdapter.add(comment);
        comments.add(comment);
        //listView.invalidate();
        arrayAdapter.notifyDataSetInvalidated();
    }


    private void getPhoneComments(View rootView) {
        String phoneNumber = ((EditText) rootView.findViewById(R.id.phone_editText)).getText().toString();
        Phone phone = phoneDal.getItem(phoneNumber);
        if (phone != null) {
            comments = commentDal.getAllItems(phone.id);
            arrayAdapter.notifyDataSetInvalidated();

        }
    }
}

你应该打电话给:

arrayAdapter.notifyDataSetChanged();

notifyDataSetInvalidated() 将从 DataSetObserver class 调用 onInvalidated() 的实现。

此外,虽然没有您的完整代码我无法对此发表评论,但看起来您的逻辑不包括对 refreshList(String comment) 的调用。如果不调用它,就不会向 arrayAdapter 对象添加元素。