There are two classes:
- ListViewActivity extends Activity,
- MyAdapter extends BaseAdapter.
MyAdapter overrides the getView method and creates an item in ListView, which drawn in ListViewActivity.
I want to call setOnClickListener in MyAdapter.getView, but the listener must be placed in another class - not in MyAdapter.

Standard approach is:

        MyAdapter.getView ... {
            Button button_del = (Button) view.findViewById(R.id.button_del);     
            button_del.setOnClickListener(this);
        }   

It is not applicable for me because requires declaring the listener in MyAdapter class.

QUESTION - Can the setOnClickListener refer to the listener in another class ?

PS: button_del.setOnClickListener(ListViewActivity) does not work !

It's not important in wich class your listener is, what's important is in wich context it'll run.

This is not exactly what you are doing, in part because I didn't understand it all, but this may help...

class Parent extends Activity
{
    public Button btnTest;
    public Child objChild;

    void onCreate() {
        btnTest = findView..

        objChild = new Child();

        // Any of the tree options should work

        btnTest.setOnClickListener(myClick) // Parent Listener
        btnTest.setOnClickListener(objChild.myClick); // Child Listener
        btnTest.setOnClickListener(AnotherStatic.myClick); Static Click

    }

    OnClickListener myClick = new OnClickListener() { ... }
}

class Child 
{
    public OnClickListener myClick = new OnClickListener() { ... }
}

class AnotherStatic
{
    public static OnClickListener myClick = new OnClickListener() { ... }
}

The other way is also true, if you have the button property is public, any class with access to it could set the listener.

Hi!

As Mr.AleMonteiro said you can use the object reference..,

Otherwise simply you may customise your Adapter class to reach your goal..,

please copy your exact code for more information..,

-

My goal is to implement all click listeners in 1 class - ListViewActivity, but installing the listeners may occurs in this class or in another - MyAdapter.
But I tried to set listener without creating "new OnClickListener()", as Mr.AleMonteiro has proposed. It is wrong syntax (a line marked with "++++") ?

Example:

    public class MyAdapter extends BaseAdapter implements OnClickListener  {
            ListViewActivity ctx;
            LayoutInflater lInflater;
            ArrayList<Web_site> sites;   // локальная копия списка сайтов специально для адаптера

            MyAdapter(ListViewActivity context, ArrayList<Web_site> _sites) {
                ctx = context;
                sites = _sites;
                lInflater = (LayoutInflater) ctx
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            }

            ...

                    @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                // используем созданные, но не используемые view
                View view = convertView;
                if (view == null) {
                    view = lInflater.inflate(R.layout.one_line, parent, false);
                }

                Web_site p = get_site(position);


                ((TextView) view.findViewById(R.id.l_url)).setText(p.url);

                String stat = Integer.toString(p.content.length()) + "=";
                if (p.is_locked()) {stat = stat + "L";};
                ((TextView) view.findViewById(R.id.l_text)).setText(stat);
                Log.d("XXX", "updating pos = "+ Integer.toString(position) + "  cont.len = " + Integer.toString(p.content.length()));

                Button button_del = (Button) view.findViewById(R.id.button_del);    
                button_del.setOnClickListener(this);
                // ++++ ^ I want to set the listener to ctx.onClick (not to "this"); but button_del.setOnClickListener(ctx.onClick) generates syntax error  ++++

                return view;
            }

                @Override
            public void onClick(View v) {

                switch(v.getId()){
                ...
                }

            }  
        }

        public class ListViewActivity extends Activity implements OnClickListener {

        MyAdapter my_adapter;
        ListView lvMain;
        WebView my_web;

        @Override
        protected void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_list_view);

            MyApplication app = (MyApplication) getApplication();

            my_adapter = new MyAdapter(this, app.sites);

            lvMain = (ListView) findViewById(R.id.lvMain);
            lvMain.setAdapter(my_adapter);

            Log.d("XXX", "setting on click listener for LOAD");

            ...
        }



    // ++++ all clicks must be processed here ++++
    @Override
    public void onClick(View v) {
        MyApplication app;

        switch(v.getId()){

        ...
        }

    }  


}

LNU1, ListViewActivity onClick is not an OnClickListener object, it's an method.

On ListViewActivity, create a listener like

public OnClickListener myClick = new OnClickListener() {
    ...
};

And then use ctx.myClick

Thanks for all !!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.