android - ListView for buttons using BaseAdapters -
i trying perform actions on click of button listview . onclicklistener dosent called on click of button.
here mainactivity.java
public class mainactivity extends activity {
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); final listview mylistview = (listview) findviewbyid(r.id.listview); final button mybutton = (button) findviewbyid(r.id.button); //create adapter final rownumadapter rownumadapter = new rownumadapter(this); mylistview.setadapter(rownumadapter); mylistview.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { if(position==0) { toast.maketext(mainactivity.this,"you clicked"+ rownumadapter.getitem(position) , toast.length_long).show(); } } }); mybutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { rownumadapter.addrows(); } }); }
}
here rownumadapter.java
public class rownumadapter extends baseadapter {
private context mcontext; private int numrows; public rownumadapter(context context) { this.mcontext=context; this.numrows= 5; } public void addrows() { this.numrows+=1; } @override public int getcount() { // todo auto-generated method stub return this.numrows; } @override public object getitem(int position) { return null; } @override public long getitemid(int position) { return 0; } @override public view getview(int position, view convertview, viewgroup parent) { button view = null; if(convertview == null) { view = new button(this.mcontext); } else { view = (button)convertview; } view.settext(" row " + position); return view; }
}
here activity_main.xml :
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:paddingleft="10dp" android:paddingright="15dp"> <button android:layout_margintop="15dp" android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="add view" android:id="@+id/button"></button> <listview android:id="@+id/listview" android:layout_width="match_parent" android:layout_height="wrap_content" android:focusable="false" android:clickable="false" > </listview> </linearlayout>
you have use getview
method adapter class. ref.
it should this.
@override public view getview(final int position, view convertview, viewgroup parent) { layoutinflater inflater = (layoutinflater) thiscontext .getsystemservice(context.layout_inflater_service); view vi = convertview; // layout here vi = inflater.inflate(r.layout.activity_main, null); // set button id here button btncheckin = (button) vi.findviewbyid(r.id.btncheckin); btncheckin.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { log.d("you clicked", "the item @ [" + position + "] position"); // button action here. } }); } return vi; }
here tutorial listview
, adapters
.
Comments
Post a Comment