Android ListView Adapter OnClickListener issue
<p>I created a ListView with a custom layout for each view. I have several onClickListeners within the list view adapter for <strong>TextView</strong> 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. </p>

<p>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 </p>

<p>partial code listing...</p>

<pre><code>public class main extends Activity {
    private ArrayList&lt;DataItem&gt; 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&lt;DataItem&gt;();
        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);
}
</code></pre>

<p>}</p>

<pre><code>public class DataItemAdapter extends ArrayAdapter&lt;DataItem&gt; {
    private Activity activity;
    private int resource;
    private static LayoutInflater inflater=null;

    public DataItemAdapter(Activity _activity,int _resource,List&lt;DataItem&gt; _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;
}
</code></pre>
