Android ListView setOnItemClickListener未触发

Android ListView setOnItemClickListener未触发,android,eclipse,listview,onitemclicklistener,Android,Eclipse,Listview,Onitemclicklistener,单击某个项目时,单击“侦听器不工作” 在此程序中,前两个arraylist使用update()更新。然后将此列表视图发送到其他类以生成列表视图。但是点击每一个项目并不能烤出任何东西 public class BlockActivity extends Activity { ListView lv; ArrayList<String> contactnumber= new ArrayList<String>(); ArrayList<String> contac

单击某个项目时,单击“侦听器不工作”

在此程序中,前两个arraylist使用update()更新。然后将此列表视图发送到其他类以生成列表视图。但是点击每一个项目并不能烤出任何东西

public class BlockActivity extends Activity {
ListView lv;
ArrayList<String> contactnumber= new ArrayList<String>();
ArrayList<String> contactname= new ArrayList<String>();
public static SQLiteDatabase database;
SQLiteDatabase myDB= null;
CustomAdapter adapter;

ArrayList<String> contactnum= new ArrayList<String>();
ArrayList<String> contactnam= new ArrayList<String>();

public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Button pickContact = (Button) findViewById(R.id.button1);






    update();



    lv=(ListView) findViewById(R.id.listView1);

    adapter = new CustomAdapter(this, contactnam, contactnum);
  lv.setAdapter(adapter);


  lv.setOnItemClickListener(new OnItemClickListener() {
      @Override
      public void onItemClick(AdapterView<?> parent, View view,
          int position, long id) 
      {

           /*new AlertDialog.Builder(view.getContext())
              .setMessage("Something here")
              .setNegativeButton("Close", null).show();*/
         Toast.makeText(BlockActivity.this,
                   "Item in position " + position + " clicked",    Toast.LENGTH_LONG).show();

      }
    });


pickContact.setOnClickListener(new OnClickListener() {
.
.
.
公共类BlockActivity扩展活动{
ListView lv;
ArrayList contactnumber=新建ArrayList();
ArrayList contactname=新建ArrayList();
公共静态数据库;
SQLiteDatabase myDB=null;
自定义适配器;
ArrayList contactnum=新的ArrayList();
ArrayList contactnam=新的ArrayList();
创建时的公共void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
按钮选择触点=(按钮)findViewById(R.id.button1);
更新();
lv=(ListView)findViewById(R.id.listView1);
适配器=新的CustomAdapter(this、contactnam、contactnum);
低压设置适配器(适配器);
lv.setOnItemClickListener(新的OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父级、视图、,
内部位置,长id)
{
/*新建AlertDialog.Builder(view.getContext())
.setMessage(“此处的某物”)
.setNegativeButton(“关闭”,null).show()*/
Toast.makeText(BlockActivity.this,
“项目在位置”+位置+“点击”,Toast.LENGTH_LONG.show();
}
});
setOnClickListener(新的OnClickListener(){
.
.
.
main.xml:

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

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="add" />

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</ListView>

detail.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" android:focusableInTouchMode="false" android:focusable="false">


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="50dip"
    android:layout_weight="1" android:focusable="false" android:focusableInTouchMode="false">
    <Button android:id="@+id/button2" android:layout_width="0dp" android:layout_height="wrap_content" android:text="Button"/>

    <TextView
        android:id="@+id/one"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="name"
        android:textSize="20dip" android:layout_gravity="center" android:focusable="false" android:focusableInTouchMode="false"/>

    <TextView
        android:id="@+id/two"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="name"
        android:textSize="20dip" android:layout_gravity="center" android:focusable="false" android:focusableInTouchMode="false"/>
</LinearLayout>

<Button
    android:id="@+id/button1"
    android:layout_width="fill_parent"
    android:layout_height="2dip" android:background="#ff7f7f"/>


如果您在CustomAdapter类中为Button in detail.xml实现了onClik侦听器,请将其删除


因为您不能同时单击listview的项目单击和CustomAdapter的视图单击。

这是因为您的
按钮
,请在列表项布局中添加
android:genderFocusability=“blocksDescendants”

detail.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" android:focusableInTouchMode="false" android:focusable="false">


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="50dip"
    android:layout_weight="1" android:focusable="false" android:focusableInTouchMode="false">
    <Button android:id="@+id/button2" android:layout_width="0dp" android:layout_height="wrap_content" android:text="Button"/>

    <TextView
        android:id="@+id/one"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="name"
        android:textSize="20dip" android:layout_gravity="center" android:focusable="false" android:focusableInTouchMode="false"/>

    <TextView
        android:id="@+id/two"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="name"
        android:textSize="20dip" android:layout_gravity="center" android:focusable="false" android:focusableInTouchMode="false"/>
</LinearLayout>

<Button
    android:id="@+id/button1"
    android:layout_width="fill_parent"
    android:layout_height="2dip" android:background="#ff7f7f"/>

查看
detail.xml中的文本视图和按钮
添加这段代码

android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false"

ListView中可能有多个可单击的视图。 最佳解决方案是:

1) 在列表项的顶级容器布局中使用android:degenantfocusability=“blocksDescendants”

2) 现在,无论listview中的哪个视图需要单击,请使用以下属性:

            android:focusable="false"
            android:focusableInTouchMode="false"

3) 将click listeners设置为视图并使用界面回调,以在单击时分割或激活。列表项的“detail.xml”是否为“detail.xml”?在ListView中添加
android:focusable=“false”android:clickable=“false”
put-In-item布局android:focusable=“false”我将它们设置为false,但没有更改