无法在4.1中的弹出窗口中选择listview中的行,但在android 5.0中工作

无法在4.1中的弹出窗口中选择listview中的行,但在android 5.0中工作,android,listview,listviewitem,android-4.1-jelly-bean,Android,Listview,Listviewitem,Android 4.1 Jelly Bean,我的页面中有一个弹出窗口,其中有一个列表视图。我已经在一个单独的xml中创建了弹出窗口的设计,并将其加载到主页中的一些按钮上。弹出窗口有一个列表视图,每行有一个图像和一个文本视图。我无法在android 4.1的listview中获得行选择,但它在5.0中工作。谁能给我建议一下解决办法吗? 列表视图: <ListView android:id="@+id/lstSites" android:layout_width="fill_parent" android

我的页面中有一个弹出窗口,其中有一个列表视图。我已经在一个单独的xml中创建了弹出窗口的设计,并将其加载到主页中的一些按钮上。弹出窗口有一个列表视图,每行有一个图像和一个文本视图。我无法在android 4.1的listview中获得行选择,但它在5.0中工作。谁能给我建议一下解决办法吗? 列表视图:

<ListView android:id="@+id/lstSites"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:fastScrollEnabled="true"
        android:layout_margin="5dp"
        android:descendantFocusability="blocksDescendants"></ListView>
</LinearLayout>

列表视图项目:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants"
android:focusable="false"
android:focusableInTouchMode="false">
<ImageView
    android:id="@+id/thumbImage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:layout_marginRight="10dp"
    android:focusable="false"
    android:focusableInTouchMode="false"/>
<TextView
    android:id="@+id/tvSite"
    android:textSize="20dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:text="Name"
    android:focusable="false"
    android:focusableInTouchMode="false"/>

添加行单击列表器:

lstSiteMenu = (ListView) popupView.findViewById(R.id.lstSites);

            adapter = new MenuItemsAdapter(SiteActivity.this, arrMenu);
            // Attach the adapter to a ListView
            lstSiteMenu.setAdapter(adapter);
            lstSiteMenu.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                                        final int position, long id) {
            }
lstSiteMenu=(ListView)popupView.findViewById(R.id.lstSites);
adapter=new MenuItemsAdapter(SiteActivity.this,arrMenu);
//将适配器连接到ListView
lstSiteMenu.setAdapter(适配器);
lstSiteMenu.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父级、视图、,
最终整数位置,长id){
}

您需要从您的
列表视图中删除
android:genderantfocusability=“blocksDescendants”

同时从
图像视图和
文本视图中的行布局中删除
android:focusableInTouchMode=“false”和
android:focusableInTouchMode=“false”


列表项应该是可单击的,有一个选项可以禁用它获取焦点,从列表视图中删除此属性,
android:genderantfocusability=“blocksDescendants”

从文本和图像视图

试试这个例子

String names[] ={"A","B","C","D"};
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
        LayoutInflater inflater = getLayoutInflater();
        View convertView = (View) inflater.inflate(R.layout.row_file, null);
        alertDialog.setView(convertView);
        alertDialog.setTitle("List");
        ListView lv = (ListView) convertView.findViewById(R.id.listView);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,names);
        lv.setAdapter(adapter);
        alertDialog.show();
字符串名[]={“A”、“B”、“C”、“D”};
AlertDialog.Builder AlertDialog=新建AlertDialog.Builder(MainActivity.this);
LayoutInflater充气机=getLayoutInflater();
视图convertView=(视图)充气器.inflate(R.layout.row_文件,null);
alertDialog.setView(convertView);
alertDialog.setTitle(“列表”);
ListView lv=(ListView)convertView.findViewById(R.id.ListView);
ArrayAdapter=新的ArrayAdapter(这是android.R.layout.simple\u list\u item\u 1,name);
低压设置适配器(适配器);
alertDialog.show();
否则你的代码就可以了,它可以工作

请仔细阅读此文档以了解更多详细信息


另一个同样的解决方案。

尝试所有解决方案:



不要使用lstSiteMenu.setOnItemClickListener

而是转到适配器
getView
并添加

convertView.setOnClickListener


希望它能对您有所帮助!

在适配器类中设置行onclick侦听器

public class MenuItemsAdapter extends Base Adapter{
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
             /*
                          Paste your adapter code
             */
              customView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.v("log", "You clicked at pos "+position);
                }
            });
        }
}
此代码适用于所有sdk版本

还要从xml文件中删除这些属性

android:descendantFocusability="blocksDescendants"
android:focusable="false"
android:focusableInTouchMode="false"

logcat中有任何错误吗?当您说“选定行”时,您的意思是onItemClick不工作或您没有获得位置?请从
列表视图中删除此
android:genderantfocusability=“blocksDescendants”
public class MenuItemsAdapter extends Base Adapter{
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
             /*
                          Paste your adapter code
             */
              customView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.v("log", "You clicked at pos "+position);
                }
            });
        }
}
android:descendantFocusability="blocksDescendants"
android:focusable="false"
android:focusableInTouchMode="false"