java - ArrayAdapter is not updating -
i have started working on android app (i pretty new platform). trying write simple todo app larn how array adapters work.
--so 1 time come in text in mytextview , press "enter", want text show in list view below. shown in image nil gets updated in list view. doing wrong here? please help.
test.java:
package com.niranjanbajgai.test; import android.app.activity; import android.os.bundle; import android.view.keyevent; import android.view.menu; import android.view.menuitem; import android.view.view; import android.widget.arrayadapter; import android.widget.edittext; import android.widget.listview; import java.util.arraylist; public class test extends activity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_test); //get reference ui widget. final listview mylistview = (listview) findviewbyid(r.id.mylistview); final edittext myedittext = (edittext) findviewbyid(r.id.myedittext); //create array list final arraylist<string> myarraylist = new arraylist<string>(); //create array adapter bind array list view final arrayadapter<string> myarrayadapter = new arrayadapter<string>( this, android.r.layout.simple_list_item_1, myarraylist); mylistview.setadapter(myarrayadapter); myedittext.setonkeylistener(new view.onkeylistener() { @override public boolean onkey(view view, int keycode, keyevent keyevent) { if(keyevent.getaction() == keyevent.action_down){ if(keycode == keyevent.keycode_enter){ myarraylist.add(0, myedittext.gettext().tostring()); myarrayadapter.notifydatasetchanged(); myedittext.settext(""); } homecoming true; } homecoming false; } }); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.test, menu); homecoming true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); if (id == r.id.action_settings) { homecoming true; } homecoming super.onoptionsitemselected(item); } }
activity_test.xml:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".test"> <edittext android:id="@+id/myedittext" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/additemhint"/> <listview android:id="@+id/mylistview" android:layout_width="match_parent" android:layout_height="wrap_content"/> </linearlayout>
you want validate content of edittext bottom right button of keyboard right?
currently, button allows add together new line. think onkeylistener called when using hardware keyboard, that's why it's never called.
to alter bottom right button, need alter imeoptions of edittext actiondone example.
<edittext android:id="@+id/myedittext" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/additemhint" android:imeoptions="actiondone" />
and can remove onkeylistener editoractionlistener :
edittext.setoneditoractionlistener(new oneditoractionlistener() { @override public boolean oneditoraction(textview v, int actionid, keyevent event) { boolean handled = false; if (actionid == editorinfo.ime_action_done) { myarraylist.add(0, myedittext.gettext().tostring()); myarrayadapter.notifydatasetchanged(); myedittext.settext(""); handled = true; } homecoming handled; } });
for more details on edittext , keyboard, can read documentation
the rest of code looks , should work. replace height of listview match_parent (a listview should not wrapped).
java android android-arrayadapter
No comments:
Post a Comment