0

I was wondering if someone could help me with something as I can't seem to find any documented way to do it, how do you get the view of a column in a list view?

So what I have is a list view, with a custom adapter which has three controls, two textviews and a imageview, what I want to happen is when you press the last column(the image) I want to be able to delete that row, I could do it using the entire row, but I want the user to have to click the imageview column in order to trigger delete.

How do I do this? what do I have to reference to within my OnClickListener in order to check the selected the imageview column?

Thank you for reading

lstNotes.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {


        }
    });

position just references the position of the list row and doesn't allow you to specify a column index or something similar

Pheonix2105
  • 1,001
  • 2
  • 9
  • 24
  • Although it's not exactly what you want, this question and answer may be useful to you. http://stackoverflow.com/questions/1709166/android-listview-elements-with-multiple-clickable-buttons/1776328#1776328 – Squonk Mar 29 '14 at 22:58
  • @Squonk Thank you :), I'll have a read now see if it clears anything for me. – Pheonix2105 Mar 29 '14 at 23:01

1 Answers1

0

You can't detect clicks using lstNotes.setOnItemClickListener - that's purely for item clicks. Instead inside your list view's adapter you should set click listeners for each view item.

You could then use an interface to let your activity know. Something like the following would work:

public interface OnViewClickListener{
    void onTextView1Click();
    void onTextView2Click();
    void onImageViewClick();
}

OnViewClickListener listener;

public CustomAdapter(Context context, int layout, ArrayList<Object> list, OnViewClickListener listener) {
this.listener = listener;
   super(context, layout, list);
}

...

public View getView(int position, View convertView, ViewGroup viewGroup) {

...

    textView1.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View arg0) {
            listener.onTextView1Click();
        }});

    textView2.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View arg0) {
            listener.onTextView2Click();
        }});

    imageView.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View arg0) {
            listener.onImageViewClick();
        }});
}
roarster
  • 4,058
  • 2
  • 25
  • 38
  • I can't quite make use of your answer, I've added all the above to my CustomListViewAdapter and implemented the interface on my fragment, It's now firing off only when I press the image but it's giving me a null pointer exception at listener.onImageViewClick(); (I've removed the other two as I only need to the image one but I imagine they would throw the same errors, I don't understand why I don't have to instantiate the OnViewClickListener? Sorry for the ramble – Pheonix2105 Mar 29 '14 at 23:25
  • @Pheonix2105 I've updated my answer. The listener can be set in your constructor, just add an extra parameter to the end of yours like I've done above. Alternatively you could add a setter method which you call on the adapter to set the listener. – roarster Mar 30 '14 at 07:47