【问题标题】:Listview onitemclick listener is not workingListview onitemclick 监听器不工作
【发布时间】:2013-11-21 13:14:05
【问题描述】:

我正在使用需要创建 onItemClick 方法的 Android 应用程序,但我没有这样做。下面是 onItemClick 方法不起作用的代码。有谁知道为什么它不起作用?

public class sample extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.live_chat_screen);

        mListview = (ListView) findViewById(R.id.mLiveview);
        mListview.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapters, View childView,int position, long arg3) {
                // TODO Auto-generated method stub
                Toast.makeText(sample.this,"Item number : " + position + " clicked",Toast.LENGTH_LONG).show();
            }
        });

这里是xml:

<ListView
     android:id="@+id/mLiveview"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"    
     android:fastScrollEnabled="true"           

     android:scrollbars="vertical" >
</ListView>

实际上,我在我的应用程序中首先使用了两个视图,即当应用程序打开时,另一个视图在列表视图加载时打开。

谁能帮帮我。

【问题讨论】:

  • 为什么它不起作用?代码很完美。在onItemClick 中添加一些日志,以便您知道该项目已被点击。
  • 你遇到什么样的错误?
  • 列表中有项目吗?写Toast.makeText(YourActivity.this,"Item number : " + position + " clicked",Toast.LENGTH_LONG).show();
  • @AndroidKiller 我尝试了上面的代码但我得到了响应,实际上我在我的应用程序中使用了两个视图
  • @priya NOT WORKING 难以置信地无用,发布一些日志(如果有),放置 List Adapter 的代码如何添加 listview 项目...

标签: android android-listview


【解决方案1】:

您没有在活动中设置布局。首先使用 setContentView() 设置您的 xml,如下所示:

setContentView(R.layout.your_xml);

然后继续

mListview = (ListView) findViewById(R.id.mLiveview);

【讨论】:

    【解决方案2】:

    将此代码添加到您的布局中,然后重试;

    android:clickable="true"
    android:focusable="true"
    

    【讨论】:

      【解决方案3】:

      在您的 ListView 中试试这个

       <ListView
                      android:id="@+id/list"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:focusable="true"
                      android:descendantFocusability="blocksDescendants" 
                      android:orientation="horizontal"
                      android:scrollbars="vertical"
                      android:smoothScrollbar="true" />
      

      如果您使用自定义ListView,例如使用复选框,请使用

         android:focusable="false"
         android:focusableInTouchMode="false" 
      
      
       <CheckBox
          android:id="@+id/chk_delete"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentRight="true"
          android:focusable="false"
          android:focusableInTouchMode="false" />
      

      【讨论】:

        【解决方案4】:

        你忘记的主要是

        setContentView(R.layout.activity_menus);
        

        【讨论】:

        • 嗨,gaurav,不要急于回答。只提供她想要的。在您的代码中,谁知道,什么是产品,您为什么要打开活动?所以,只需用一些日志编辑你的答案,让提问者简单理解。 :)
        • @TarunDroid priya 代码的问题是她忘记设置 contentview
        • @TarunDroid 是的,在她的代码中 setContentView 不是 thr。
        猜你喜欢
        • 2012-11-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多