通过Dialog更新片段列表视图中显示的数据库条目

时间:2015-04-11 21:56:18

标签: android android-fragments android-listview android-alertdialog sqliteopenhelper

我有一个显示Fragment的{​​{1}},其Listview使用Database填充了条目。我目前有SQLiteOpenHelper,允许删除或编辑条目。 ContextMenu选项用于运行DELETE中的条目,但Database选项仅接受EDIT中输入的字段,并向{{1}添加新条目}}。我希望能够将对话框提供的值归因于Dialog中已存在的条目。提前感谢任何意见或建议。

片段:

ListView

数据库:

Database

LiftSave:

   public static class FragmentS extends Fragment {

    private ListView saveListView;
    private List<LiftSave> LiftSaves = new ArrayList<LiftSave>();
    private static final int EDIT = 0, DELETE = 1;

    LiftSave longClickedItemLiftSave;
    DatabaseHandler dbHandler;
    ArrayAdapter<LiftSave> saveAdapter;

    public FragmentS() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.listview_s,
                container, false);
        saveListView = (ListView) rootView.findViewById(R.id.saveListView);
        registerForContextMenu(saveListView);
        DatabaseHandler dbHandler;
        dbHandler = new DatabaseHandler (getActivity().getApplicationContext());
        if (dbHandler.getLiftSavesCount() != 0)
            LiftSaves.addAll(dbHandler.getAllLiftSaves());

        populateList();

        saveListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                longClickedItemLiftSave = (LiftSave) parent.getItemAtPosition(position);
                return false;
            }
        });

        return rootView;
    }

    private void populateList() {
        saveAdapter = new SaveListAdapter();
        saveListView.setAdapter(saveAdapter);
    }


    public class SaveListAdapter extends ArrayAdapter<LiftSave> {
            public SaveListAdapter() {
                super(getActivity(), R.layout.listview_item, LiftSaves);
            }

            @Override
            public View getView(int position, View view, ViewGroup parent) {
                if (view == null)
                    view = getActivity().getLayoutInflater().inflate(R.layout.listview_item, parent, false);

                LiftSave currentLiftSave = LiftSaves.get(position);

                TextView liftName = (TextView) view.findViewById(R.id.liftName);
                liftName.setText(currentLiftSave.getLiftName());
                TextView maxValue = (TextView) view.findViewById(R.id.maxValue);
                maxValue.setText(currentLiftSave.getMaxValue());
                TextView liftNotes = (TextView) view.findViewById(R.id.liftNotes);
                liftNotes.setText(currentLiftSave.getLiftNotes());
                TextView weightAndReps = (TextView) view.findViewById(R.id.weightAndReps);
                weightAndReps.setText(currentLiftSave.getRepsAndWeight());
                TextView date = (TextView) view.findViewById(R.id.todayDate);
                date.setText(currentLiftSave.getTodayDate());

                return view;
            }

    }
    public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, view, menuInfo);

        menu.setHeaderIcon(R.drawable.pencil_icon);
        menu.setHeaderTitle("Save Options");
        menu.add(Menu.NONE, EDIT, menu.NONE, "Edit Save");
        menu.add(Menu.NONE, DELETE, menu.NONE, "Delete Save");
    }

    public boolean onContextItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case EDIT:
                final View dialogViewEdit = LayoutInflater.from(this.getActivity()).inflate(R.layout.edit_save, null, false);
                final AlertDialog builderE = new AlertDialog.Builder(this.getActivity()).create();

                TextView liftName = (TextView) getActivity().findViewById(R.id.liftName);
                TextView maxValue = (TextView) getActivity().findViewById(R.id.maxValue);
                TextView weightAndReps = (TextView) getActivity().findViewById(R.id.weightAndReps);
                TextView liftNotes = (TextView) getActivity().findViewById(R.id.liftNotes);
                TextView date = (TextView) getActivity().findViewById(R.id.todayDate);

                final EditText editName = (EditText) dialogViewEdit.findViewById(R.id.liftNameED);
                editName.setText(liftName.getText().toString());
                EditText editNotes = (EditText) dialogViewEdit.findViewById(R.id.liftNotesED);
                editNotes.setText(liftNotes.getText().toString());
                EditText editWR = (EditText) dialogViewEdit.findViewById(R.id.txtWRED);
                editWR.setText(weightAndReps.getText().toString());
                EditText editMax = (EditText) dialogViewEdit.findViewById(R.id.txtMaxED);
                editMax.setText(maxValue.getText().toString());
                EditText editDate = (EditText) dialogViewEdit.findViewById(R.id.txtDateED);
                editDate.setText(date.getText().toString());

                Button cancel =(Button) dialogViewEdit.findViewById(R.id.btnCancel);
                Button save =(Button) dialogViewEdit.findViewById(R.id.btnSave);
                cancel.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                    builderE.dismiss();
                    }
                });

                save.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {

                        final EditText editName = (EditText) dialogViewEdit.findViewById(R.id.liftNameED);
                        EditText editNotes = (EditText) dialogViewEdit.findViewById(R.id.liftNotesED);
                        EditText editWR = (EditText) dialogViewEdit.findViewById(R.id.txtWRED);
                        EditText editMax = (EditText) dialogViewEdit.findViewById(R.id.txtMaxED);
                        EditText editDate = (EditText) dialogViewEdit.findViewById(R.id.txtDateED);
                        //Problem
                        dbHandler = new DatabaseHandler(getActivity().getApplicationContext());
                        LiftSave liftSave = new LiftSave(dbHandler.getLiftSavesCount(), String.valueOf(editName.getText()), String.valueOf(editMax.getText()), String.valueOf(editNotes.getText()), String.valueOf(editWR.getText()), String.valueOf(editDate.getText()));
                        LiftSaves.add(liftSave);
                        dbHandler.getLiftSave(longClickedItemLiftSave.getId());
                        dbHandler.updateLiftSave(longClickedItemLiftSave);
                        saveAdapter.notifyDataSetChanged();
                        //Problem
                    }
                });
                builderE.setView(dialogViewEdit);
                builderE.show();
                break;
            case DELETE:
                AlertDialog.Builder builder = new AlertDialog.Builder(this.getActivity());
                builder.setMessage("Are you sure you want to delete this save?")
                        .setCancelable(false)
                        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                dbHandler = new DatabaseHandler(getActivity().getApplicationContext());
                                dbHandler.deleteLiftSave(longClickedItemLiftSave);
                                saveAdapter.remove(longClickedItemLiftSave);
                            }
                        })
                        .setNegativeButton("No", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                dialog.cancel();
                            }
                        });
                AlertDialog dialogD = builder.create();
                dialogD.show();
                break;
                            }
                            return super.
                            onContextItemSelected(item);
                        }
        }

