在解除对话框后从mysql填充listview

时间:2015-05-18 23:52:33

标签: android mysql listview

目前我有一个包含列表视图的片段。它有一个Add按钮,它将生成一个自定义对话框片段,用户可以在其中输入一个字符串。

当通过ok按钮关闭对话框时,listview应自动更新以包含新输入。

截至目前,我必须单击添加按钮,我可以在对话框后面的背景中看到列表已更新。因此,只有在再次单击添加按钮后才会填充列表。我试图以某种方式从对话框填充列表视图,但没有任何工作。 Notifydatasetchanged()没有用,而且changeCursor(游标)也没有用。

我认为这就是问题所在。

addButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addButtonPressed();
                testToast();
                myDb.insertRow(name,Integer.toString(time));


                    populateListViewFromDB(rootView);

                Toast.makeText(getActivity().getApplicationContext(), "Updated " + name + time + updateListview,
                           Toast.LENGTH_LONG).show();

            }});

   public void addButtonPressed(){
        FragmentManager fm = getActivity().getSupportFragmentManager();
        dialog_foods dialog1 = new dialog_foods();
        // dialog1.show(getSupportFragmentManager(), "my_dialog");
        //dialog1.show(ft, "acknowledgements");
        dialog1.setTargetFragment(this, 0);
        //dialog1.show(ft, "acknoledgments");
        dialog1.show(fm, "acknowledgements");

        //Toast.makeText(getActivity().getApplicationContext(), "Add Button Pressed",
          //      Toast.LENGTH_LONG).show();
        i++;




    }



private void populateListViewFromDB(View root){

        Cursor cursor = myDb.getAllRows();

        //Add others here.
        String [] fromFieldNames = new String[] {DBAdapter.KEY_ROWID, DBAdapter.KEY_NAME};
        int[] toViewIDs = new int[] {R.id.mMainLabel, R.id.mCityLabel};
        SimpleCursorAdapter myCursorAdapter;
        myCursorAdapter = new SimpleCursorAdapter(getActivity().getBaseContext(), R.layout.list_cell, cursor, fromFieldNames,toViewIDs,0);

        foodsListView = (ListView) root.findViewById(R.id.mFoodListView);
        foodsListView.setAdapter(myCursorAdapter);

        //myCursorAdapter.notifyDataSetChanged();

        myCursorAdapter.changeCursor(cursor);


    }

以防我在下面添加了我的其余代码。

这是我的片段,其中包含listview

public class foods_fragment extends Fragment implements dialog_foods.foodsDialogListener {
        Button addButton;
        Button deleteButton;
        int time;
        String name;
        boolean updateListview = false;

    int i;

    ListView foodsListView;
    //DB Stuff
    DBAdapter myDb;




