列表刷新了notifyDataSetChange吗?

时间:2018-11-08 23:55:09

标签: android database android-studio listview notifydatasetchanged

我有一个问题要问你。我有一个应用程序,它显示可以删除或添加的主题列表。在开发过程中,我意识到无需调用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 个答案:

答案 0 :(得分:0)

如果您当前想知道的是当前代码,则添加项目时 会调用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();
            }
        });