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

Java 为什么我的listView不显示适配器的列表?,java,android,listview,adapter,Java,Android,Listview,Adapter,我有以下代码 但是我的listView在任何调用notifyDataChanged的流中都看不到 它少了什么 public class ItemDetailFragment extends Fragment { ... @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInsta

我有以下代码

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

它少了什么

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类调用onInvalidated的实现

另外,虽然我不能在没有完整代码的情况下对此进行评论,但您的逻辑似乎不包括对refreshListString注释的调用。如果不调用此函数,arrayAdapter对象中就不会添加元素。

refreshList或getPhoneComments是否有效?此外,还应将其更改为NotifyDataSetChange