0

I have ListView lv contains a Button b and TextView tv.

I want to handle OnClickListener of Button in an Activty that I created ListView in it. Not in Adapter Class. I can Create listener for button in Adapter but I don't want it.

Anyway?

public class CustomListAdabterSura extends CustomListAdapter {

    private Button button_downloadplay_qiraats; 



    public CustomListAdabterSura(Context context, ArrayList listData) {
        super(context, listData);
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {


        ViewHolder holder;

        if (convertView == null) {
            convertView = super.layoutInflater.inflate(R.layout.list_row_layoutsura, null);

            holder = new ViewHolder();

            holder.b_main_voice=(Button) convertView.findViewById(R.id.b_main_voice);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }


        return convertView;
    }

    static class ViewHolder {

        Button b_main_voice;

    }


}

public MainActivity extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

//handle here

}


}

2 Answers2

0

create and implement onClickListener class on your activity pass it through constructor of adapter then set onClickLister to your button inside adapter

NavinRaj Pandey
  • 1,674
  • 2
  • 26
  • 40
0

You can make interface to notify click Listener to your Activity. so that add argument of interface reference to adapter class constructor.

and implement that interface to your activity. so you can notify your activity via adapter class button click.

You can pass position of clicked item from that click.. and you can perform your action in activity class.

example:

i have found sample in one of SO Answer.

Community
  • 1
  • 1
Sanket Kachhela
  • 10,861
  • 8
  • 50
  • 75