繁体   English   中英

AdapterView.OnItemClickListener()在我的customAdapter中不起作用

[英]AdapterView.OnItemClickListener() is not working in my customAdapter

请参考这张图片https://www.dropbox.com/s/6zoj9lw10oc07xa/to_dropbox.png

what i am doing :我正在创建一个list_view,在其中添加自定义适配器。

what i am using :我正在使用listView,customAdapter,menuitem。 listView :在整个应用程序单列表视图customadapters :3个的定制适配器menuitem :1

How i am implementing :我有一个数据库,可以从该数据库中正确提取内容,并且通过过滤3种类型的数据在列表视图中输入了这些值:默认情况下(在onCreate中)输入第一个adapter_type。

adapter = new Adapter_forCompletedReminder( array_today_title , this) ;
ls.setAdapter(adapter) ;

按下menuitem在我的列表视图中输入第二个adapter_type。

adapter = new Adapter_forCompletedReminder( array_past_2_day_title , this) ;
ls.setAdapter(adapter) ;

按下menuitem在我的列表视图中输入第三个adapter_type。

adapter = new Adapter_forCompletedReminder( array_other_day_title , this) ;
ls.setAdapter(adapter) ;

what is my problem :此代码添加在onCreate()方法内部。

ls.setOnItemClickListener( new AdapterView.OnItemClickListener() 
{
    public void onItemClick(AdapterView<?> adapterView , View view , int position ,long arg3) 
    {
        Log.i("Item clicked","tushar:itemclicked") ;
    }
});

当我尝试实现AdapterView.OnItemClickListener()时,它无法正常工作……代码没有崩溃(log cat中没有红线)。 单击llist_view_element时代码未执行

谢谢你阅读我的问题。

您使用customview_completedxml_listview.xml中的复选框,这就是为什么onItemClick侦听器不起作用的原因。 如果您在复选框中设置clickable =“ false”,则onItemclick侦听器将起作用。

如果您希望该复选框仍然有效,则必须在自定义适配器类中设置onclicklistener事件。

//我编辑getView

 @Override
  public View getView(int position, View convertView, ViewGroup parent)  
   { 
    LayoutInflater inflater = LayoutInflater.from(ob) ; 
    View v = inflater.inflate(R.layout.customview_completedxml_listview, null ) ; 


     TextView txt = ( TextView ) v.findViewById(R.id.txt_fordisplayingdata) ; 
      txt.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
             Toast.makeText(ob, "Hello", Toast.LENGTH_SHORT).show();

        }
    });
      txt.setText(recieved_Array[position]) ; 

      return v ; 
   } 

////////////////////// //第二个解决方案在复选框中设置了android:focusable =“ false”

     <?xml version="1.0" encoding="utf-8"?> 
      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="match_parent"
       android:layout_height="50dp"
       android:orientation="horizontal" 
       > 

    <TextView 
    android:id="@+id/txt_fordisplayingdata"
    android:layout_width="240dp"
    android:text="display data"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_marginLeft="10dp"
    /> 

  <TextView 
    android:id="@+id/txt_fordisplayingLargerdata"
    android:layout_width="240dp"
    android:text="display data larger bahut larger "
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_marginLeft="10dp"
    android:visibility="gone"
    /> 

  <View
    android:layout_width="2dp"
    android:layout_toRightOf="@id/txt_fordisplayingdata"
    android:layout_height="15dp"
    android:layout_marginLeft="15dp"
    android:layout_centerVertical="true"
    android:id="@+id/view_forcompletedtask"
    /> 


  <CheckBox 
    android:layout_toRightOf="@id/view_forcompletedtask"
    android:id="@+id/checkbox_tocomplete"
    android:layout_marginLeft="15dp"
    android:layout_width="wrap_content"
    android:focusable="false"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    /> 

</RelativeLayout>

您可以尝试以下几种方法:-

  1. 如果您的listview项中有任何按钮(或复选框)或任何元素可以处理click事件,则对每个元素执行以下操作:-

     android:focusable = "false" android:focusableInTouchMode = "false" 
  2. 尝试设定这个

     list.setItemsCanFocus(false); 
  3. 重写onItemClick()方法

     ls.setOnItemClickListener( new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView , View view , int position ,long arg3) { Log.i("Item clicked","tushar:itemclicked") ; } }); 

我真的不能说您到底有什么问题,但是我为您写了一个非常简单的示例。 尝试一下,如果可行,只需将当前项目移植到我的示例项目中即可。 https://docs.google.com/file/d/0Bz4Xd7Ju_kbYbVlyd1dvYTJZYTg/edit?usp=sharingalways

PS:当您完成构想(关于ViewHolder模式)时,建议您阅读有关“ Android最佳实践”的信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM