java - Why can't my button gain focus unless I click another button first? -
i have 5 buttons on activity, 1 of close button finish() activity. clicking nothing unless click 1 of other buttons first , click it. i'm assuming problem lies somewhere in xml.
<linearlayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="3.05" android:orientation="vertical" > <linearlayout android:layout_width="wrap_content" android:layout_height="wrap_content" > <textview android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/sitename" android:textappearance="?android:attr/textappearancelarge" /> <edittext android:id="@+id/dialogsitename" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginleft="20dp" android:layout_weight="8.05" android:ems="10" /> </linearlayout> <linearlayout android:layout_width="wrap_content" android:layout_height="wrap_content" > <textview android:id="@+id/textview2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/siteaddress" android:textappearance="?android:attr/textappearancelarge" /> <edittext android:id="@+id/dialogaddress" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:ems="10" > </edittext> </linearlayout> <linearlayout android:layout_width="wrap_content" android:layout_height="wrap_content" > <textview android:id="@+id/textview3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/username" android:textappearance="?android:attr/textappearancelarge" /> <edittext android:id="@+id/dialogusername" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginleft="20dp" android:layout_weight="1" android:ems="10" /> </linearlayout> <linearlayout android:layout_width="wrap_content" android:layout_height="wrap_content" > <textview android:id="@+id/textview4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/password" android:textappearance="?android:attr/textappearancelarge" /> <edittext android:id="@+id/dialogpassword" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginleft="26dp" android:layout_weight="58.17" android:ems="10" /> </linearlayout> <linearlayout android:layout_width="wrap_content" android:layout_height="wrap_content" > <textview android:id="@+id/textview5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/port" android:textappearance="?android:attr/textappearancelarge" /> <edittext android:id="@+id/dialogport" android:layout_width="54dp" android:layout_height="wrap_content" android:layout_weight="1" android:ems="10" android:inputtype="number" /> <checkbox android:id="@+id/dialogpassive" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginleft="25dp" android:text="passive" /> </linearlayout> <linearlayout android:layout_width="wrap_content" android:layout_height="wrap_content" > <button android:id="@+id/testbtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margintop="20dp" android:text="test connection" /> <button android:id="@+id/savesite" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margintop="20dp" android:text="save" android:visibility="visible" /> <button android:id="@+id/closebtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margintop="20dp" android:text="close" /> <button android:id="@+id/connectbtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margintop="20dp" android:text="connect" /> </linearlayout> </linearlayout> <linearlayout android:layout_width="440dp" android:layout_height="401dp" android:orientation="vertical" > <listview android:id="@+id/sitelist" android:layout_width="match_parent" android:layout_height="320dp" > </listview> <button android:id="@+id/clrbtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="clear sites" /> </linearlayout>
code:
public class sitemanager extends activity { dbadapter mydb; public ftpclient mftpclient = null; public edittext sitename; public edittext address; public edittext username; public edittext password; public edittext port; public checkbox passive; public listview site_list; public textview tview; public button clr; public button test; public button savesite; public button close; public button connect; string _name; string _address; string _username; string _password; int _port; int _passive = 0; list<ftpsite> model = new arraylist<ftpsite>(); arrayadapter<ftpsite> adapter; public void oncreate(bundle icicle) { super.oncreate(icicle); setcontentview(r.layout.site_manager); site_list = (listview) findviewbyid(r.id.sitelist); adapter = new siteadapter(this, r.id.ftpsitename, r.layout.siterow, model); site_list.setadapter(adapter); site_list.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, final view view, int position, long id) { final ftpsite item = (ftpsite) parent .getitematposition(position); string tmpname = item.getname(); string tmpaddress = item.getaddress(); string tmpuser = item.getusername(); string tmppass = item.getpassword(); int tmpport = item.getport(); string tmp_port = integer.tostring(tmpport); int tmppassive = item.ispassive(); sitename.settext(tmpname); address.settext(tmpaddress); username.settext(tmpuser); password.settext(tmppass); port.settext(tmp_port); if (tmppassive == 1) { passive.setchecked(true); } else { passive.setchecked(false); } } }); sitename = (edittext) findviewbyid(r.id.dialogsitename); address = (edittext) findviewbyid(r.id.dialogaddress); username = (edittext) findviewbyid(r.id.dialogusername); password = (edittext) findviewbyid(r.id.dialogpassword); port = (edittext) findviewbyid(r.id.dialogport); passive = (checkbox) findviewbyid(r.id.dialogpassive); close = (button) findviewbyid(r.id.closebtn); close.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { finish(); } }); connect = (button) findviewbyid(r.id.connectbtn); clr = (button) findviewbyid(r.id.clrbtn); test = (button) findviewbyid(r.id.testbtn); test.setonclicklistener(new onclicklistener() { @override public void onclick(view arg0) { sitemanager(); } }); savesite = (button) findviewbyid(r.id.savesite); savesite.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { sitemanager(); } }); opendb(); displayrecords(); } private void opendb() { mydb = new dbadapter(this); mydb.open(); } @override protected void ondestroy() { // todo auto-generated method stub super.ondestroy(); closedb(); } private void closedb() { mydb.close(); } public void sitemanager() { connect.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { intent returnresult = new intent(); returnresult.putextra("ftpname", _name); returnresult.putextra("ftpaddress", _address); returnresult.putextra("ftpusername", _username); returnresult.putextra("ftppassword", _password); returnresult.putextra("ftpport", _port); setresult(result_ok, returnresult); finish(); } }); test.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { _name = sitename.gettext().tostring(); _address = address.gettext().tostring(); _username = username.gettext().tostring(); _password = password.gettext().tostring(); _port = integer.parseint(port.gettext().tostring()); if (passive.ischecked()) { _passive = 1; } else { _passive = 0; } boolean status = ftpconnect(_address, _username, _password, _port); ftpdisconnect(); if (status == true) { toast.maketext(sitemanager.this, "connection succesful", toast.length_long).show(); savesite.setvisibility(0); } else { toast.maketext(sitemanager.this, "connection failed:" + status, toast.length_long) .show(); } } }); savesite.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { _name = sitename.gettext().tostring(); _address = address.gettext().tostring(); _username = username.gettext().tostring(); _password = password.gettext().tostring(); _port = integer.parseint(port.gettext().tostring()); if (passive.ischecked()) { _passive = 1; } else { _passive = 0; } long newid = mydb.insertrow(_name, _address, _username, _password, 21, _passive); ftpsite tmpsite = new ftpsite(); tmpsite.setname(_name); tmpsite.setaddress(_address); tmpsite.setusername(_username); tmpsite.setpassword(_password); tmpsite.setport(_port); model.add(tmpsite); adapter.notifydatasetchanged(); toast.maketext(sitemanager.this, model.get(0).getname(), toast.length_short).show(); // displayrecords(); } }); clr.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { clearall(); } }); } public void displayrecords() { cursor cursor = mydb.getallrows(); displayrecordset(cursor); } protected void displayrecordset(cursor c) { // string msg = ""; if (c.movetofirst()) { { // int id = c.getint(0); _name = c.getstring(1); _address = c.getstring(2); _username = c.getstring(3); _password = c.getstring(4); _port = c.getint(5); ftpsite sitesfromdb = new ftpsite(); sitesfromdb.setname(_name); sitesfromdb.setaddress(_address); sitesfromdb.setusername(_username); sitesfromdb.setaddress(_password); sitesfromdb.setport(_port); sitesfromdb.setpassive(_passive); model.add(sitesfromdb); adapter.notifydatasetchanged(); } while (c.movetonext()); } c.close(); } public void clearall() { mydb.deleteall(); adapter.notifydatasetchanged(); } public boolean ftpconnect(string host, string username, string password, int port) { try { mftpclient = new ftpclient(); // connecting host mftpclient.connect(host, port); // check reply code, if positive mean connection success if (ftpreply.ispositivecompletion(mftpclient.getreplycode())) { // login using username & password boolean status = mftpclient.login(username, password); mftpclient.enterlocalpassivemode(); return status; } } catch (exception e) { // log.d(tag, "error: not connect host " + host ); } return false; } public boolean ftpdisconnect() { try { mftpclient.logout(); mftpclient.disconnect(); return true; } catch (exception e) { // log.d(tag, // "error occurred while disconnecting ftp server."); } return false; } class siteadapter extends arrayadapter<ftpsite> { private final list<ftpsite> objects; private final context context; public siteadapter(context context, int resource, int textviewresourceid, list<ftpsite> objects) { super(context, r.id.ftpsitename, r.layout.siterow, objects); this.context = context; this.objects = objects; } /** @return number of items in */ public int getcount() { return objects.size(); } public boolean areallitemsselectable() { return false; } /** use array index unique id. */ public long getitemid(int position) { return position; } public view getview(int position, view convertview, viewgroup parent) { layoutinflater inflater = (layoutinflater) context .getsystemservice(context.layout_inflater_service); view rowview = inflater.inflate(r.layout.siterow, parent, false); textview textview = (textview) rowview .findviewbyid(r.id.ftpsitename); textview.settext(objects.get(position).getname()); return (rowview); } } }
Comments
Post a Comment