I am trying to create an application with android. I use an EditText and want to get the text from it after the user presses the "enter" key on its keyboard. This is the XML code for the editText:
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/nameTxt"
android:ems="8"
android:layout_below="@+id/textView"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="45dp"
android:layout_toRightOf="@+id/textView2" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView"
android:layout_below="@+id/textView2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="36dp"
android:layout_above="@+id/btnClr" />
In my .Java class I have these relevant rows:
ArrayList<String> listItems=new ArrayList<String>();
ArrayAdapter<String> adapter;
And that function:
public void openAddFriend(View view) {
String txt;
final EditText editName;
editName = (EditText)findViewById(R.id.nameTxt);
ListView lst;
lst = (ListView)findViewById(R.id.listView);
txt = editName.getText().toString();
listItems.add(txt);
adapter=new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,listItems);
lst.setAdapter(adapter);
}
My goal is to get the string from the EditText and inserts it into a listView. I've tried using OnKeyListener but it didn't work. Can someone give me a direction to a solution? Thank you in advance
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.