在我添加一个片段对话框后,notifyDataSetChanged无法正常工作

时间:2016-11-28 22:26:59

标签: android adapter notifydatasetchanged

我在更新片段中的列表时遇到问题。我希望用户输入2个数字选择器中的文本和2个值。当我只使用AlertDialog时,notifyDataSetChanged似乎工作正常,当我点击AlertDialog中的OK按钮时,列表会更新。

但是,当我使用两个数字选择器添加Dialog时(在用户在AlertDialog中单击OK之后)并且调用notifyDataSetChanged没有任何事情发生在列表中。我必须关闭并重新打开片段以便更新。

我已经考虑过在用户完成添加时刷新片段,但我觉得这不是正确的方法。

(关于我的代码的任何其他评论,可以改善它显然是好的!)

//add Button
    button2 = (Button) myInflatedView.findViewById(R.id.buttonAdd);
    button2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            AlertDialog.Builder alert = new AlertDialog.Builder(getContext());  //create the AlertBuilder
            alert.setTitle("Subject");
            // Set an EditText view to get user input
            final EditText input = new EditText(getContext());
            alert.setView(input);
            alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                    //adds the sub to profile
                    profile.subject.add(input.getText().toString());


                    String[] days = {"Monday" , "Tuesday" , "Wednesday" ,"Thursday" ,"Friday"};
                    //dialog with the days
                    final Dialog d = new Dialog(getContext());
                    d.setTitle("Day");
                    d.setContentView(R.layout.dialog);
                    Button b1 = (Button) d.findViewById(R.id.button1);
                    Button b2 = (Button) d.findViewById(R.id.button2);
                    final NumberPicker np = (NumberPicker) d.findViewById(R.id.numberPicker1);
                    final NumberPicker np2 = (NumberPicker) d.findViewById(R.id.numberPicker2);
                    np.setMaxValue(4);
                    np.setMinValue(0);
                    np.setDisplayedValues(days);
                    np.setWrapSelectorWheel(false);
                    np2.setMaxValue(4);
                    np2.setMinValue(0);
                    np2.setDisplayedValues(days);
                    np2.setWrapSelectorWheel(false);
                    b1.setOnClickListener(new View.OnClickListener()
                    {
                        @Override
                        public void onClick(View v) {
                            profile.day.add(np.getValue());
                            profile.day2.add(np2.getValue());   //add the two values in profile

                            //saves profile
                            SharedPreferences.Editor editor = prefs.edit();
                            Gson gson = new Gson();
                            String json1 = gson.toJson(profile);
                            editor.putString("DATES" , json1);
                            editor.apply();

                            d.dismiss();

                            adapter.notifyDataSetChanged(); //should update the list, but instead does nothing
                        }
                    });
                    b2.setOnClickListener(new View.OnClickListener()
                    {
                        @Override
                        public void onClick(View v) {
                            d.dismiss();
                        }
                    });
                    d.show();

                    create(input.getText().toString());
                }

            });

            alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    // Canceled.
                }
            });
            alert.show();
        }

    });

0 个答案:

没有答案