EditText到ListView项的值onItemLongCl

EditText到ListView项的值onItemLongCl

本文介绍了设置对话框中的EditText到ListView项的值onItemLongClick的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我有一个的ListView 中的片段具有文本菜单编辑和删除条目。在文本菜单启动一个对话框修改选项的选择。我想设置的是对话框的到字符串的EditText >在的ListView 通过 onItemLongClick 值,但一直未能如愿。先谢谢了。

片段:

 公共静态类片段扩展片段{    私人的ListView saveListView;
    私人列表< LiftSave> LiftSaves =新的ArrayList< LiftSave>();
    私有静态最终诠释EDIT = 0,DELETE = 1;    LiftSave longClickedItemLiftSave;
    数据库处理器dbHandler;
    ArrayAdapter< LiftSave> saveAdapter;    公共片段(){    }    @覆盖
    公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
                             捆绑savedInstanceState){
        查看rootView = inflater.inflate(R.layout.listview_s,
                集装箱,FALSE);
        saveListView =(ListView控件)rootView.findViewById(R.id.saveListView);
        registerForContextMenu(saveListView);
        数据库处理器dbHandler;
        dbHandler =新数据库处理器(getActivity()getApplicationContext());
        如果(dbHandler.getLiftSavesCount()!= 0)
            LiftSaves.clear();
            LiftSaves.addAll(dbHandler.getAllLiftSaves());        populateList();        saveListView.setOnItemLongClickListener(新AdapterView.OnItemLongClickListener(){
            @覆盖
            公共布尔onItemLongClick(适配器视图<>母公司,观景,INT位置,长的id){
                longClickedItemLiftSave = LiftSaves.get(位置);
                返回false;
            }
        });        返回rootView;
    }    私人无效populateList(){
        saveAdapter =新SaveListAdapter();
        saveListView.setAdapter(saveAdapter);
    }
    公共类SaveListAdapter扩展ArrayAdapter< LiftSave> {
            公共SaveListAdapter(){
                超级(getActivity(),R.layout.listview_item,LiftSaves);
            }            @覆盖
            公共查看getView(INT位置,查看视图的ViewGroup父){
                如果(查看== NULL)
                    鉴于= getActivity()getLayoutInflater()膨胀(R.layout.listview_item,父母,假)。                LiftSave currentLiftSave = LiftSaves.get(位置);                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的日期=(TextView中)view.findViewById(R.id.todayDate);
                date.setText(currentLiftSave.getTodayDate());                返回视图。
            }    }
    公共无效onCreateContextMenu(文本菜单菜单,查看视图,ContextMenu.ContextMenuInfo menuInfo){
        super.onCreateContextMenu(菜单,查看menuInfo);        menu.setHeaderIcon(R.drawable.pencil_icon);
        menu.setHeaderTitle(保存选项);
        menu.add(Menu.NONE,编辑,menu.NONE,编辑保存);
        menu.add(Menu.NONE,DELETE,menu.NONE,删除保存);
    }    公共布尔onContextItemSelected(菜单项项){
        开关(item.getItemId()){
            案例编辑:
                最后查看dialogViewEdit = LayoutInflater.from(this.getActivity())膨胀(R.layout.edit_save,空,假的)。
                最后AlertDialog builderE =新AlertDialog.Builder(this.getActivity())创建()。                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的日期=(TextView中)getActivity()findViewById(R.id.todayDate)。                的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());                按钮取消=(按钮)dialogViewEdit.findViewById(R.id.btnCancel);
                按钮保存=(按钮)dialogViewEdit.findViewById(R.id.btnSave);
                cancel.setOnClickListener(新View.OnClickListener(){
                    公共无效的onClick(视图v){
                    builderE.dismiss();
                    }
                });                save.setOnClickListener(新View.OnClickListener(){
                    公共无效的onClick(视图v){                        的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);                        dbHandler =新数据库处理器(getActivity()getApplicationContext());
                        LiftSave liftSave =新LiftSave(longClickedItemLiftSave.getId(),将String.valueOf(editName.getText()),将String.valueOf(editMax.getText()),将String.valueOf(editNotes.getText()),将String.valueOf(editWR .getText()),将String.valueOf(editDate.getText()));
                        dbHandler.updateLiftSave(liftSave);
                        LiftSaves.clear();
                        LiftSaves.addAll(dbHandler.getAllLiftSaves());
                        saveAdapter.notifyDataSetChanged();                    }
                });
                builderE.setView(dialogViewEdit);
                builderE.show();
                打破;
            案例DELETE:
                AlertDialog.Builder建设者=新AlertDialog.Builder(this.getActivity());
                builder.setMessage(你确定要删除这个救?)
                        .setCancelable(假)
                        .setPositiveButton(是,新DialogInterface.OnClickListener(){
                            公共无效的onClick(DialogInterface对话,诠释的id){
                                dbHandler =新数据库处理器(getActivity()getApplicationContext());
                                dbHandler.deleteLiftSave(longClickedItemLiftSave);
                                saveAdapter.remove(longClickedItemLiftSave);
                            }
                        })
                        .setNegativeButton(否,新DialogInterface.OnClickListener(){
                            公共无效的onClick(DialogInterface对话,诠释的id){
                                dialog.cancel();
                            }
                        });
                AlertDialog dialogD = builder.create();
                dialogD.show();
                打破;
                            }
                            返回超。
                            onContextItemSelected(项目);
                        }
        }


