android - Why is my CustomAdapter not being used? -
i created class called ftpsite getters , setters name,address,username,password,port,passive mode. i'm trying create site manager using custom adapter show site names i'm getting package name , numbers list item.
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; boolean _passive; list<ftpsite> model = new arraylist<ftpsite>(); siteadapter adapter; public void oncreate(bundle icicle) { super.oncreate(icicle); setcontentview(r.layout.site_manager); site_list = (listview) findviewbyid(r.id.sitelist); adapter = new siteadapter(); site_list.setadapter(adapter); 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); 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(); } }); close.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { } }); 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()); _passive = false; if (passive.ischecked()) { _passive = true; } 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()); long newid = mydb.insertrow(_name, _address, _username, _password, 21); 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, tmpsite.getname(), toast.length_short).show(); // shows correct name // 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); msg += "id=" + id + "\n"; msg += ", name=" + _name + "\n"; msg += ", address=" + _address + "\n"; msg += ", username=" + _username + "\n"; msg += ", password=" + _password + "\n"; msg += ", port=" + _port + "\n"; } while (c.movetonext()); } c.close(); } public void clearall() { mydb.deleteall(); } 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> { public siteadapter(context context, int resource, int textviewresourceid, list<t> objects) { super(context,resource,textviewresourceid,objects); } } public view getview(int position, view convertview, viewgroup parent) { view row = convertview; if (row == null) { layoutinflater inflater = getlayoutinflater(); row = inflater.inflate(r.layout.siterow, null); } ftpsite s = model.get(position); ((textview) row.findviewbyid(r.id.title)).settext(s.getname()); return (row); } } ftpsite.java public class ftpsite { private string name = ""; private string address = ""; private string username = ""; private string password = ""; private int port = 21; private boolean passive = false; public ftpsite() { // todo auto-generated constructor stub } public string getname() { return (name); } public void setname(string name) { this.name = name; } public string getaddress() { return (address); } public void setaddress(string address) { this.address = address; } public string getusername() { return (username); } public void setusername(string username) { this.username = username; } public string getpassword() { return (password); } public void setpassword(string password) { this.password = password; } public int getport() { return (port); } public void setport(int port) { this.port = port; } public boolean ispassive() { return (passive); } public void setpassive(boolean passive) { this.passive = passive; } }
and siterow.xml
<?xml version="1.0" encoding="utf-8"?> <textview xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/title" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancelarge" android:gravity="center_vertical" android:paddingleft="6dip" android:minheight="?android:attr/listpreferreditemheight" />
edit
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(sitemanager.this, r.layout.siterow, r.id.title, model); site_list.setadapter(adapter); 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); 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(); } }); close.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { } }); 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()); _passive = false; if (passive.ischecked()) { _passive = true; } 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()); long newid = mydb.insertrow(_name, _address, _username, _password, 21); 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, tmpsite.getname(), toast.length_short).show(); // displayrecords(); } }); clr.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { clearall(); } }); } class siteadapter extends arrayadapter<ftpsite> { public siteadapter(context context, int resource, int textviewresourceid, list<ftpsite> objects) { super(context, resource, textviewresourceid, objects); } } public view getview(int position, view convertview, viewgroup parent) { view row = convertview; if (row == null) { layoutinflater inflater = getlayoutinflater(); row = inflater.inflate(r.layout.siterow, null); } ftpsite s = model.get(position); ((textview) row.findviewbyid(r.id.title)).settext(s.getname()); return (row); }
replace siteadapter adapter;
with
arrayadapter<ftpsite> adapter
then in adapter class need create constructor this
public siteadapter(context context, int resource, int textviewresourceid, list<t> objects) { super(context,resource,textviewresourceid,objects); }
see documentation
edit:
when declare need do
adapter = new siteadapter(this,r.layout.your_layout,new int[] {r.id.title},model);
Comments
Post a Comment