2015-09-21 44 views
2

单击某个项目时单击侦听器无法工作。ListView setOnItemClickListener不会触发

在这个程序中前两个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() { 
. 
. 
. 

的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"/> 

+0

列表项是“detail.xml”吗? – JoeyJubb

+0

在您的ListView中添加'android:focusable =“false” android:clickable =“false”' –

+0

放入项目布局android:focusable =“false” – Pavan

回答

2

这就是,在你的列表项布局添加android:descendantFocusability="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:descendantFocusability="blocksDescendants" > 


<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"/> 

    <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"/> 
</LinearLayout> 

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

谢谢。它真的解决了我的问题。 – coder65

+0

不客气! – Rami

0

如果您进行了详细实施onClik听者CustomAdapter类按钮.xml然后请 去掉它。

因为同时你不能点击listview的项目点击和customAdaptor的视图点击。因为你Button

0

对于TextViews和巴顿在detail.xml加上这一段代码

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

在可以点击的列表视图中可能有多个视图。 最佳解决方案是:

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

2)现在无论视图列表视图需要点击,请使用以下属性:

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

3)设置点击监听器使用界面上点击片段或活动的意见和回调。

相关问题