android - Handling the Click Event in the ListView -
i have listview in there 20 items . 10 items contacts of phone , rest of 10 bookmars url of default browser . want that: 1) when click on contacts(first 10) should take me partular contact in contact app. 2)when click on bookmark url(rest of 10) , should open url in browser .
i implemented first requirement of mine following code below :
arraylist<hashmap<string, object>> listitem = new arraylist<hashmap<string, object>>(); msharedprefs = preferencemanager .getdefaultsharedpreferences(getapplicationcontext()); boolean contact_check = msharedprefs.getboolean("contact_check", true); boolean bookmark_check = msharedprefs .getboolean("bookmark_check", true); contentresolver cr = getcontentresolver(); cursor cur = cr.query(contactscontract.contacts.content_uri, null, null, null, null); if (contact_check) { if (cur.getcount() > 0) { while (cur.movetonext()) { string id = cur.getstring(cur .getcolumnindex(contactscontract.contacts._id)); string name = cur .getstring(cur .getcolumnindex(contactscontract.contacts.display_name)); if (integer .parseint(cur.getstring(cur .getcolumnindex(contactscontract.contacts.has_phone_number))) > 0) { cursor pcur = cr .query(contactscontract.commondatakinds.phone.content_uri, null, contactscontract.commondatakinds.phone.contact_id + " = ?", new string[] { id }, null); while (pcur.movetonext()) { string phoneno = pcur .getstring(pcur .getcolumnindex(contactscontract.commondatakinds.phone.number)); hashmap<string, object> map = new hashmap<string, object>(); map.put("name", name); map.put("phone no", phoneno); // map.put("bookmarks", faves.getstring(titleidx)); listitem.add(map); } pcur.close(); } } } } simpleadapter listitemadapter = new simpleadapter(this, listitem, r.layout.list_style, new string[] { "name", "phone no" }, new int[] { r.id.toptextview, r.id.bottomtextview }); lv.setadapter(listitemadapter); lv.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> arg0, view arg1, int arg2, long arg3) { relativelayout lr = (relativelayout) arg1; textview mtext = (textview) lr.getchildat(1); intent = new intent(intent.action_view); i.setaction(contactscontract.intents.show_or_create_contact); i.setdata(uri .fromparts("tel", mtext.gettext().tostring(), null)); startactivity(i); } });
the above code adding contacts in listview on handling click event .
for second requirement extended listview bookmarks , following code:
string[] requestedcolumns = { browser.bookmarkcolumns.title, browser.bookmarkcolumns.url };
@suppresswarnings("deprecation") final cursor faves = managedquery(browser.bookmarks_uri, requestedcolumns, browser.bookmarkcolumns.bookmark + "=1", null, null); log.d("bookmarks", "bookmarks count: " + faves.getcount()); faves.movetofirst(); int titleidx = faves.getcolumnindex(browser.bookmarkcolumns.title); final int urlidx = faves.getcolumnindex(browser.bookmarkcolumns.url); if (bookmark_check) { while (!faves.isafterlast()) { log.d("simplebookmarks", faves.getstring(titleidx)); log.d("simplebookmarks url", " " + faves.getstring(urlidx)); hashmap<string, object> map = new hashmap<string, object>(); map.put("name", faves.getstring(titleidx)); map.put("phone no", faves.getstring(urlidx)); listitem.add(map); faves.movetonext(); }
its getting populated not able handle click event . want open url in browser when user click on it.
with adaptation method:
lv.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> arg0, view arg1, int arg2, long arg3) { if (arg2 < 10) { relativelayout lr = (relativelayout) arg1; textview mtext = (textview) lr.getchildat(1); intent = new intent(intent.action_view); i.setaction(contactscontract.intents.show_or_create_contact); i.setdata(uri.fromparts("tel", mtext.gettext().tostring(), null)); startactivity(i); } else { intent browserintent = new intent(intent.action_view, uri.parse(getcurrenturi)); //getcurrenturi uri @ position arg2-10 startactivity(browserintent); } } });
Comments
Post a Comment