解决方案

看来,如果 longClickedItemLiftSave 是越来越正确填充,你可以在code更改为这它会解决它:

 公共布尔onContextItemSelected(菜单项项){
        开关(item.getItemId()){
            案例编辑:
                最后查看dialogViewEdit = LayoutInflater.from(this.getActivity())膨胀(R.layout.edit_save,空,假的)。
                最后AlertDialog builderE =新AlertDialog.Builder(this.getActivity())创建()。                /* 不需要
                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的日期=(TextView中)getActivity()findViewById(R.id.todayDate)。                 * /
                //使用来自longClickedItemLiftSave值
                的EditText editName =(EditText上)dialogViewEdit.findViewById(R.id.liftNameED);
                editName.setText(longClickedItemLiftSave.getLiftName());
                的EditText editNotes =(EditText上)dialogViewEdit.findViewById(R.id.liftNotesED);
                editNotes.setText(longClickedItemLiftSave.getLiftNotes());
                的EditText editWR =(EditText上)dialogViewEdit.findViewById(R.id.txtWRED);
                editWR.setText(longClickedItemLiftSave.getRepsAndWeight());
                的EditText editMax =(EditText上)dialogViewEdit.findViewById(R.id.txtMaxED);
                editMax.setText(longClickedItemLiftSave.getMaxValue());
                的EditText editDate =(EditText上)dialogViewEdit.findViewById(R.id.txtDateED);
                editDate.setText(longClickedItemLiftSave.getTodayDate());

I have a ListView within a Fragment that has a ContextMenu to edit and delete entries. The ContextMenu launches a Dialog on selection of the EDIT option. I'd like to set the EditText's of that Dialog to the String values of the item selected via onItemLongClick on the ListView, but have been unable to do so. Thanks in advance.

Fragment:

    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.clear();
            LiftSaves.addAll(dbHandler.getAllLiftSaves());

        populateList();

        saveListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                longClickedItemLiftSave = LiftSaves.get(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);

                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) {

                        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);

                        dbHandler = new DatabaseHandler(getActivity().getApplicationContext());
                        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()));
                        dbHandler.updateLiftSave(liftSave);
                        LiftSaves.clear();
                        LiftSaves.addAll(dbHandler.getAllLiftSaves());
                        saveAdapter.notifyDataSetChanged();

                    }
                });
                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);
                        }
        }
解决方案

It seems that if longClickedItemLiftSave is getting populated correctly, you could change the code to this and it would fix it:

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();

                /* not needed
                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);

                 */


                //Use values from longClickedItemLiftSave
                EditText editName = (EditText) dialogViewEdit.findViewById(R.id.liftNameED);
                editName.setText(longClickedItemLiftSave.getLiftName());
                EditText editNotes = (EditText) dialogViewEdit.findViewById(R.id.liftNotesED);
                editNotes.setText(longClickedItemLiftSave.getLiftNotes());
                EditText editWR = (EditText) dialogViewEdit.findViewById(R.id.txtWRED);
                editWR.setText(longClickedItemLiftSave.getRepsAndWeight());
                EditText editMax = (EditText) dialogViewEdit.findViewById(R.id.txtMaxED);
                editMax.setText(longClickedItemLiftSave.getMaxValue());
                EditText editDate = (EditText) dialogViewEdit.findViewById(R.id.txtDateED);
                editDate.setText(longClickedItemLiftSave.getTodayDate());

这篇关于设置对话框中的EditText到ListView项的值onItemLongClick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 18:44