Thursday, 17 December 2015

Get Position when Click on ListView and Click on Button in ListView

I am going to create customized ListView. In each row item put a Button so when user click on button it will show position of the item in ListView. In the same way when user click on ListView it will show position of the item in ListView.

Set the listener on ListView-:

listView.setOnItemClickListener(onItemClickListener);
Define the listener

private AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {

        @Override

        public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                                long arg3) {
            Toast.makeText(MainActivity.this, "This is click=" + position, Toast.LENGTH_LONG).show();
        }

    };

Set the OnClickListener on Button which is entail in ListView Row Item layout-:

eventButton.setOnClickListener(eventButtonClickListener);

Define the OnClickListener :

private OnClickListener eventButtonClickListener = new OnClickListener() {     
     @Override                                                                  
     public void onClick(View v) {                                              
         View parentRow = (View) v.getParent();                                 
         ListView listView = (ListView) parentRow.getParent();                  
         final int position = listView.getPositionForView(parentRow);           
                                                                                
     }                                                                          
 };                                                                             


No comments:

Post a Comment