Drunken Daddy
Drunken Daddy

Reputation: 8001

How to get the values of all the elements in a list view when a button outside the listview is pressed

Each row of my listview consists of a TextView and a EditText and I've a button outside the ListView. When that button is clicked, I want to retrieve all the ExitText Values.

I've generated the ListView inside a DialogFragment. It is the saveButton onClick I'm trying to accomplish. Here's my code. Any help would be appreciated.

public class SubVitalsEntryDialogFragment extends DialogFragment {

    ArrayList<SubVitals> subVitalList = new ArrayList<SubVitals>();
    ListView mylist;
    Button saveButton;
    Button cancelButton;
    EditText entryEditText;

    public static SubVitalsEntryDialogFragment newInstance(ArrayList<SubVitals> sub) {
        SubVitalsEntryDialogFragment f = new SubVitalsEntryDialogFragment();

        // Supply num input as an argument.
        Bundle args = new Bundle();
        args.putParcelableArrayList("subVitalList", sub);
        f.setArguments(args);
        return f;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.subvitals_dialog_layout, null, false);
        mylist = (ListView) view.findViewById(R.id.vitalEntryListView);
        saveButton = (Button) view.findViewById(R.id.entrySaveButton);
        cancelButton = (Button) view.findViewById(R.id.entryCancelButton);
        //getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        getDialog().setTitle("Enter your vitals");

        cancelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getDialog().dismiss();
            }
        });

        saveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ///??????????

            }
        });
        return view;
    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {

        super.onActivityCreated(savedInstanceState);
        subVitalList = getArguments().getParcelableArrayList("subVitalList");
        ArrayAdapter<SubVitals> adapter = new MyListAdapter(subVitalList);
        mylist.setAdapter(adapter);

    }

    private class MyListAdapter extends ArrayAdapter<SubVitals>{

        public MyListAdapter(ArrayList<SubVitals> subVitalList){

            super(getActivity(), R.layout.subvitals_dialog_layout, subVitalList);
            Log.d("Tag", "super okay");
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View itemView = convertView;
            if (itemView == null)
                itemView = LayoutInflater.from(this.getContext()).inflate(R.layout.sub_vital_template, parent, false);

            String currentVital = subVitalList.get(position).getSubVitalName();

            //Fill the view

            TextView vitalText = (TextView) itemView.findViewById((R.id.vitalEntryTextView));
            vitalText.setText(currentVital);

            return itemView;
        }
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return subVitalList.size();
        }
    }

}

This is what it looks like

--------------EDIT------------------

public class SubVitalsEntryDialogFragment extends DialogFragment {

    ArrayList<SubVitals> subVitalList = new ArrayList<SubVitals>();
    ListView mylist;
    Button saveButton;
    Button cancelButton;
    EditText entryEditText;
    List<String> vitalsEntered = new ArrayList<>();
    private HashMap<String, String> textValues = new HashMap<String, String>();

    public static SubVitalsEntryDialogFragment newInstance(ArrayList<SubVitals> sub) {
        SubVitalsEntryDialogFragment f = new SubVitalsEntryDialogFragment();

        // Supply num input as an argument.
        Bundle args = new Bundle();
        args.putParcelableArrayList("subVitalList", sub);
        f.setArguments(args);
        return f;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, final ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.subvitals_dialog_layout, null, false);
        mylist = (ListView) view.findViewById(R.id.vitalEntryListView);
        saveButton = (Button) view.findViewById(R.id.entrySaveButton);
        cancelButton = (Button) view.findViewById(R.id.entryCancelButton);
        //getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        getDialog().setTitle("Enter your vitals");

        cancelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getDialog().dismiss();
            }
        });

        saveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d("SAVE INSIDE","OK");
                for (Map.Entry<String,String> entry : textValues.entrySet()) {
                    String key = entry.getKey();
                    String value = entry.getValue();
                    Log.d("HASHCHECK","Key: " + key + " Value" + value);
                    // do stuff
                }


            }
        });
        return view;
    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {

        super.onActivityCreated(savedInstanceState);
        subVitalList = getArguments().getParcelableArrayList("subVitalList");
        ArrayAdapter<SubVitals> adapter = new MyListAdapter(subVitalList);
        mylist.setAdapter(adapter);

    }

    private class MyListAdapter extends ArrayAdapter<SubVitals>{



        public MyListAdapter(ArrayList<SubVitals> subVitalList){

            super(getActivity(), R.layout.subvitals_dialog_layout, subVitalList);
            Log.d("Tag", "super okay");
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            //return super.getView(position, convertView, parent);
            //Making sure we've a view to work with(may have been given null
            Log.d("TAG","Inside get view");
            View itemView = convertView;
            boolean convertViewWasNull = false;

            if (itemView == null){
                itemView = LayoutInflater.from(this.getContext()).inflate(R.layout.sub_vital_template, parent, false);
            convertViewWasNull = true;
            }

            TextView vitalText = (TextView) itemView.findViewById((R.id.vitalEntryTextView));
            EditText vitalEditText = (EditText) itemView.findViewById(R.id.vitalEntryEditText);

            if(convertViewWasNull ){
                vitalEditText.addTextChangedListener(new GenericTextWatcher(vitalEditText));
            }

            String currentVital = subVitalList.get(position).getSubVitalName();

            //Fill the view

            vitalText.setText(currentVital);
            return itemView;

        }
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return subVitalList.size();
        }

        private class GenericTextWatcher implements TextWatcher {

            private View view;
            private GenericTextWatcher(View view) {
                this.view = view;
            }

            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {}

            public void afterTextChanged(Editable editable) {

                String text = editable.toString();
                //save the value for the given tag :
                textValues.put(view.getTag().toString(), editable.toString());
            }
        }

    }

}

Upvotes: 2

Views: 95

Answers (1)

Kiril Aleksandrov
Kiril Aleksandrov

Reputation: 2591

Do not use a ListView. It is not designed to be used for such purposes. Rows from the ListView are recycled and it is possible to lose data that has already been entered when scrolling. Use LinearLayout instead. This way you will have a direct access to the views in it.

Upvotes: 3

Related Questions