我有以下代码。
但我的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();
}
}
}
答案 0 :(得分:0)
你应该打电话:
arrayAdapter.notifyDataSetChanged();
notifyDataSetInvalidated()将从DataSetObserver类中调用onInvalidated()的实现。
另外,虽然如果没有完整代码我无法对此进行评论,但看起来您的逻辑不包含对refreshList(String comment)的调用。如果不调用它,就不会在arrayAdapter对象中添加元素。