android - Problem registering clicks on Button on CustomCursorAdapter in a ListView -


what trying catch button click inside listview managed customcursoradapter. when clicked need make button invisible , update value in database. here code using listactivity , cursoradapter.

public class maintabview extends listactivity{ /** called when activity first created. */ @override public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.main);     filllistdata(); }   private void filllistdata(){  databasenameshelper mydbnameshelper = new databasenameshelper(this);  mydbnameshelper.opendatabase();  cursor cursor = mydbnameshelper.getcursorquerywithallthetaxistations();  startmanagingcursor(cursor);    // desired columns bound     string[] columns = new string[] { databasenameshelper.column_name, databasenameshelper.column_people};     // xml defined views data bound     int[] = new int[] { r.id.name_entry, r.id.number_entry };      // create adapter using cursor pointing desired data layout information     customcursoradapter madapter = new customcursoradapter(this, r.layout.list_entry, cursor, columns, to);     // set adapter listactivity's adapter     this.setlistadapter(madapter);        this.getlistview().setonitemclicklistener(madapter);     mydbnameshelper.close();  } 

and adapter:

public class customcursoradapter extends simplecursoradapter implements sectionindexer,filterable,      android.widget.adapterview.onitemclicklistener{  private context context; private int layout; private alphabetindexer alphaindexer;  public customcursoradapter (context context, int layout, cursor c, string[] from, int[] to) {     super(context, layout, c, from, to);     this.context = context;     this.layout = layout;     alphaindexer=new alphabetindexer(c, c.getcolumnindex(databasenameshelper.column_name), " abcdefghijklmnopqrstuvwxyz"); }  @override public view newview(context context, cursor cursor, viewgroup parent) {      cursor c = getcursor();      final layoutinflater inflater = layoutinflater.from(context);     view v = inflater.inflate(layout, parent, false);      int namecol = c.getcolumnindex(databasenameshelper.column_name);      string name = c.getstring(namecol);      /**      * next set name of entry.      */     textview name_text = (textview) v.findviewbyid(r.id.name_entry);     if (name_text != null) {         name_text.settext(name);     }      int favcol = c.getcolumnindex(databasenameshelper.column_favourited);     int fav = c.getint(favcol);      button button = (button) v.findviewbyid(r.id.button01);     if(fav==1){      button.setvisibility(view.invisible);     }      return v; }  @override public void bindview(view v, context context, cursor c) {      int namecol = c.getcolumnindex(databasenameshelper.column_name);      string name = c.getstring(namecol);      /**      * next set name of entry.      */     textview name_text = (textview) v.findviewbyid(r.id.name_entry);     if (name_text != null) {         name_text.settext(name);     }     int favcol = c.getcolumnindex(databasenameshelper.column_favourited);     int fav = c.getint(favcol);      button button = (button) v.findviewbyid(r.id.button01);     log.e("fav",string.valueof(fav));     if(fav==1){      button.setvisibility(view.invisible);     } }   @override  public int getpositionforsection(int section) {   return alphaindexer.getpositionforsection(section);  }   @override  public int getsectionforposition(int position) {     return alphaindexer.getsectionforposition(position);  }   @override  public object[] getsections() {     return alphaindexer.getsections();  }  @override  public void onitemclick(adapterview<?> arg0, view arg1, int arg2, long arg3) {   log.e("item click", arg1.tostring()+ " position> " +arg2);  } 

i have set button clickable(true) , focusable(false).

with code can achieve want clicking listview row (logs item clicks on linearlayout holding button. how make button receive click same linearlayout does?

here row layout:

 <?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="fill_parent"         android:layout_height="wrap_content" android:orientation="horizontal" android:focusable="false">  <textview   android:id="@+id/name_entry"   android:layout_height="wrap_content"   android:textsize="28dip" android:layout_width="wrap_content" android:layout_weight="1" android:layout_gravity="center_vertical"/>        <button android:id="@+id/button01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="fav" android:layout_gravity="center_vertical" android:layout_marginright="10dp" android:focusable="false" android:clickable="true"></button><textview   android:id="@+id/number_entry"   android:layout_width="wrap_content"   android:layout_height="wrap_content"   android:textsize="28dip" />   </linearlayout> 

you need new aproach described in button documentation.

however, instead of applying onclicklistener button in activity, can assign method button in xml layout, using android:onclick attribute. example:

<button      android:layout_height="wrap_content"      android:layout_width="wrap_content"      android:text="@string/self_destruct"      android:onclick="selfdestruct" /> 

now, when user clicks button, android system calls activity's selfdestruct(view) method. in order work, method must public , accept view parameter. example:

 public void selfdestruct(view view) {      // kabloey  } 

the view passed method reference widget clicked. can settag() on view in adapter recognize button clicked.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -