1

i have a button on the listview which i want to click ,but the problem is that on click ,presently it is not able to recognize the listener call whether it is of listview or a button,

thanks in advance... :)

3 Answers3

1

Do not set listener on listview if you don't want to handle click on entire list item. You can set listener for individual view on list item like given in below example:

public class MyArrayAdapter extends ArrayAdapter<Map<String, String>> {

    private Context mContext;

    public MyArrayAdapter(Context context, int textViewResourceId,
            List<Map<String, String>> objects) {
        super(context, textViewResourceId, objects);
        mContext = context;
    }

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

        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(
                    R.layout.list_row, null, false);
        }

        final Map<String, String> data = (Map<String, String>) getItem(position);

        ImageView imageView = (ImageView) convertView
                .findViewById(R.id.imageView);
        imageView.setImageResource(Integer.parseInt(data.get("image")));

        TextView textView = (TextView) convertView
                .findViewById(R.id.textViewTitle);
        textView.setText(data.get("name"));

        Button button = (Button) convertView
                .findViewById(R.id.buttonShow);

        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                mContext.startActivity(new Intent(mContext,
                            DetailActivity.class).putExtra(EXTRA_IMAGE_URL,
                            data.get("url")));
            }
        });

        return convertView;
    }

}
Homam
  • 5,018
  • 4
  • 36
  • 39
0

You need to add the button.setOnClickListener() inside the getView() function of the list adapter.

David Manpearl
  • 12,362
  • 8
  • 55
  • 72
  • i have tried that ,but the problem is that StartActivity is nt recognised in getview() function –  Apr 11 '13 at 05:44
  • 1
    @SajadBinNazir pass the context to custom adapter and call startActivity using that context – Sankar V Apr 11 '13 at 05:50
  • 1
    `startActivity()` may not compile, but `YourActivity.this.startActivity()` or `mContext.startActivity()` (if you use @Homam's answer) will work correctly. – David Manpearl Apr 11 '13 at 05:51
0

First you need create custom Adapter extends ArrayAdapter or BaseAdapter and he has getView method. inside this method you can set OnlickListener to button.

Link example create custom Adapter:

Custom Adapter for List View

http://developer.android.com/reference/android/widget/Adapter.html

http://www.ezzylearning.com/tutorial.aspx?tid=1763429

Community
  • 1
  • 1
SBotirov
  • 13,872
  • 7
  • 59
  • 81