listview - Android on Long Click is not working properly - custom list -
my custom listview not working properly. works fine till didnt scroll end, scroll end onclick feature stop working, m using custom list extending base adapter..
i have set click listener on toogle , it, have set focusable false in custom row.
but thing noticed whenever list stop taking clicks, following error generated in logs.
05-18 17:54:02.674: w/keycharactermap(4343): can't open keycharmap file 05-18 17:54:02.674: w/keycharactermap(4343): error loading keycharmap file '/system/usr/keychars/cy8c-touchscreen.kcm.bin'. hw.keyboards.65537.devname='cy8c-touchscreen' 05-18 17:54:02.674: i/keycharactermap(4343): using default keymap: /system/usr/keychars/qwerty.kcm.bin
list view
<listview android:id="@+id/main_list" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margintop="3dp" android:cachecolorhint="#00000000" android:clickable="true" android:divider="@null" android:dividerheight="0dp" android:listselector="@android:color/transparent" android:smoothscrollbar="true" > </listview>
custom row
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/list_row" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margintop="5dp" android:background="@drawable/main_row" > <togglebutton android:id="@+id/row_toggle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/row_fri" android:layout_alignleft="@+id/row_fri" android:background="@drawable/tog_selector" android:focusable="false" android:text="togglebutton" android:textoff="" android:texton="" /> </relativelayout>
main class
package com.personoids.personoid; import java.sql.date; import java.text.simpledateformat; import java.util.arraylist; import java.util.calendar; import java.util.list; import java.util.locale; import java.util.timezone; import java.util.timer; import java.util.timertask; import java.util.concurrent.timeunit; import android.net.uri; import android.os.bundle; import android.os.handler; import android.os.message; import android.provider.mediastore; import android.annotation.suppresslint; import android.app.activity; import android.app.alarmmanager; import android.app.pendingintent; import android.content.context; import android.content.intent; import android.content.res.configuration; import android.graphics.color; import android.graphics.typeface; import android.graphics.drawable.drawable; import android.graphics.drawable.statelistdrawable; import android.text.format.formatter; import android.util.displaymetrics; import android.util.log; import android.view.contextmenu; import android.view.contextmenu.contextmenuinfo; import android.view.gravity; import android.view.layoutinflater; import android.view.menu; import android.view.menuinflater; import android.view.menuitem; import android.view.motionevent; import android.view.view; import android.view.view.ontouchlistener; import android.view.viewgroup; import android.view.view.onclicklistener; import android.view.view.onfocuschangelistener; import android.view.viewgroup.layoutparams; import android.view.window; import android.view.animation.translateanimation; import android.widget.adapterview; import android.widget.adapterview.onitemclicklistener; import android.widget.adapterview.onitemlongclicklistener; import android.widget.arrayadapter; import android.widget.baseadapter; import android.widget.imageview; import android.widget.linearlayout; import android.widget.listview; import android.widget.relativelayout; import android.widget.textview; import android.widget.toast; import android.widget.togglebutton; @suppresslint("newapi") public class main extends activity implements onclicklistener, onitemclicklistener, onitemlongclicklistener{ intent add_alarm; listview main_list; statelistdrawable state_list; db_alarm db; alarm_info[] data; categoryadapter main_adapter; textview next_date; textview next_time; textview next_in; int cur_position; float text_size; @suppresswarnings("deprecation") @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); requestwindowfeature(window.feature_custom_title); setcontentview(r.layout.main); getwindow().setfeatureint(window.feature_custom_title, r.layout.custom_title); textview custom_title = (textview) findviewbyid(r.id.custom_title); custom_title.settext("alarm clock wise"); init_vars(); clock_update(); populatelist(); } @override public void onclick(view v) { // todo auto-generated method stub if((v.getid() == r.id.add_alarm_text) ||( v.getid() == r.id.add_alarm_icon)) { add_alarm = new intent(this, add_alarm.class); add_alarm.putextra("edit", 0); startactivity(add_alarm); } } public void clock_update() { long time_millis = system.currenttimemillis(); long hr; long min; string am_pm = "am"; string time = null; timezone timezone = timezone.getdefault(); int gmt = timezone.getrawoffset()/(60 * 60 * 1000); hr = (time_millis/(1000*60*60)%24)+gmt; min = (time_millis/(1000*60)%60); if (hr >= 12 && hr <24) { am_pm = "pm"; } hr= hr%12; if(hr == 0) hr = 12; textview clock = (textview) findviewbyid(r.id.alarm_clock); clock.settext(string.format("%02d:%02d "+ am_pm, hr,min)); timer clock_timer = new timer(); int delay =(60 - ((int)((time_millis/1000)%60))) * 1000; int period = 60000; clock_timer.scheduleatfixedrate(new timertask() { long hr; long min; long time_millis; string am_pm = "am"; string time = null; textview clock = (textview) findviewbyid(r.id.alarm_clock); timezone timezone = timezone.getdefault(); int gmt = timezone.getrawoffset()/(60 * 60 * 1000); @override public void run() { // todo auto-generated method stub time_millis = system.currenttimemillis(); hr = (time_millis/(1000*60*60)%24)+gmt; min = (time_millis/(1000*60)%60); if (hr >= 12 && hr <24) { am_pm = "pm"; } hr= hr%12; if(hr == 0) hr = 12; clock.post(new runnable() { public void run() { runonuithread(new runnable() { public void run() { clock.settext(string.format("%02d:%02d "+ am_pm, hr,min)); populatelist(); } }); } }); } }, delay,period); } public void init_vars() { relativelayout row1= (relativelayout) findviewbyid(r.id.row1); drawable row = row1.getbackground(); next_date = (textview) findviewbyid(r.id.next_alarm_date); next_time = (textview) findviewbyid(r.id.next_alarm_time); next_in = (textview) findviewbyid(r.id.next_alarm_time_left); //list_row.getalpha(); //row1.setalpha(80); row.setalpha(80); row1.setbackgrounddrawable(row); textview clock = (textview) findviewbyid(r.id.alarm_clock); textview next_time = (textview) findviewbyid(r.id.next_alarm_time); typeface ethnocen = typeface.createfromasset(getassets(), "fonts/ethnocen.ttf"); typeface goodtime = typeface.createfromasset(getassets(), "fonts/goodtime.ttf"); textview add_alarm_text = (textview) findviewbyid(r.id.add_alarm_text); imageview add_alarm_icon = (imageview) findviewbyid(r.id.add_alarm_icon); clock.settypeface(ethnocen); next_time.settypeface(goodtime); add_alarm_text.setonclicklistener(this); add_alarm_icon.setonclicklistener(this); db = new db_alarm(this); data = db.ret_alarm_info(); main_adapter = new categoryadapter(main.this, data); main_list = (listview) findviewbyid(r.id.main_list); main_list.setadapter(main_adapter); main_list.setonitemclicklistener(this); main_list.setonitemlongclicklistener(this); registerforcontextmenu(main_list); text_size = next_time.gettextsize(); main_list.post(new runnable() { public void run() { main_adapter.notifydatasetchanged(); } }); } @override public void oncreatecontextmenu(contextmenu menu, view v, contextmenuinfo menuinfo) { // todo auto-generated method stub super.oncreatecontextmenu(menu, v, menuinfo); menuinflater men_inflater = getmenuinflater(); men_inflater.inflate(r.menu.context_rows, menu); } @override public boolean oncontextitemselected(menuitem item) { // todo auto-generated method stub switch(item.getitemid()) { case r.id.delete: db.delete(data[cur_position].getid()); cancel_pendings(data[cur_position].getid()); break; case r.id.edit: add_alarm = new intent(this, add_alarm.class); add_alarm.putextra("alarm_info", data[cur_position]); add_alarm.putextra("links", db.ret_links(data[cur_position].getid())); add_alarm.putextra("edit", 1); startactivity(add_alarm); break; case r.id.duplicate: db.duplicate(data[cur_position].getid()); int id = db.return_last_id(); intent = new intent(this, launch_alarm.class); i.putextra("id", id); pendingintent pi = pendingintent.getactivity(this, id, i, pendingintent.flag_update_current); alarmmanager = (alarmmanager) getsystemservice(alarm_service); calendar calendar = calendar.getinstance(); data[cur_position] = db.ret_alarm_row(id); int secs = calendar.get(calendar.second); secs = 60-secs; calendar.settimeinmillis(system.currenttimemillis()); int day = data[cur_position].days_left()*24; int hr = data[cur_position].hours_left()+day; hr = hr*60; int min = data[cur_position].mins_left()+hr; min--; calendar.add(calendar.minute, min); calendar.add(calendar.second, secs); am.set(alarmmanager.rtc_wakeup, calendar.gettimeinmillis(), pi); break; case r.id.clear: for(int j = 0; j < data.length ; j++) { db.delete(data[j].getid()); cancel_pendings(data[j].getid()); } break; } //toast.maketext(this, item.gettitle(), toast.length_short).show(); populatelist(); return super.oncontextitemselected(item); } @override public void onitemclick(adapterview<?> arg0, view v, int pos, long arg3) { // todo auto-generated method stub } @override public boolean onitemlongclick(adapterview<?> arg0, view arg1, int pos, long arg3) { // todo auto-generated method stub cur_position = pos; return false; } @override protected void onresume() { // todo auto-generated method stub super.onresume(); populatelist(); } private void populatelist() { // todo auto-generated method stub data = db.ret_alarm_info(); main_adapter.update(data); update_next(); } public void update_next() { arraylist<integer> min = new arraylist<integer>(); int minimum =0; int index=0; int found =0; int counter = 0; string[] months = {"january","february","march","april","may","june","july","august","september","october","november","december"}; string[] days = {"sunday","monday","tuesday","wednesday","thursday","friday","saturday"}; while(found==0 && counter < data.length) { if(data[counter].activated()==1) { minimum=(data[counter].days_left()*24*60)+(data[counter].hours_left()*60)+(data[counter].mins_left()); min.add(counter); found=1; } counter++; } log.i("1", "1"); if(counter <= data.length) {log.i("2", "1"); for(int = counter; < data.length ; i++) { if((((data[i].days_left()*24*60)+(data[i].hours_left()*60)+(data[i].mins_left()))<minimum&& data[i].activated()==1)) { minimum=(data[i].days_left()*24*60)+(data[i].hours_left()*60)+(data[i].mins_left()); min.add(i); } } for(int = 0; < min.size();i++) index = min.get(min.size()-1); } if(data.length == 0 || found==0) { log.i("3", "1"); next_date.settext(""); next_in.settext(""); next_time.settext("no alarm"); int sizes =(int)((22*getresources().getdisplaymetrics().density)+0.5); next_time.settextsize((float)sizes); } else { log.i("4", "1"); next_time.settextsize(text_size); calendar cal= calendar.getinstance(); cal.add(calendar.day_of_week, data[index].days_left()); cal.add(calendar.hour_of_day, data[index].hours_left()); cal.add(calendar.minute, data[index].mins_left()); // string next_day = days[cal.get(calendar.day_of_week)-1]; string next_month = months[cal.get(calendar.month)]; string date = cal.get(calendar.day_of_month)+""; if(date.equals("1") || date.equals("21")) date = date+"st"; else if(date.equals("2") || date.equals("22")) date = date+"nd"; else if(date.equals("3") || date.equals("23")) date = date+"rd"; else date = date+"th"; next_date.settext(next_day+" "+date+" "+next_month); int time_hrs = data[index].time_hrs(); string am_pm = "am"; if(time_hrs>11) { if(time_hrs>12) time_hrs = time_hrs % 12; am_pm = "pm"; } if(time_hrs == 0) { time_hrs= 12; } next_time.settext(string.format("%02d:%02d "+am_pm, time_hrs,data[index].time_mins())); next_in.settext(string.format("in %d hrs %d mins", data[index].days_left()*24+data[index].hours_left(), data[index].mins_left())); } } public void cancel_pendings(int id) { intent = new intent(this, launch_alarm.class); pendingintent pi = pendingintent.getactivity(this, id, i, pendingintent.flag_update_current); pi.cancel(); } public void set_alarm(int id, int day, int hr, int min) { intent = new intent(this, launch_alarm.class); i.putextra("id", id); pendingintent pi = pendingintent.getactivity(this, id, i, pendingintent.flag_update_current); alarmmanager = (alarmmanager) getsystemservice(alarm_service); calendar calendar = calendar.getinstance(); int secs = calendar.get(calendar.second); secs = 60-secs; calendar.settimeinmillis(system.currenttimemillis()); day = day*24; hr = hr+day; hr = hr*60; min = min+hr; min--; calendar.add(calendar.minute, min); calendar.add(calendar.second, secs); am.set(alarmmanager.rtc_wakeup, calendar.gettimeinmillis(), pi); } @override public boolean oncreateoptionsmenu(menu menu) { menu.add(menu.none, 0, 0, "add alarm").seticon(r.drawable.add); menu.add(menu.none, 1, 1, "clear list").seticon(r.drawable.clear); menu.add(menu.none, 2, 2, "rate").seticon(r.drawable.rate); menu.add(menu.none, 3, 3, "upgrade").seticon(r.drawable.upgrade); menu.add(menu.none, 4, 4, "help").seticon(r.drawable.help); menu.add(menu.none, 5, 5, "close").seticon(r.drawable.close); return true; } @override public boolean onoptionsitemselected(menuitem item) { switch(item.getitemid()) { case 0: add_alarm = new intent(this, add_alarm.class); add_alarm.putextra("edit", 0); startactivity(add_alarm); break; case 1: for(int j = 0; j < data.length ; j++) { db.delete(data[j].getid()); cancel_pendings(data[j].getid()); } populatelist(); break; case 2: break; case 3: break; case 4: break; case 5: finish(); break; } return super.onoptionsitemselected(item); } }
custom adapter
package com.personoids.personoid; import java.util.list; import android.annotation.suppresslint; import android.content.context; import android.graphics.color; import android.graphics.drawable.drawable; import android.util.log; import android.view.layoutinflater; import android.view.view; import android.view.view.onclicklistener; import android.view.view.onfocuschangelistener; import android.view.viewgroup; import android.webkit.webview.findlistener; import android.widget.baseadapter; import android.widget.compoundbutton; import android.widget.compoundbutton.oncheckedchangelistener; import android.widget.imagebutton; import android.widget.relativelayout; import android.widget.textview; import android.widget.toast; import android.widget.togglebutton; @suppresslint("newapi") public class categoryadapter extends baseadapter implements onclicklistener { private main catactivity; private alarm_info[] data; private context co; relativelayout rl ; drawable d; db_alarm db; textview alarm_time; textview remaining; textview sun; textview mon; textview tue; textview wed; textview thu; textview fri; textview sat; togglebutton toggle; public categoryadapter(context c, alarm_info[] datas) { co = c; data = datas; } @suppresswarnings("deprecation") @override public view getview(int position, view convertview, viewgroup parent) { view v = convertview; final int pos = position; if (v == null) { layoutinflater inflater = (layoutinflater) co.getsystemservice(context.layout_inflater_service); v= inflater.inflate(r.layout.main_row, null); } // v.setonclicklistener(this); //v.setonfocuschangelistener(this); rl = (relativelayout) v.findviewbyid(r.id.list_row); d =rl.getbackground(); d.setalpha(80); rl.setbackgrounddrawable(d); int[] days = data[position].days(); alarm_time = (textview) v.findviewbyid(r.id.row_alarm_time); int time_hrs = data[position].time_hrs(); string am_pm = "am"; if(time_hrs>11) { if(time_hrs>12) time_hrs = time_hrs % 12; am_pm = "pm"; } if(time_hrs == 0) { time_hrs= 12; } alarm_time.settext(string.format("%02d:%02d "+am_pm, time_hrs,data[position].time_mins())); alarm_time.settag(position); remaining = (textview) v.findviewbyid(r.id.row_rem_time); remaining.settag(position); remaining.settext(string.format("%dd %dh %dm", data[position].days_left(),data[position].hours_left(),data[position].mins_left())); textview description = (textview) v.findviewbyid(r.id.row_desc); description.settag(position); description.settext(data[position].getdesc()); sun = (textview) v.findviewbyid(r.id.row_sun); sun.settag(position); if(days[0]==0) sun.settextcolor(color.parsecolor("#50ffffff")); else sun.settextcolor(color.parsecolor("#ffffffff")); mon = (textview) v.findviewbyid(r.id.row_mon); mon.settag(position); if(days[1]==0) mon.settextcolor(color.parsecolor("#50ffffff")); else mon.settextcolor(color.parsecolor("#ffffffff")); tue = (textview) v.findviewbyid(r.id.row_tue); tue.settag(position); if(days[2]==0) tue.settextcolor(color.parsecolor("#50ffffff")); else tue.settextcolor(color.parsecolor("#ffffffff")); wed = (textview) v.findviewbyid(r.id.row_wed); wed.settag(position); if(days[3]==0) wed.settextcolor(color.parsecolor("#50ffffff")); else wed.settextcolor(color.parsecolor("#ffffffff")); thu = (textview) v.findviewbyid(r.id.row_thu); thu.settag(position); if(days[4]==0) thu.settextcolor(color.parsecolor("#50ffffff")); else thu.settextcolor(color.parsecolor("#ffffffff")); fri = (textview) v.findviewbyid(r.id.row_fri); fri.settag(position); if(days[5]==0) fri.settextcolor(color.parsecolor("#50ffffff")); else fri.settextcolor(color.parsecolor("#ffffffff")); sat = (textview) v.findviewbyid(r.id.row_sat); sat.settag(position); if(days[6]==0) sat.settextcolor(color.parsecolor("#50ffffff")); else sat.settextcolor(color.parsecolor("#ffffffff")); if(data[position].activated()==0) { remaining.settext("disabled"); notifydatasetchanged(); } if(data[position].skip==1 && data[position].activated()==1) { remaining.settext("snoozed: "+data[position].snooze_duration+" min"); notifydatasetchanged(); } toggle = (togglebutton) v.findviewbyid(r.id.row_toggle); toggle.settag(position); if(data[position].activated()==0) toggle.setchecked(false); else toggle.setchecked(true); toggle.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub db = new db_alarm(co); if(data[pos].activated==0) { db.activate(data[pos].getid()); data[pos].activated=1; ((main) co).set_alarm(data[pos].getid(), data[pos].days_left(), data[pos].hours_left(), data[pos].mins_left()); } else { db.deactivate(data[pos].getid()); db.iskip(data[pos].getid(), 0); data[pos].activated=0; data[pos].skip=0; ((main) co).cancel_pendings(data[pos].getid()); } ((main) co).update_next(); notifydatasetchanged(); } }); return v; } public void update(alarm_info[] datas) { data=datas; notifydatasetchanged(); } @override public void onclick(view view) { } @override public int getcount() { // todo auto-generated method stub return data.length; } @override public object getitem(int position) { // todo auto-generated method stub return null; } @override public long getitemid(int position) { // todo auto-generated method stub return 0; } }
Comments
Post a Comment