gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:52:40 25 4
gpt4 key购买 nike

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

我在做什么:我正在创建一个 list_view ,我正在其中添加自定义适配器。

我正在使用什么:我正在使用,listView,customAdapter,menuitem。listView : 整个应用程序中的单个 ListView customadapters:3 个自定义适配器菜单项 : 1

我是如何实现的:我有从中正确获取内容的数据库,并且从该数据库中我通过过滤 3 种类型的数据在我的 ListView 中输入了这些值:默认输入第一个 adapter_type(在 onCreate 中)。

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

通过按 menuitem 在我的 ListView 中输入第二个 adapter_type 。

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

通过按 menuitem 在我的 ListView 中输入第三个 adapter_type 。

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

我的问题是什么:此代码添加到 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>

关于android - AdapterView.OnItemClickListener() 在我的 customAdapter 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16913783/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com