        public static foods_fragment newInstance(){

            foods_fragment fragment = new foods_fragment();
            return fragment;
        }
    public foods_fragment(){


    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        //outState.putAll();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        final View rootView = inflater.inflate(R.layout.fragment_foods, container, false);


        openDB();
        populateListViewFromDB(rootView);




        foodsListView = (ListView) rootView.findViewById(R.id.mFoodListView);



/*
        foodsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


            }
        });   */




        addButton = (Button)rootView.findViewById(R.id.mAddButton);

        deleteButton = (Button)rootView.findViewById(R.id.mDeleteButton);

        deleteButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clearAll(rootView);

            }});

        addButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addButtonPressed();
                testToast();
                myDb.insertRow(name,Integer.toString(time));

                 populateListViewFromDB(rootView);

                Toast.makeText(getActivity().getApplicationContext(), "Updated " + name + time + updateListview,
                           Toast.LENGTH_LONG).show();

            }});






        return rootView;
    }

    public void addButtonPressed(){
        FragmentManager fm = getActivity().getSupportFragmentManager();
        dialog_foods dialog1 = new dialog_foods();
        // dialog1.show(getSupportFragmentManager(), "my_dialog");
        //dialog1.show(ft, "acknowledgements");
        dialog1.setTargetFragment(this, 0);
        //dialog1.show(ft, "acknoledgments");
        dialog1.show(fm, "acknowledgements");

        //Toast.makeText(getActivity().getApplicationContext(), "Add Button Pressed",
          //      Toast.LENGTH_LONG).show();
        i++;




    }





    @Override
    public void onDestroy() {
        super.onDestroy();

        closeDB();
    }

    private void openDB(){
        myDb = new DBAdapter(getActivity());
        myDb.open();

    }

    private void closeDB(){
        myDb.close();

    }

    public void clearAll(View root){

        myDb.deleteAll();

        populateListViewFromDB(root);
    }

    private void populateListViewFromDB(View root){

        Cursor cursor = myDb.getAllRows();

        //Add others here.
        String [] fromFieldNames = new String[] {DBAdapter.KEY_ROWID, DBAdapter.KEY_NAME};
        int[] toViewIDs = new int[] {R.id.mMainLabel, R.id.mCityLabel};
        SimpleCursorAdapter myCursorAdapter;
        myCursorAdapter = new SimpleCursorAdapter(getActivity().getBaseContext(), R.layout.list_cell, cursor, fromFieldNames,toViewIDs,0);

        foodsListView = (ListView) root.findViewById(R.id.mFoodListView);
        foodsListView.setAdapter(myCursorAdapter);

        //myCursorAdapter.notifyDataSetChanged();

        myCursorAdapter.changeCursor(cursor);


    }






    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        ((MainActivity) activity).onSectionAttached(2);
    }
    public void testToast(){
       // Toast.makeText(getActivity().getApplicationContext(), time + " " + name,
         //       Toast.LENGTH_LONG).show();
    }
    @Override
    public void nameTimeSubmit(int time, String name, boolean updateListview) {
        this.time = time;
        this.name = name;
        this.updateListview = updateListview;


       // View view = getView().findViewById(android.R.id.content);
        //populateListViewFromDB(view);


    }
}

如果你需要看到这个,我的对话框片段。

public class dialog_foods extends DialogFragment {

    private foodsDialogListener callback;

    public interface foodsDialogListener{
        public void nameTimeSubmit(int time, String name, boolean updateListview);
    }


    LayoutInflater inflater;
    View v;
    NumberPicker hour;
    NumberPicker min;
    NumberPicker sec;
    int time;
    EditText nameField;
    String name;
    boolean updateListview = false;




    @Override
    @NonNull
    public Dialog onCreateDialog(Bundle savedInstanceState){
        inflater = getActivity().getLayoutInflater();
        v = inflater.inflate(R.layout.fragment_add_food, null);

        hour = (NumberPicker) v.findViewById(R.id.mHoursNumberPickerFoods);
        hour.setMaxValue(24);
        hour.setMinValue(0);
        hour.setWrapSelectorWheel(true);
        min = (NumberPicker) v.findViewById(R.id.mMinutesNumberPickerFoods);
        min.setMaxValue(60);
        min.setMinValue(0);
        min.setWrapSelectorWheel(true);
        sec = (NumberPicker) v.findViewById(R.id.mSecondsNumberPickerFoods);
        sec.setMaxValue(90);
        sec.setMinValue(0);
        sec.setWrapSelectorWheel(true);
        nameField = (EditText) v.findViewById(R.id.mNameEditText);



        try{
            callback = (foodsDialogListener) getTargetFragment();
        } catch (ClassCastException e){
            throw new ClassCastException("calling Fragment must implement onTimeListener");
        }

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        builder.setView(v).setPositiveButton("OK", new DialogInterface.OnClickListener() {



            @Override
            public void onClick(DialogInterface dialog, int which) {


                int hours = hour.getValue();
                int minutes = min.getValue();
                int seconds = sec.getValue();

                name =  nameField.getText().toString();

                time  = hours * 3600000 + minutes * 60000 + seconds * 1000;

                updateListview = true;
                callback.nameTimeSubmit(time, name, updateListview);


/*
                MainActivity main;
                main = (MainActivity)getActivity();
                main.time = time; */

              //  Toast.makeText(getActivity().getApplicationContext(), "Time: " + time,
                //        Toast.LENGTH_LONG).show();

            }
        }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {



            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });

        return builder.create();
    }





}

0 个答案:

没有答案