java - incompatible operand types r.integer and int -
i trying compare integer type list , position in spinner. integer list contains list of ids database , spinner contains list of names respect ids. ids , names stored in database, after comparing id list , position spinner, 2 edit text boxes set values corresponding specific id other name. id set auto increment. here mainactivity:
public class loginactivity extends appcompatactivity { button b; edittext ed1, ed2, ed3; spinner sp; string url = "https://web.facebook.com/"; string urltest = "http://www.google.com/"; webview wb; database db; string details[]; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_login); b = (button) findviewbyid(r.id.button1); ed1 = (edittext) findviewbyid(r.id.edittext1); ed2 = (edittext) findviewbyid(r.id.edittext2); ed3 = (edittext) findviewbyid(r.id.edittext3); wb = (webview) findviewbyid(r.id.webview1); sp = (spinner) findviewbyid(r.id.spinner1); db = new database(loginactivity.this); // wb.loadurl(url); list<string> l = db.userlist(); final list<integer> idlist = db.idlist(); arrayadapter<string> sa = new arrayadapter<string>(loginactivity.this, android.r.layout.simple_spinner_item, l); sp.setadapter(sa); b.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { user u = new user(); string name = ed1.gettext().tostring(); string uname = ed2.gettext().tostring(); string pass = ed3.gettext().tostring(); if (name.length() > 0 && uname.length() > 0 && pass.length() > 0) { u.setname(name); u.setuname(uname); u.setpass(pass); db.insert(u); ed1.settext(""); ed2.settext(""); ed3.settext(""); } else { toast.maketext(loginactivity.this, "please enter name, user name , password", toast.length_short).show(); } toast.maketext(loginactivity.this, "the user name details has been saved.", toast.length_long).show(); } }); sp.setonitemselectedlistener(new adapterview.onitemselectedlistener() { @override public void onitemselected(adapterview<?> parent, view view, int position, long id) { // todo auto-generated method stub if (idlist.get(position) == position) { details = db.details(position); ed2.settext(details[0]); ed3.settext(details[1]); } } @override public void onnothingselected(adapterview<?> parent) { // todo auto-generated method stub } }); }
}
i have created 2 other java files database.java creates database , returns:
public class database extends sqliteopenhelper { public static string dbname = "personal"; public static int version = 1; public static string tblname = "data"; public static string id = "id"; public static string name = "name"; public static string uname = "username"; public static string pass = "password"; public database(context context) { super(context, dbname, null, version); // todo auto-generated constructor stub } @override public void oncreate(sqlitedatabase db) { // todo auto-generated method stub string sql1 = "create table " + tblname + " (" + id + " integer primary key autoincrement, " + name + " text, " + uname + " text, " + pass + " text)"; db.execsql(sql1); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { // todo auto-generated method stub } void insert(user u) { sqlitedatabase db = getwritabledatabase(); contentvalues values = new contentvalues(); values.put(name, u.getname()); values.put(uname, u.getuname()); values.put(pass, u.getpass()); db.insert(tblname, id, values); } public list<string> userlist() { list<string> list = new arraylist<string>(); sqlitedatabase db = getreadabledatabase(); cursor cursor = db.query(tblname, new string[] { name }, null, null, null, null, null); while (cursor.movetonext()) { string data = cursor.getstring(0); list.add(data); // log.e("1", data); } return list; } public list<integer> idlist() { list<integer> idlist = new arraylist<integer>(); sqlitedatabase db = getreadabledatabase(); cursor cursor = db.query(tblname, new string[] { id }, null, null, null, null, null); while (cursor.movetonext()) { string data = cursor.getstring(0); idlist.add(integer.parseint(data), null); } return idlist; } public string[] details(int position) { string username = null, password=null; sqlitedatabase db = getreadabledatabase(); cursor cursor = db.query(tblname, new string[] { id, uname, pass }, null, null, null, null, null); while (cursor.movetonext()) { if (position == cursor.getint(0)) { username = cursor.getstring(1); password = cursor.getstring(2); } else { return null; } } return new string[] {username, password}; } }
and user.java , set details:
public class user { string id, name, uname, pass; public string getname() { return name; } public void setname(string name) { this.name = name; } public string getid() { return id; } public void setid(string id) { this.id = id; } public string getuname() { return uname; } public void setuname(string uname) { this.uname = uname; } public string getpass() { return pass; } public void setpass(string pass) { this.pass = pass; } }
i getting error in mainactivity.java
file incompatible operand types r.integer , int
. on line if(idlist.get(position)==position)
---- in spinner set on item selected listener solved changing integer integer. statement somehow invalid. cannot compare integer , int. suggestions rectify it?
'integer' exists nested class of class 'r' . list interpreted list data type r.integer, 'r.integer' , primitive data type 'int' not compatible.
as purpose of class 'integer' turn integer numbers objects when needed, not compatible well.
edit: first thing want change every 'integer' in code 'integer', because want deal numbers, not android resources.
looking @ method crash occurred, it's bit difficult tell need make code work (not 'not crash').
the 'position' value in 'onitemselected()' method give index of list 'l' corresponding selected listview row.
now seem check wether list index of 1 user name equal userid in sqlite db. don't know data model, can't tell wether doing makes sense. know did fill 'idlist' , 'l' separate queries database , included no 'order by', maybe names , ids not ordered same way.
but that's question... :)
Comments
Post a Comment