notifyDataSetChanged()不使用自定义适配器

时间:2016-01-16 02:46:08

标签: android android-arrayadapter custom-adapter notifydatasetchanged

我正在对自定义对象列表进行编辑,但notifyDataSetChanged()未更新列表。设置:片段显示状态列表。在Dialog Fragment中编辑状态,并在编辑操作时调用applyDialogState()

void applyDialogStateEdit(int id, String name) {
        // Apply StateEditDialog fragment edits directly to mStates
        State state = mStates.get(id);
        state.name = name;

        // Write state change to saves
        getActivity().getSharedPreferences("myStates", Context.MODE_PRIVATE).edit().
                putString(state.date, mGson.toJson(state)).commit();

        mStateAdapter.notifyDataSetChanged();
    }

我看过的事情:

  • 我确认调用了该方法。

  • SharedPreferences已更新 - 因此重新加载活动后可以看到更改。

  • 似乎没有参考问题。

  • 调用notifyDataSetChanged()后,我看到调试器中自定义适配器中对应于mStates的内部状态列表已正确更新...

  • 在其他情况下,notifyDataSetChanged()正在工作......即

    public void deleteAllStates() {
        getActivity().getSharedPreferences("myStates", Context.MODE_PRIVATE).edit().clear().commit();
        mStates.clear();
        if (mStateAdapter != null) {
            mStateAdapter.notifyDataSetChanged();
        }
    }
    

我的自定义阵列适配器:

class StateAdapter extends ArrayAdapter<State> {
    Context context;
    ArrayList<State> states;
    int row_res;
    StateAdapter(Context context, int itemViewID, ArrayList<State> states) {
        super(context, itemViewID, states);
        this.context = context;
        this.row_res = itemViewID;
        this.states = states;
    }
@Override
public View getView(int pos, View convertView, ViewGroup parent) {
    View row;
    if (convertView == null) {
        LayoutInflater inflater = ((Activity)context).getLayoutInflater();
        row = inflater.inflate(row_res, null);
        TextView tv = (TextView) row.findViewById(R.id.state_description);
        tv.setText(states.get(pos).name + "; " + states.get(pos).date);
    } else {
        row = convertView;
    }
    return row;
}

有什么想法吗?

EDIT1 - 扩展ArrayAdapter:

class StateAdapter extends ArrayAdapter<State> {
    Context context;
    ArrayList<State> states;
    int row_res;
    StateAdapter(Context context, int itemViewID, ArrayList<State> states) {
        super(context, itemViewID, states);
        this.context = context;
        this.row_res = itemViewID;
        this.states = states;
    }

EDIT2 - 在列表中设置数组适配器:

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Log.e(TAG, "onCreateView: called");
        // Inflate the layout for this fragment
        View root = inflater.inflate(R.layout.fragment_state_list, null, false);
        mRoot = root;
        ListView lv = (ListView) root.findViewById(R.id.state_list);
        if (mStates != null) {
            mStateAdapter = new StateAdapter(getActivity(), R.layout.state_row, mStates);
            lv.setAdapter(mStateAdapter);
            lv.setOnItemClickListener(new ListView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    try {
                        Log.e(TAG, "onItemClick: position clicked - " + position);
                        ((StateListInterface) getActivity()).onStateSelected(mStateAdapter.getItem(position));
                    } catch (Exception e) {
                        // do nothing
                    }
                }
            });

0 个答案:

没有答案