Search code examples
androidlistviewandroid-edittextandroid-arrayadapternotifydatasetchanged

Android: Update ListView from EditText


friends.

I have an app module that implements State pattern. Idea is: 1. User enter the Date --> 2. Screen changes to a ne state: dynamically inflates ListView and asks user to fill the list with some data

Problem is that when I call notifyDataSetChange() from update method 2nd time, EditText view doens't want to do its functions anymore.

This is a string from a logcat, that I think contains key of my problem.

09-10 10:33:38.937 26954-26954/com.example.android.turtleinfo D/ListView: change accessibility focus position = 0

Here are codes for everything.

1. My State Pattern Super Class

public abstract class ScreenState
    {

        public BuilderActivity activity;



        public ScreenState(BuilderActivity activity) {
            this.activity = activity;
            //initializeScreen(activity);
        }

        public abstract void initializeScreen(Activity activity);
        public abstract void switchState();
        public abstract void updateText(View view);


        public void addTextWatcher(EditText text) {
            text.setOnEditorActionListener(new TextView.OnEditorActionListener() {
                @Override
                public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                    if (actionId == EditorInfo.IME_ACTION_DONE) {

                        activity.state.updateText(v);

                        return true;
                    }
                    return false;
                }
            });
        }


    }

2. Implementation of initialize screen and updateData methods

@Override
    public void initializeScreen(Activity activity) {

        ((BuilderActivity) activity).editText.setHint("Name");
        addTextWatcher(((BuilderActivity) activity).editText);
        textView = (TextView)activity.getLayoutInflater().inflate(R.layout.schedule_text_style, null);
        textView.setHeight(100);
        screenText = "End Date: " + new SimpleDateFormat("MMMM dd, yyyy", Locale.ENGLISH).format(((BuilderActivity) activity).data.getEndDate())+"\n\n";

        textView.setText(screenText+"\n");
        ((BuilderActivity) activity).scheduleLayout.addView(textView);

        ((BuilderActivity) activity).data.setNames(new ArrayList<String>());

        lv = (ListView)activity.getLayoutInflater().inflate(R.layout.listview_text_style, null);
        listItems = new ArrayList<>();

        adapt = new ArrayAdapter<>(super.activity, R.layout.schedule_text_style, listItems);
        lv.setAdapter(adapt);
        ((BuilderActivity) activity).scheduleLayout.addView(lv);


    }

@Override
public void updateText(View view) {

    String text = activity.editText.getText().toString();

    if (text.length() >= 2) {
        if (!activity.data.names.contains(text)) {
            activity.data.addToList(text);



            listItems.add(text);
            adapt.notifyDataSetChanged();

            activity.editText.setText("");
            counter++;
            activity.editText.setText("");

        } else {
            Toast.makeText(activity, "same names are not allowed",
                Toast.LENGTH_LONG).show();
            ((BuilderActivity) activity).editText.setText("");
        }
    }
    else
    {
        Toast.makeText(activity, "name should contain 2 or more symbols",
                Toast.LENGTH_LONG).show();
        ((BuilderActivity) activity).editText.setText("");
    }

}

This is how it looks on device.

1. First insertion is ok

enter image description here

2. And second and further insertionas are impossible. OK softkeyboard button switches first to NEXT buttonenter image description here

3. If I click next, EditText looses focus, nothing happens, but NEXT button Switches to '/n' button.

enter image description here

So, please, if anyone know, what am I doing worng, help!

Thanks in advance!


Solution

  • Your problem is in

    public void addTextWatcher(EditText text)
    

    function.

    Look at the android:imeActionId and android:imeOptions attributes, plus the setOnEditorActionListener() method, all on TextView/Edittext.

    You can also change the text of the keyboard button by using this.

    mEditText.setImeActionLabel("Your text", KeyEvent.KEYCODE_ENTER);