How to set onClickListener for separate parts of a custom listView element? [Android]

I created my own listView for my Android application, and I had a problem creating separate onClickListeners for the individual parts of the element. My item has an image and text. I want to start different actions depending on which one was clicked.

This onClick () method shoud triggers an action that makes it impossible to define in the getView () method of my DataBinder class. (DataBinder redirects my viewlist with custom layout)

Any help?

Thank!

+5
source share
3 answers

ListAdapter's getView onClickListeners -, .

, getView:

class CustomListAdapter extends ArrayAdapter<String> implements OnClickListener {

    public CustomListAdapter(Context context, int textViewResourceId) {
        super(context, textViewResourceId);
    }

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

        TextView tv = (TextView) v.findViewById(R.id.textView1);
        tv.setOnClickListener(this);

        ImageView iv = (ImageView) v.findViewById(R.id.imageView1);
        iv.setOnClickListener(this);

        return super.getView(position, convertView, parent);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.textView1:
            // Do stuff accordingly...
            break;
        case R.id.imageView1:
            // Do stuff when imageView1 is clicked...
        default:
            break;
        }
    }
}
+9

onClick() ImageView TextView . , onClick() ListView.

onClick() getView() .

+1

One option is to enable the onClick method for individual elements. Assuming you built a custom string in XML, just add a method to the onClick field, set this element (say an image) to allow clicks (if it is not already set) and define a method in your class, Then, if you click on the line, the handler clicks listview fires, but if an element receives a click (image), then its own onClick method starts.

0
source

All Articles