2012-03-12 70 views
0

我在我的应用程序下面的代码:的ListView不听OnClickListener

     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扩展。

上面的代码不工作!

事件没有被触发!

我在做什么错? =(

+0

您是否使用自定义适配器?你的名单有什么看法。即你的列表是否带有点击监听器的按钮? – Jayabal 2012-03-12 04:47:43

+0

你好巴亚!谢谢回答。我使用的是自定义适配器,而我的listview是一个包含imageview和文本的列表。他们充气。我将它们设置为android:clickable =“true”,但它没有帮助。我想单击图像(也许也是textview)并获取他们的信息。你能帮我吗? – 2012-03-12 04:53:22

+0

可以粘贴更多的代码,我可以看到这是什么问题 – Khan 2012-03-12 04:57:10

回答

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

} 
+0

这个伎俩!谢谢!!!! – 2012-03-12 12:35:11

3

你需要检查,如果你的行布局和视图,您在getView充气无法点击,而且可成为焦点。

+0

你好,我的朋友。谢谢回答。是的,我所有的商品都是可点击和可聚焦的=“真”。有任何想法吗? =) – 2012-03-12 04:58:15

+1

更改这些项目clciable = false;因为当你点击这些项目时你听不到事件 – jeet 2012-03-12 04:59:57

0

您的均码是在列表视图对的,也许另一种观点,你可以” t触列表视图。

+0

我不这么认为,因为我可以滚动浏览ListView,所以看起来我可以触摸它。 – 2012-03-12 05:03:04

+0

你好:这个链接[listview演示](http://bigcateasymorse.googlecode.com/svn/trunk/androidlistviewdemo0.1/)是一个列表演示。我认为你是适配器代码有一些错误。你应该使用getView方法替换bindview和newView方法。 – 2012-03-12 05:24:01

相关问题