2015-09-21 1 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

"Детальный.xml" для элементов списка? – JoeyJubb

+0

В вашем ListView добавить 'android: focusable =" false " android: clickable =" false "' –

+0

положить в элемент макет android: focusable = "false" – Pavan

ответ

2

Это потому, что вашей Button, добавьте 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.

0

Для TextViews и кнопки в вашем detail.xml добавить этот кусок кода

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

В listviews может быть несколько просмотров, которые можно кликать. Лучшее решение:

1) Использовать android: descendantFocusability = "blocksDescendants" в макете верхнего уровня элемента списка.

2) Теперь в зависимости от того мнения в ListView нужно нажимать, используйте ниже атрибут:

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

3) Установить клики слушателей к мнениям и обратному вызову с использованием интерфейса для фрагмента или деятельности по щелчку.