java - view.getId is -1 in onClick Listener -


i'm inflating view, setting it's id, set tags, set onclicklistener on , add parent view.

linearlayout llcategorylistincome = (linearlayout)calculatorflipcontainerback.findviewbyid(r.id.ll_category_list_income); // inflate category item linearlayout categoryitem = (linearlayout)inflater.inflate(                         r.layout.fragment_category_item,                         container,                         false);  integer id = 2; integer position = 3;  categoryitem.setid(position); categoryitem.settag(r.string.tag_category_position, position); categoryitem.settag(r.string.tag_category_id, id); categoryitem.setonclicklistener(this); llcategorylistincome.addview(categoryitem); 

in onclick listener want check id. id -1. same issue tags. tags null.

public void onclick(view v) {    integer position = (integer)view.gettag(r.string.tag_category_position);    integer id = (integer)view.gettag(r.string.tag_category_id);     log.d(tag, "view id " + view.getid());   // null    log.d(tag, "position " + position);   // null    log.d(tag, "id " + id);   // -1 

any idea wrong?

you must use id , not string resource when assign tag.

use like

categoryitem.settag(r.id.your_id_for_position, position); 

instead of

categoryitem.settag(r.string.tag_category_position, position); 

as said in view class documentation

the specified key should id declared in resources of application ensure unique


Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -