vijay

welcome Netizen

Share Your Knowledge.It is a way to achieve immortality

Wednesday, August 1, 2012

Enable the Button click event on ListView in android.


Hi.
Recently I had work with activity with listview. Activity I have a case to add button on listview, so I design custom layout with button and textview.my requirement has when on listview or button I need to get the id from the row and move to next activity.
In code I used only activity and arrayadapter to setadapter on listview

Already I wrote some articles about listview please refe it..
http://www.dotnetcode.in/2012/07/showing-context-menu-in-single-click-on.html

Requirement:
I have a requirement if the user clicks on button I need to get the id from the row and move on to another activity.

Problem:
Normally In listview without using button you can call the method OnItemClickListener
By using this one we can easily get the position and move to the next activity. But if you use button on listview this event will not fire on listview.

Solution:
By made big search on Google I found a simple and best solution to solve my requirement.
Intially you need add the two things in xml in button,then only OnItemClickListener
will work on listview.
android:focusable="false"
android:focusableInTouchMode="false"
Next one we want to make enable the button click event on list view
For that another line on xml button layout
android:onClick="myClick"

The final code will be looki like below one..
<Button
        android:id="@+id/btnaccno"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="View"
        android:focusable="false"
android:focusableInTouchMode="false"
 android:onClick="myClick"
        />

Then write the myclick function on the activity. If you want to get the postion of the row from the listview use lst.getPositionForView(v)
Here lst denotes the ListView
public void myClick (View v)
           {
               int position1 = lst.getPositionForView(v);           
                     Intent i=new Intent(viewacc.this,viewtran.class);
                     i.putExtra("no", position1);
                     startActivity(i);         
           }

Thatsit…Now I fulfill my requirement I need ,Incase if you have met same kind of problem I hope this article would help you,the reason I share the code is I spent more time search to find the solutions.i hope I will reduce someone spare time to search this one..
In case if you have any problem to follow this method post your comments here..i try to give my solution best…

0 comments:

Post a Comment