简体   繁体   中英

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

I have the following code.

but my listView isn't seen in any flow that call notifyDataChanged.

what is it missing?

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

        }
    }
}

You are supposed to call:

arrayAdapter.notifyDataSetChanged();

notifyDataSetInvalidated() will call your implementation of onInvalidated() from the DataSetObserver class.

Also, while I cannot comment on this without your full code, it looks like your logic does not include the call to refreshList(String comment). Without a call to this, there would be no addition of element to the arrayAdapter object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM