Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android OnItemClickListener未在自定义ListView上触发_Android_Listview_Adapter_Onitemclicklistener_Onitemclick - Fatal编程技术网

Android OnItemClickListener未在自定义ListView上触发

Android OnItemClickListener未在自定义ListView上触发,android,listview,adapter,onitemclicklistener,onitemclick,Android,Listview,Adapter,Onitemclicklistener,Onitemclick,我在ListView上使用OnItemClick Listener时遇到问题,未触发OnItemClick重写的方法。。。这是我的密码: public void popupCatCategories(){ LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE); View popupCatCategoriesView =

我在ListView上使用OnItemClick Listener时遇到问题,未触发OnItemClick重写的方法。。。这是我的密码:

public void popupCatCategories(){
    LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);  
    View popupCatCategoriesView = layoutInflater.inflate(R.layout.popup_cats_categories, null);  
    final PopupWindow popupWindow = new PopupWindow(popupCatCategoriesView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

    Button btnDismiss = (Button) popupCatCategoriesView.findViewById(R.id.dismiss);

    btnDismiss.setOnClickListener(new Button.OnClickListener(){
        public void onClick(View v) {
            // TODO Auto-generated method stub
            popupWindow.dismiss();
        }
    });

    // listview to display image and text
    ListView listCatCategories = (ListView) popupCatCategoriesView.findViewById(R.id.listCatCategories);

    List<HashMap<String, String>> listCategories = new ArrayList<HashMap<String, String>>();
    db.open();
    Cursor catCategories = db.getAllCatCategoryList();
    if (catCategories.moveToFirst())
    {
        do {                
            HashMap<String, String> hm = new HashMap<String, String>();
            hm.put("catCategoryThumbnail", Integer.toString(catCategories.getInt(1)));
            hm.put("catName", catCategories.getString(0));
            listCategories.add(hm);
        } while (catCategories.moveToNext());
    }
    db.close();

    listCatCategories.setAdapter(new ItemListBaseAdapter(getApplicationContext(), listCategories));

    Log.i("got it?", "wait for it...");
    listCatCategories.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> a, View view, int position, long id) {
            Log.i("got it?", "yea, got it!");
            Toast.makeText(getBaseContext(), "got it", Toast.LENGTH_SHORT).show();
        }
    });

    popupWindow.showAsDropDown(btnCats, 50, -330);
}
public void popupCatCategories(){
LayoutFlater LayoutFlater=(LayoutFlater)getBaseContext().getSystemService(布局\充气器\服务);
视图PopupCategoriesView=LayoutFlater.inflate(R.layout.popup\u cats\u categories,null);
final PopupWindow PopupWindow=新的PopupWindow(popupCatCategoriesView,LayoutParams.WRAP_内容,LayoutParams.WRAP_内容);
按钮btnDismiss=(按钮)popupCatCategoriesView.findViewById(R.id.Disease);
btnDismiss.setOnClickListener(新建按钮.OnClickListener(){
公共void onClick(视图v){
//TODO自动生成的方法存根
popupWindow.disclose();
}
});
//listview以显示图像和文本
ListView ListCategories=(ListView)PopupCategoriesView.findViewById(R.id.ListCategories);
List listCategories=新建ArrayList();
db.open();
游标catcatcategories=db.getAllCatCatCatCategoriList();
if(catCategories.moveToFirst())
{
做{
HashMap hm=新的HashMap();
hm.put(“catcatcategorithumbnail”,Integer.toString(catCategories.getInt(1));
hm.put(“catName”,catCategories.getString(0));
列表类别。添加(hm);
}while(catcatcategories.moveToNext());
}
db.close();
setAdapter(新的ItemListBaseAdapter(getApplicationContext(),listCategories));
我(“明白了吗?”,“等等…”);
setOnItemClickListener(新的OnItemClickListener(){
@凌驾
public void onItemClick(适配器视图a、视图视图、整型位置、长id){
我(“明白了?”,“是的,明白了!”);
Toast.makeText(getBaseContext(),“明白了”,Toast.LENGTH_SHORT.show();
}
});
弹出窗口显示下拉列表(BTNCAT,50,-330);
}
我只是在HashMap的ArrayList中添加一些值,每个HashMap包含2个字符串变量,它们显示在ListView的每一行中。ListView的自定义xml布局的RelativeLayout中只有一个ImageView和一个TextView。它显示的很好,只是我点击了一行,什么都没有发生,它不会触发日志或任何东西。重写的方法onItemClick根本不会被触发

我已经尝试在ListView上设置Clickable(true)或setItemsCanFocus(false),或者在ImageView和TextView上设置Focusable(false)和setFocusableInTouchMode(false)(即使它适用于复选框和按钮,对吗?)

以下是ListView的自定义xml:

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


    <ImageView
        android:id="@+id/catCategoryThumbnail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingBottom="10dp"
        android:paddingRight="10dp"
        android:paddingTop="10dp" />

    <TextView
        android:id="@+id/txtCatCategoryName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="15dp"
        android:focusable="false"
        android:focusableInTouchMode="false" />

</RelativeLayout>

不管怎样,我不知道该怎么办了,有人能帮我吗


如果你需要更多的信息,请告诉我。谢谢

您的视图不接受事件的原因是TextView(可聚焦)正在覆盖它,并为自己执行所有触摸/点击操作

您只需为TextView设置以下内容:

android:focusable=“false”


android:focusableInTouchMode=“false”
找到了另一种解决方案:。它也可以正常工作。

您是否尝试过:listView.setChoiceMode(listView.CHOICE\u MODE\u SINGLE);我只是尝试了一下,还是什么都没有…你在代码中动态地完成了textView.setFocusable(flase)吗?尝试一下,因为几天前它对我不起作用(只是以xml文件的形式提供)。只有当我通过动态编码实现这一点时,它才起作用。是的,我尝试过,但仍然不起作用:-/我想我尝试了其他问题上给出的所有可能的解决方案。我已经尝试过了,它仍然不起作用。onItemClick仍然没有触发:(这是我实现列表的方式,但我不想建议进行如此广泛的重写;)我更喜欢这种方式,而不是白白浪费一天的时间lol我已经完成了,所以:)@Saran你实现了你的列表,以至于在选择行时仍然获得单击效果吗?是的,我做到了。我已经扩展了ArrayAdapter,并且必须处理行(以及行上的文本)的onTouch事件。对于MotionEvent.ACTION\u DOWN/MOVE,我刚刚更改了背景,对于MotionEvent.ACTION\u CANCEL,我将背景更改回默认值,对于MotionEvent.ACTION\u UP,我执行了实际操作。当然,如果您想要长按操作,也必须实现onItemLongClickedCallback。