1 个答案:

答案 0 :(得分:1)

编辑:在我看到你的LiftSave课程后解决它的一种方法。 使用此修复程序,您根本不必修改DatabaseHandler类:

                //Problem
                dbHandler = new DatabaseHandler(getActivity().getApplicationContext());

                //pass in the existing ID instead of dbHandler.getLiftSavesCount()
                LiftSave liftSave = new LiftSave(longClickedItemLiftSave.getId() , String.valueOf(editName.getText()), String.valueOf(editMax.getText()), String.valueOf(editNotes.getText()), String.valueOf(editWR.getText()), String.valueOf(editDate.getText()));

                //LiftSaves.add(liftSave); //Don't add to the list here
                //dbHandler.getLiftSave(longClickedItemLiftSave.getId()); //looks like this is not needed

                //pass in the LiftSave object with existing ID and new values
                dbHandler.updateLiftSave(liftSave);

                LiftSaves.clear(); //Remove all entries from the list
                LiftSaves.addAll(dbHandler.getAllLiftSaves()); //Re-add all records to List including modified entry
                saveAdapter.notifyDataSetChanged();
                //Problem

请注意,longClickedItemLiftSave.getId()作为新id的{​​{1}}传入,因此当您将其传递到LiftSave时,它会更新原始条目。

最初提出的解决方案,另一种解决方法:

首先,让您的updateLiftSave()方法以updateLiftSave()为参数,并使用id更新记录:

id

然后,在更新时,使用原始public int updateLiftSave(LiftSave liftSave, int id) { SQLiteDatabase db = getWritableDatabase(); ContentValues values = new ContentValues(); values.put(KEY_LIFTNAME, liftSave.getLiftName()); values.put(KEY_MAXVALUE, liftSave.getMaxValue()); values.put(KEY_LIFTNOTES, liftSave.getLiftNotes()); values.put(KEY_REPSANDWEIGHT, liftSave.getRepsAndWeight()); values.put(KEY_TODAYDATE, liftSave.getTodayDate()); //int rowsAffected = db.update(TABLE_SAVES, values, KEY_ID + "=?", new String[] { String.valueOf(liftSave.getId()) }); int rowsAffected = db.update(TABLE_SAVES, values, KEY_ID + "=?", new String[] { String.valueOf(id) }); db.close(); return rowsAffected; } 和新值:

id

更新:您可能还需要添加一个调用来清除 //Problem dbHandler = new DatabaseHandler(getActivity().getApplicationContext()); LiftSave liftSave = new LiftSave(dbHandler.getLiftSavesCount(), String.valueOf(editName.getText()), String.valueOf(editMax.getText()), String.valueOf(editNotes.getText()), String.valueOf(editWR.getText()), String.valueOf(editDate.getText())); //LiftSaves.add(liftSave); //don't add it do the list //dbHandler.getLiftSave(longClickedItemLiftSave.getId()); //looks like this is not needed dbHandler.updateLiftSave(liftSave, longClickedItemLiftSave.getId()); //pass in the new values and the existing ID LiftSaves.clear(); //Remove all entries from the list LiftSaves.addAll(dbHandler.getAllLiftSaves()); //Re-add all records to List including modified entry saveAdapter.notifyDataSetChanged(); //Problem 中的列表,以确保您永远不会在列表中获得重复的条目,并且列表始终镜像当前数据库中的条目:

onCreateView()

更新2:关于您的上一期,请尝试使用@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //........ if (dbHandler.getLiftSavesCount() != 0){ LiftSaves.clear(); //Clear the list just in case LiftSaves.addAll(dbHandler.getAllLiftSaves()); } 列表而不是LiftSaves AdapterView:

parent