ListView不会侦听OnClickListener

时间:2012-03-12 04:42:39

标签: android

我的应用中有以下代码:

                        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;
        }
    });        

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是我的数据库填充的ListView。我没有从ListActivity扩展。

上面的代码不起作用!

事件未被触发!

我做错了什么? =(

4 个答案:

答案 0 :(得分:3)

你需要检查你在getView中膨胀的行布局和视图是不可点击和可聚焦的。

答案 1 :(得分:1)

尝试在绑定时为列表项中的文本和/或图像设置onClickListener。

@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
        }
    });

} 

答案 2 :(得分:1)

Cyril Mottier撰写的listview with several clickable area精彩教程。我强烈建议你去看看。

出现当前问题是因为从listitem获取焦点的按钮。

答案 3 :(得分:0)

您的代码是正确的,也许是列表视图上的其他视图,您无法触摸列表视图。