android - Add click listener to Buttons in ListView -


beginner here. using cursoradapter , listview. have 2 buttons per row(item?). add click listener them such can hide/show textviews same row.

here adapter:

public class todocursoradapter extends cursoradapter {     public todocursoradapter(context context, cursor cursor) {         super(context, cursor, 0);     }      // newview method used inflate new view , return it,     // don't bind data view @ point.     @override     public view newview(context context, cursor cursor, viewgroup parent) {         return layoutinflater.from(context).inflate(r.layout.listview, parent, false);     }      @override     public void bindview(view view, context context, cursor cursor) {         textview engtext = (textview) view.findviewbyid(r.id.engtext);         textview arabtext = (textview) view.findviewbyid(r.id.arabtext);         textview reftext = (textview) view.findviewbyid(r.id.reftext);          string arabic = cursor.getstring(cursor.getcolumnindexorthrow("plainarab_text"));         string english = cursor.getstring(cursor.getcolumnindexorthrow("plaineng_text"));         string ref = cursor.getstring(cursor.getcolumnindexorthrow("ref"));          arabtext.settext(arabic);         engtext.settext(english);         reftext.settext(ref);     } } 

adapter xml:

<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" >      <linearlayout         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:orientation="horizontal">          <button             android:id="@+id/button"             android:layout_width="0sp"             android:layout_weight="0.5"             android:layout_height="wrap_content"             android:text="left button"             />          <button             android:id="@+id/button2"             android:layout_width="0sp"             android:layout_weight="0.5"             android:layout_height="wrap_content"             android:text="right button"             />     </linearlayout>     <textview         android:id="@+id/engtext"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_marginstart="10sp"         android:textsize="15sp"         android:typeface="serif"/>      <textview         android:id="@+id/arabtext"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_marginstart="10sp"         android:textsize="15sp"         android:typeface="sans"         />     <textview         android:id="@+id/reftext"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_marginstart="10sp"         android:textsize="15sp" /> </linearlayout> 

activity code:

public class listviewactivity extends activity {     listview listview ;     private sqlitedatabase hadlistdb;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_listview);          databasehelper1 hadlistdbhelper;         hadlistdbhelper = new databasehelper1(this);          try {             hadlistdbhelper.createdatabase();         }         catch (ioexception ioe)         {             throw new error("unable create database");         }         hadlistdb =  hadlistdbhelper.opendatabase();          cursor todocursor = hadlistdb.rawquery("select  id _id, * had_table id < 5 ", null);          // find listview populate         listview lvitems = (listview) findviewbyid(r.id.list); // setup cursor adapter using cursor last step         todocursoradapter todoadapter = new todocursoradapter(this, todocursor); // attach cursor adapter listview         lvitems.setadapter(todoadapter);          todoadapter.changecursor(todocursor);     }  } 

activity xml:

<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:orientation="vertical" >      <linearlayout         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:orientation="horizontal">         <button             android:id="@+id/button3"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:layout_weight="1"             android:text="top1" />           <button             android:id="@+id/button4"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:layout_weight="1"             android:text="top2" />     </linearlayout>       <listview         android:id="@+id/list"         android:layout_width="match_parent"         android:layout_height="wrap_content">     </listview> </linearlayout> 

how add listeners buttons in adapter xml?

create button object on bindview method have overridden on todocursoradapter class use findviewbyid use the rest of text view initialize object, after need add onclicklistner have intended on code (ie. hiding textview)

public class todocursoradapter extends cursoradapter { public todocursoradapter(context context, cursor cursor) {     super(context, cursor, 0); }  // newview method used inflate new view , return it, // don't bind data view @ point. @override public view newview(context context, cursor cursor, viewgroup parent) {     return layoutinflater.from(context).inflate(r.layout.listview, parent, false); }  @override public void bindview(view view, context context, cursor cursor) {     textview engtext = (textview) view.findviewbyid(r.id.engtext);     textview arabtext = (textview) view.findviewbyid(r.id.arabtext);     textview reftext = (textview) view.findviewbyid(r.id.reftext);     textview reftext = (textview) view.findviewbyid(r.id.reftext);     button button = (button) view.findviewbyid(r.id.button);     button button2 = (button) view.findviewbyid(r.id.button2);      string arabic = cursor.getstring(cursor.getcolumnindexorthrow("plainarab_text"));     string english = cursor.getstring(cursor.getcolumnindexorthrow("plaineng_text"));     string ref = cursor.getstring(cursor.getcolumnindexorthrow("ref"));      arabtext.settext(arabic);     engtext.settext(english);     reftext.settext(ref);     //updated     arabtext.setvisibility(view.visible);     engtext.setvisibility(view.visible);      button.setonclicklistener(new view.onclicklistener()           {             @override             public void onclick(view v){            arabtext.setvisibility(view.gone);          }        });     button2.setonclicklistener(new view.onclicklistener()          {             @override             public void onclick(view v){            engtext.setvisibility(view.gone);          }       });  } } 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -