Java 仅Android列表列表项单击不工作
我有一个需要点击的新列表,但这个列表不起作用。永远不会调用onListItemClick。我的应用程序中有另一个应用程序按预期工作,我不知道有什么不同。我见过有人说要改变焦点,但我尝试过很多不同的方法,但都没有效果。这里有一些代码 工作:Java 仅Android列表列表项单击不工作,java,android,listview,onclick,Java,Android,Listview,Onclick,我有一个需要点击的新列表,但这个列表不起作用。永远不会调用onListItemClick。我的应用程序中有另一个应用程序按预期工作,我不知道有什么不同。我见过有人说要改变焦点,但我尝试过很多不同的方法,但都没有效果。这里有一些代码 工作: @Override protected void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id
@Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
// get the item that was clicked
v_ProjectInvestigatorSiteContact project = (v_ProjectInvestigatorSiteContact) this.getListAdapter().getItem(
position);
Intent myIntent = new Intent(this, Details.class);
myIntent.putExtra(res.getString(R.string.project), project);
startActivity(myIntent);
}// onListItemClick
不工作:
@Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
// get the item that was clicked
final v_SitePeople vSitePeople = (v_SitePeople) this.getListAdapter().getItem(
position);
AlertDialog.Builder builder = new AlertDialog.Builder(Share.this);
builder.setTitle(res.getString(R.string.forgot_password_check_dialog_title))
.setMessage(res.getString(R.string.share_check_dialog_text))
.setPositiveButton(res.getString(R.string.send), new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
sendShareEmail(vSitePeople);
}
}).setNegativeButton(res.getString(R.string.cancel), new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
// cancelled, so do nothing
}
});
AlertDialog msgBox = builder.create();
msgBox.show();
}// onListItemClick
工作XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffffff"
android:orientation="vertical" >
<!-- dummy item to prevent edittext from gaining focus on activity start -->
<LinearLayout
android:layout_width="0px"
android:layout_height="0px"
android:focusable="true"
android:focusableInTouchMode="true" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/title_background" >
<ImageView
android:id="@+id/logo"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:scaleType="centerCrop"
android:src="@drawable/ic_logo" >
</ImageView>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/logo"
android:gravity="center"
android:paddingBottom="5dp"
android:paddingLeft="50dp"
android:paddingRight="60dp"
android:paddingTop="5dp"
android:text="@string/app_header"
android:textColor="#ffffffff"
android:textSize="15sp"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/search_gradient" >
<ImageView
android:id="@+id/searchBoxIcon"
android:layout_width="38dp"
android:layout_height="38dp"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:scaleType="centerCrop"
android:src="@drawable/action_search" >
</ImageView>
<EditText
android:id="@+id/searchBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="@+id/searchBoxIcon"
android:layout_marginRight="8dp"
android:layout_marginTop="4dp"
android:layout_toRightOf="@+id/searchBoxIcon"
android:background="@drawable/search_box"
android:hint="@string/search_hint"
android:inputType="text"
android:maxLines="1"
android:minHeight="30sp"
android:paddingBottom="2dp"
android:paddingLeft="25sp"
android:paddingTop="2dp"
android:textColor="#ff000000" />
</RelativeLayout>
<ListView
android:id="@id/android:list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@color/divider_gray"
android:cacheColorHint="#00000000"
android:divider="@color/divider_gray"
android:dividerHeight="1dp"
android:footerDividersEnabled="false"
android:headerDividersEnabled="false" />
<TextView
android:id="@+id/android:empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="@string/loading"
android:textColor="@color/loading_gray"
android:textSize="20sp" />
</LinearLayout>
非常感谢您的帮助。您还没有显示初始化列表和适配器的位置,但需要确保已设置onClickListner。比如:
myListView.setOnItemClickListener(this);
编辑:“这”假设初始化是在与单击侦听器出现的类相同的类中完成的。我找到了答案。我从未发布过相关代码,否则我相信你们会帮我找到的 我的不工作适配器从我的工作适配器复制了一个isEnabled方法,该方法禁用了类型为0的项。但由于我的新列表只是一种类型,所以所有内容都被禁用
很抱歉给您添麻烦,谢谢您的努力。使用ListView时,您主要有两种布局: 1) 显示listView的布局 例如:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="?attr/actionBarSize"
android:clickable="false"
>
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="@android:color/transparent"
android:clipToPadding="false"
android:paddingLeft="@dimen/list_left_padding"
android:paddingRight="@dimen/list_right_padding"
android:paddingTop="@dimen/list_separator"
android:dividerHeight="@dimen/list_separator"
android:paddingBottom="@dimen/list_separator" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/white_responsive"
android:descendantFocusability="blocksDescendants"
>
<RelativeLayout
android:id="@+id/sampleItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="3dp"
android:clipToPadding="false">
<com.devspark.robototextview.widget.RobotoTextView
android:id="@+id/textViewSample"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:paddingBottom="5dp"
android:text="Sample Text"
android:textSize="17sp"
app:typeface="roboto_regular" />
</RelativeLayout>
</RelativeLayout>
到布局的根目录
2) 加
到您的项目
您可以分别使用这两个选项。第一个帮助了我。但是,您需要注意将这些属性放在XML文件中的什么位置。将它们放在项目布局中,而不是ListView布局中。我这样说是因为我使用了那个解决方案,但我使用了错误的xml。所以,只要注意,它就会很好地工作。但你的问题是什么?那onListItemClick没有接到电话?是的。我在里面调试了一个断点,但它从未被击中。我会在OP中澄清。你在哪里设置监听器?你能在活动中创建适配器并为列表视图设置侦听器的地方发布代码吗?显然,因为我的id为@android:id/list,所以我不需要侦听器。但这可能会引起冲突,因为我现在用了两次。
myListView.setOnItemClickListener(this);
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="?attr/actionBarSize"
android:clickable="false"
>
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="@android:color/transparent"
android:clipToPadding="false"
android:paddingLeft="@dimen/list_left_padding"
android:paddingRight="@dimen/list_right_padding"
android:paddingTop="@dimen/list_separator"
android:dividerHeight="@dimen/list_separator"
android:paddingBottom="@dimen/list_separator" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/white_responsive"
android:descendantFocusability="blocksDescendants"
>
<RelativeLayout
android:id="@+id/sampleItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="3dp"
android:clipToPadding="false">
<com.devspark.robototextview.widget.RobotoTextView
android:id="@+id/textViewSample"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:paddingBottom="5dp"
android:text="Sample Text"
android:textSize="17sp"
app:typeface="roboto_regular" />
</RelativeLayout>
</RelativeLayout>
android:descendantFocusability="blocksDescendants"
android:clickable="false"
android:focusable="false"