Carlos Pereira
Carlos Pereira

Reputation: 1924

ListView doesn't listens to OnClickListener

I have the following code in my app:

                        AnotherCursorAdapter adapter = new AnotherCursorAdapter(CadItemActivity.this, 
                                                                                R.layout.imgsinternas, 
                                                                                cursorImagens, 
                                                                                new String[] {"nome", "tags",},
                                                                                new int[] { R.id.txtNome, R.id.txtTags });
                        telaScroll.setAdapter(adapter);


    telaScroll.setClickable(true);
    telaScroll.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> av, View v, int pos, long id) {
            Log.d("1212121", "OnClick"); 
            // return false;
        }
    });        

Code for AnotherCursorAdapter:

public class AnotherCursorAdapter extends SimpleCursorAdapter {

    private LayoutInflater inflater;
    public AnotherCursorAdapter(Context context, 
                                int layout, 
                                Cursor c,
                                String[] from, 
                                int[] to) {
        super(context, layout, c, from, to);
        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
             // get the views from the row
             TextView name = (TextView) view.findViewById(R.id.txtNome);
             TextView tags = (TextView) view.findViewById(R.id.txtTags);
             ImageView img = (ImageView) view.findViewById(R.id.figura);
            //asign the values
             name.setText(cursor.getString(4)); 
             tags.setText(cursor.getString(3)); 

             name.setClickable(true);
             tags.setClickable(true);
             img.setClickable(true);


    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        View v = inflater.inflate(R.layout.imgsinternas, null);
        return v;
    }
}

telaScroll is a ListView populated by my database. I am not extending from ListActivity.

The code above DOESN'T work!

The event is not being triggered!

What am I doing wrong? =(

Upvotes: 0

Views: 724

Answers (4)

Kowlown
Kowlown

Reputation: 1166

There is an excellent tutorial for listview with several clickable area written by Cyril Mottier. I strongly recommend you to take a look.

Your current problem occurs because of the buttons which take the focus from the listitem.

Upvotes: 1

Reena
Reena

Reputation: 96

Try setting onClickListener for your text and/or image in your list item while binding.

@Override     
public void bindView(View view, Context context, Cursor cursor) {              
    // get the views from the row         
    TextView name = (TextView) view.findViewById(R.id.txtNome);         
    TextView tags = (TextView) view.findViewById(R.id.txtTags);          
    ImageView img = (ImageView) view.findViewById(R.id.figura);        
    //asign the values          
    name.setText(cursor.getString(4));              
    tags.setText(cursor.getString(3));            
    name.setClickable(true);          
    tags.setClickable(true);           
    img.setClickable(true);

    name.setOnClickListener( new OnClickListener()) {
        public void onClick(View v) {
             // code for performing action on click
        }
    });

    img.setOnClickListener( new OnClickListener()) {
        public void onClick(View v) {
             // code for performing action on click
        }
    });

} 

Upvotes: 1

bigcat_hui
bigcat_hui

Reputation: 41

your are code is right, maybe other view over the listview,and you can't touch the listview.

Upvotes: 0

jeet
jeet

Reputation: 29199

you need to check if your row layouts and views, which you are inflating in getView are not clickable and focusable.

Upvotes: 3

Related Questions