在没有 notifyDataSetChanged 的​​情况下刷新列表?

Posted

技术标签:

【中文标题】在没有 notifyDataSetChanged 的​​情况下刷新列表?【英文标题】:List refreshed withous notifyDataSetChange? 【发布时间】:2019-04-12 13:27:57 【问题描述】:

我有一个小问题要问你。我有一个应用程序,它显示可以删除或添加的主题列表。在开发过程中,我意识到我可以在不调用 notifyDataSetChange 的情况下添加新主题,为什么?

管理主题列表的类(ListSubject):

public static ListView mListView;
private Context mContext;
List<Subject> data;
SubjectAdapter subjectSubjectAdapter;

public ListSubject(Context mContext) 
    this.mContext = mContext;


public List<Subject> populateList() 
    DBManager db = new DBManager(mContext);

    data = db.GetSubjects();

    return data;


public void DeleteAll()
    DBManager db = new DBManager(mContext);
    db.ClearTable("TBLSUBJECTS");
    data.clear();


public void AddSubject(String name, int frequency)

    DBManager db = new DBManager(mContext);
    Subject subToAdd = db.InsertSubject(name, frequency);
    data.add(subToAdd);


public void DeleteSubject(int id)
    DBManager db = new DBManager(mContext);
    db.DeleteSubject(id);

类(ListSubjectActivity),它表示listview所在的活动:

DBManager db;
Button btnClearSubjectTable;
Button btnAddSub;

Context mContext;
private List<Subject> mListSubject;
private SubjectAdapter subjectAdapter;
private ListView lvSubject;
private ListSubject ls;

public ListSubjectActivity(Context mContext) 
    this.mContext = mContext;


public ListSubjectActivity()

@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.listview_subject);

    db = new DBManager(this);
    btnClearSubjectTable = (Button) findViewById(R.id.btnClearSubjectTable);
    btnAddSub = (Button) findViewById(R.id.btnAddSubject);
    lvSubject = (ListView)findViewById(R.id.subjectList);
    mListSubject = new ArrayList<>();
    ls = new ListSubject(getApplicationContext());

    mListSubject = ls.populateList();


    btnClearSubjectTable.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View v) 
           ls.DeleteAll();
             subjectAdapter.notifyDataSetChanged();
        
    );

    btnAddSub.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View v) 

            final Dialog dialog = new Dialog(ListSubjectActivity.this);
            dialog.setContentView(R.layout.dialog_insert_subject);

            final EditText txtNewSubName = (EditText)  dialog.findViewById(R.id.txtNewSubName);
            final EditText txtFreqNewSub = (EditText) dialog.findViewById(R.id.txtFreqNewSub);


            Button dialogButtonOK = (Button) dialog.findViewById(R.id.btnOK);
            dialogButtonOK.setOnClickListener(new View.OnClickListener() 
                @Override
                public void onClick(View v) 
                    AddSubject(txtNewSubName.getText().toString(), Integer.parseInt(txtFreqNewSub.getText().toString()));
                    subjectAdapter.notifyDataSetChanged();
                    dialog.dismiss();
                
            );
            Button dialogButtonCancel = (Button) dialog.findViewById(R.id.btnCancel);
            dialogButtonCancel.setOnClickListener(new View.OnClickListener() 
                @Override
                public void onClick(View v) 
                    dialog.dismiss();
                
            );

            dialog.show();
            Window window = dialog.getWindow();
            window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, 700);

        
    );
    subjectAdapter = new SubjectAdapter(getApplicationContext(), mListSubject);
    lvSubject.setAdapter(subjectAdapter);


public void AddSubject(String newSub, int frequency)

    ls.AddSubject(newSub, frequency);

我的主题适配器:

private Context mContext;
private List<Subject> mList;
private ListSubject ls;


public SubjectAdapter(Context mContext, List<Subject> mList) 
    this.mContext = mContext;
    this.mList = mList;


@Override
public int getCount() 
    return mList.size();


@Override
public Object getItem(int position) 
    return mList.get(position);


@Override
public long getItemId(int position) 
    return position;


@Override
public View getView(final int position, View convertView, ViewGroup parent) 

    View v = View.inflate(mContext, R.layout.item_subject_list,null);
    TextView txtSubjectName = v.findViewById(R.id.txtName);
    TextView txtFrequency = v.findViewById(R.id.txtFrequency);

    txtSubjectName.setText(mList.get(position).GetSubjectName());
    txtFrequency.setText(Integer.toString(mList.get(position).GetFrequency()));

    ls = new ListSubject(mContext);
    ImageButton btnDeleteSub = (ImageButton) v.findViewById(R.id.btnDeleteSub);
    btnDeleteSub.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View v) 
            ls.DeleteSubject(mList.get(position).GetSubjectID());
            mList.remove(position);
            notifyDataSetChanged(); useless

        
    );

    return v;

请记住,我是 android 开发的新手,所以可能会犯很多错误。谢谢你帮助我

【问题讨论】:

【参考方案1】:

如果您当前的代码是您想知道的,您正在在添加项目时调用notifyDataSetChanged()。就在这里:

        Button dialogButtonOK = (Button) dialog.findViewById(R.id.btnOK);
        dialogButtonOK.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                AddSubject(txtNewSubName.getText().toString(), Integer.parseInt(txtFreqNewSub.getText().toString()));
                subjectAdapter.notifyDataSetChanged(); //see?
                dialog.dismiss();
            
        );

【讨论】:

是的,伙计,我知道,但是如果我评论“subjectAdapter.notifyData..”那一行,listview 无论如何都会刷新,所以我问自己,notifyDataSetChange 的作用是什么?因为那时我不明白。例如,如果我添加新项目并且在我的代码中我没有 subjectAdapter.notifyDataSetChanged() 我可以看到显示的新项目

以上是关于在没有 notifyDataSetChanged 的​​情况下刷新列表?的主要内容,如果未能解决你的问题,请参考以下文章

在 notifyDataSetChanged 之后 FragmentStateAdapter 没有重新创建 currentFragment

没有在 RecyclerView 上工作 Notifydatasetchange 与中心选择水平滚动视图?

RecyclerView 在 NotifyDataSetChanged() 之后没有更新,打开和关闭屏幕后显示内容

自定义 listView 数组,搜索,在 NotifyDataSetChanged 上崩溃,加上 listView 没有得到更新

notifyDataSetChanged() 没有变化/黑屏;

notifyDataSetChanged 示例