java - Android ListView with button -


so making app have list of items contains text , button besides it.

i getting error in custom adapter class:

java.lang.nullpointerexception: attempt invoke virtual method 'void android.widget.button.setonclicklistener(android.view.view$onclicklistener)' on null object reference 

the entire custom adapter class follows:

package com.android.ict.seneca.androidpocketguide;  import android.app.activity; import android.content.context; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.arrayadapter; import android.widget.button; import android.widget.imageview; import android.widget.textview; import android.widget.toast;  import java.util.list;   public class customlistadapter extends arrayadapter<rowitem> {      private context context;      public customlistadapter(context context,                              int resourceid,                              list<rowitem> items ) {          super( context, resourceid, items );         this.context = context;     }      private class viewholder {         textview txtname;         textview txtdate;         textview txtlocation;         button button;      }     public view getview(int position, view convertview, viewgroup parent) {          viewholder holder = null;          rowitem rowitem = getitem(position);          layoutinflater minflater =                 (layoutinflater) context.getsystemservice(                         activity.layout_inflater_service );          if (convertview == null) {              convertview = minflater.inflate(r.layout.list_item, null);              holder = new viewholder();              holder.txtname = (textview) convertview.findviewbyid( r.id.nam );             holder.txtdate = (textview) convertview.findviewbyid( r.id.dat );             holder.txtlocation = (textview) convertview.findviewbyid( r.id.loc );             holder.button = (button)convertview.findviewbyid(r.id.deletebtn);             convertview.settag(holder);         }         else             holder = (viewholder) convertview.gettag();          holder.txtname.settext( rowitem.getnameid() );         holder.txtdate.settext( rowitem.getlocation() );         holder.txtlocation.settext( rowitem.getdate() );          holder.button.setonclicklistener(new view.onclicklistener()         {             @override             public void onclick(view v)             {                 toast.maketext(context, "delete button pressed: ",toast.length_long).show();                 // code want execute on button click             }          });          return convertview;     }  } 

i have 2 layouts, 1 listview itelf , 1 buttons.

the listview items follows (activity_websites):

<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" >      <listview         android:id="@+id/locationlist"         android:layout_width="match_parent"         android:layout_height="wrap_content"/>  </relativelayout> 

the button xmlfile follows (delete.xml):

<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"     android:orientation="vertical" android:layout_width="match_parent"     android:layout_height="match_parent">      <button         android:id="@+id/deletebtn"         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:textcolor="#ffffffff"         android:text="delete"         android:onclick="onclickdelete"/>  </linearlayout> 

also have feeling doing right wrong in general view want app. can on these?

the layout trying inflate list_item.xml. question understand button in delete.xml. convertview.findviewbyid(r.id.deletebtn); return null.

you need put button in list_item.xml.

also remove android:onclick="onclickdelete" in xml. have onclick in adapter.


Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -