I am trying to have the user enter text into a edit text. When the user hits the enter key I want to take the text from the edit text and add it to my ArrayList. I am having issues controlling the keyEvent from the edit text.
public class MainActivity extends Activity {
ArrayList<String> array_list;
ListAdapter lv;
ListView list;
EditText edittext;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
array_list = new ArrayList<String>();
edittext = (EditText) findViewById(R.id.textView);
lv = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, array_list);
list = (ListView) findViewById(R.id.lstView);
list.setAdapter(lv);
edittext.requestFocus();
edittext.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
boolean handled = false;
if (actionId == EditorInfo.IME_ACTION_GO) {
array_list.add(edittext.getText().toString());
edittext.setText("");
edittext.requestFocus();
}
return handled;
}
});
}
}
So what I need to happen is when the user presses enter, it takes the text from the field and adds it to an arrayList. Then it erases the text from the textfield and gives it focus again.