Android ListView Adapter OnClickListener issue

I created a ListView with a custom layout for each view. I have several onClickListeners within the list view adapter for TextView items within the custom layout. The onClickListeners work within the list view adapter and I can obtain the position of the view, but when I try to make calls within the onClickListeners to methods in my activity, I get the “Cannot make a static reference to a non-static method” errors. So I started converting things to static, which made things work as intended, but I’m sure you all know it’s a big mistake, which has finally caught up with me.

How the heck do I access methods in my main activity without making the methods static?? I’m new at this, so please forgive my noobish question. Thanks

  • findViewById vs View Holder Pattern in ListView adapter
  • How to detect a layout resize?
  • Load files bigger than 1M from assets folder
  • Single selection in RecyclerView
  • Android: When is onCreateOptionsMenu called during Activity lifecycle?
  • Error: No resource found that matches the given name (at 'theme' with value '@style/AppTheme')
  • partial code listing…

    public class main extends Activity {
        private ArrayList<DataItem> dataItems;
        private DataItemAdapter aa;
        private ListView dataListView;  
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            dataListView = (ListView)findViewById(R.id.dataListView);     
            dataItems = new ArrayList<DataItem>();
            int resID = R.layout.dataitem;
            aa = new DataItemAdapter(this, resID, dataItems);
            dataListView.setAdapter(aa);
            dataListView.setItemsCanFocus(true);
    
        populateArray();
    }
    
    public void populateArray() {
        DataItem newItem = new DataItem(
                "2008","Ferrari","F430","Red","ASX772"
            );
        dataItems.add(0, newItem);
        newItem = new DataItem(
                "2008","Ferrari","F430","Black","TZB123"
            );
        dataItems.add(0, newItem);
        newItem = new DataItem(
                "2009","Ferrari","F430","Red","MIDAS"
            );
        dataItems.add(0, newItem);      
    
        aa.notifyDataSetChanged();
    }
    
    public static void modelInfo(int pos) {
        Log.i("modelInfo", "=" + pos);
    }
    
    public static void makeInfo(int pos) {
        Log.i("makeInfo", "=" + pos);
    }
    
    public static void assetInfo(int pos) {
        Log.i("assetInfo", "=" + pos);
    }
    
    }
    
    
    public class DataItemAdapter extends ArrayAdapter<DataItem> {
        private Activity activity;
        private int resource;
        private static LayoutInflater inflater=null;
    
        public DataItemAdapter(Activity _activity,int _resource,List<DataItem> _items) {
            super(_activity, _resource, _items);
            inflater = (LayoutInflater)_activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            resource = _resource;
            activity = _activity;
        }
    
    public static class ViewHolder {
        TextView carYear;
        TextView carMake;
        TextView carModel;
        TextView carColor;
        TextView assetTag;
       }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        ViewHolder holder;
        if (convertView == null) {
            vi = inflater.inflate(resource, null);
            holder=new ViewHolder();
            holder.carYear = (TextView)vi.findViewById(R.id.carYear);
            holder.carMake = (TextView)vi.findViewById(R.id.carMake);
            holder.carModel = (TextView)vi.findViewById(R.id.carModel);
            holder.carColor = (TextView)vi.findViewById(R.id.carColor);
            holder.assetTag = (TextView)vi.findViewById(R.id.assetTag);
            vi.setTag(holder);
        } else {
            holder=(ViewHolder)vi.getTag();
        }
    
        DataItem item = getItem(position);  
    
        holder.carYear.setText(item.getCarYear());
        holder.carMake.setText(item.getCarMake());
        holder.carModel.setText(item.getCarModel());
        holder.carColor.setText(item.getCarColor());
        holder.assetTag.setText(item.getAssetTag());
    
        holder.carYear.setTag(position);
        holder.assetTag.setTag(position);
    
        final OnClickListener makeListener = new OnClickListener() {
            @Override
            public void onClick(View v) {
                LinearLayout ll = (LinearLayout)v.getParent();
                TextView tv = (TextView)ll.getChildAt(0);
                Integer pos = (Integer) tv.getTag();
                main.makeInfo(pos);
            }
        };
        holder.carMake.setOnClickListener(makeListener);
    
        final OnClickListener modelListener = new OnClickListener() {
            @Override
            public void onClick(View v) {
                LinearLayout ll = (LinearLayout)v.getParent();
                TextView tv = (TextView)ll.getChildAt(0);
                Integer pos = (Integer) tv.getTag();
                main.modelInfo(pos);
    
            }
       };        
    
        holder.carModel.setOnClickListener(modelListener);
    
        final OnClickListener assetListener = new OnClickListener() {
            @Override
            public void onClick(View v) {
                LinearLayout ll = (LinearLayout)v.getParent();
                TextView tv = (TextView)ll.getChildAt(0);
                Integer pos = (Integer) tv.getTag();
                main.assetInfo(pos);
            }
        };
        holder.assetTag.setOnClickListener(assetListener);        
    
        return vi;
    }
    

    Related posts:

    Vertical content align in Android EditText
    How to remove child nodes in firebase android?
    Connection with adb was interrupted, and attempts to reconnect have failed
    Google Play Alpha App BM-PPH-01 Error
    How to disable Volley request from retrying?
    Annotation Processors generated resources not packaged to APK
  • Android, Why launcher icon and other images never change when I update my images?
  • Does Google Maps Android MapView use different resolution tiles for different screen densities?
  • USB OTG Supported Phones?
  • how to get available wifi networks and display them in a list in android
  • ADB Shell Input Events
  • navigation drawer google maps v2 , map blocking drawer
  • One Solution collect form web for “Android ListView Adapter OnClickListener issue”

    why you dont attach an onItemClickListener to your ListView in your activity, instead of perform ItemClick from each inner view in the ListView

            dataListView = (ListView)findViewById(R.id.dataListView);     
            dataItems = new ArrayList<DataItem>();
            int resID = R.layout.dataitem;
            aa = new DataItemAdapter(this, resID, dataItems);
            dataListView.setAdapter(aa);
            //attach a listener to the list view
            dataListView.setOnItemClickListener (listener);
    
            dataListView.setItemsCanFocus(true);
    

    and inside your listener on onItemClick method you can access the activity methods.

    EDIT 1:
    the OnItemClickListener gives to you the following parameters, AdapterView parent, View view, int position, long

    your individual TextView is a child of the view parameter and you can access to it getting the childAt… something like this:

    OnItemClickListener listener = new OnItemClickListener (){
    
      @Override
      onItemClick(AdapterView<?> parent, View view, int position, long id){
          ((TextView)view.findViewById(R.id.yourTextViewId)).getText();
          //or do your stuff
      }
    
    }
    

    EDIT 2:

    your main activity, remember, by convention all Class name are capitalized, so main class must be Main class

    public class Main extends Activity {
        private ArrayList<DataItem> dataItems;
        private DataItemAdapter aa;
        private ListView dataListView;  
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            dataListView = (ListView)findViewById(R.id.dataListView);     
            dataItems = new ArrayList<DataItem>();
            int resID = R.layout.dataitem;
            aa = new DataItemAdapter(this, resID, dataItems);
            dataListView.setAdapter(aa);
            dataListView.setItemsCanFocus(true);
    
        populateArray();
    }
    
    public void populateArray() {
        DataItem newItem = new DataItem(
                "2008","Ferrari","F430","Red","ASX772"
            );
        dataItems.add(0, newItem);
        newItem = new DataItem(
                "2008","Ferrari","F430","Black","TZB123"
            );
        dataItems.add(0, newItem);
        newItem = new DataItem(
                "2009","Ferrari","F430","Red","MIDAS"
            );
        dataItems.add(0, newItem);      
    
        aa.notifyDataSetChanged();
    }
    
    public void modelInfo(int pos) {
        Log.i("modelInfo", "=" + pos);
    }
    
    public void makeInfo(int pos) {
        Log.i("makeInfo", "=" + pos);
    }
    
    public void assetInfo(int pos) {
        Log.i("assetInfo", "=" + pos);
    }
    }
    

    Now, your adapter

    public class DataItemAdapter extends ArrayAdapter<DataItem> {
        private Activity activity;
        private int resource;
        private LayoutInflater inflater=null;
    
        public DataItemAdapter(Activity _activity,int _resource,List<DataItem> _items) {
            super(_activity, _resource, _items);
            inflater = (LayoutInflater)_activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            //i always do this way, but i dont think this is the error
            //inflater = LayoutInflater.from(_activity.getBaseContext());
            resource = _resource;
            activity = _activity;
        }
    
    public static class ViewHolder {
        TextView carYear;
        TextView carMake;
        TextView carModel;
        TextView carColor;
        TextView assetTag;
       }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        ViewHolder holder;
        if (convertView == null) {
            vi = inflater.inflate(resource, null);
            holder=new ViewHolder();
            holder.carYear = (TextView)vi.findViewById(R.id.carYear);
            holder.carMake = (TextView)vi.findViewById(R.id.carMake);
            holder.carModel = (TextView)vi.findViewById(R.id.carModel);
            holder.carColor = (TextView)vi.findViewById(R.id.carColor);
            holder.assetTag = (TextView)vi.findViewById(R.id.assetTag);
            vi.setTag(holder);
        } else {
            holder=(ViewHolder)vi.getTag();
        }
    
        DataItem item = getItem(position);  
    
        holder.carYear.setText(item.getCarYear());
        holder.carMake.setText(item.getCarMake());
        holder.carModel.setText(item.getCarModel());
        holder.carColor.setText(item.getCarColor());
        holder.assetTag.setText(item.getAssetTag());
    
        holder.carYear.setTag(position);
        holder.assetTag.setTag(position);
    
        final OnClickListener makeListener = new OnClickListener() {
            @Override
            public void onClick(View v) {
                LinearLayout ll = (LinearLayout)v.getParent();
                TextView tv = (TextView)ll.getChildAt(0);
                Integer pos = (Integer) tv.getTag();
                //main.makeInfo(pos);
                ((Main)activity).makeInfo(pos);
            }
        };
        holder.carMake.setOnClickListener(makeListener);
    
        final OnClickListener modelListener = new OnClickListener() {
            @Override
            public void onClick(View v) {
                LinearLayout ll = (LinearLayout)v.getParent();
                TextView tv = (TextView)ll.getChildAt(0);
                Integer pos = (Integer) tv.getTag();
                //main.modelInfo(pos);
                ((Main)activity).modelInfo(pos);
            }
       };        
    
        holder.carModel.setOnClickListener(modelListener);
    
        final OnClickListener assetListener = new OnClickListener() {
            @Override
            public void onClick(View v) {
                LinearLayout ll = (LinearLayout)v.getParent();
                TextView tv = (TextView)ll.getChildAt(0);
                Integer pos = (Integer) tv.getTag();
                //main.assetInfo(pos);
                ((Main)activity).assetInfo(pos);
            }
        };
        holder.assetTag.setOnClickListener(assetListener);        
    
        return vi;
    }
    

    hope it works 🙂

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.