2016-08-02 15 views
2

Ich entwickle eine Android-Anwendung.Java Android: onItemLongClick für listView reagiert nicht

Ich möchte eine Liste der Kontakte angezeigt werden (unter Verwendung von Listview) und ich möchte 2 Warnungen Dialoge hinzufügen: ein von onItemClickListener und die andere von onItemLongClickListener.

Aber die onItemLongClickListener wird niemals genannt, nur die onItemClickListener. Ich habe es schon für eine andere Anwendung, aber hier funktioniert es nicht und ich finde nicht, warum:/

Mein Code:

public class ContactActivity extends AppCompatActivity { 
    private ListView listView; 
    private ContactAdapter adapter; //my adapter 
    private ArrayList<Contact> contacts; //my list of contacts 

    //... 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_contact); 

     //here i init my "listView" and "contacts" 

     adapter = new ContactAdapter(this, contacts); 
     listView.setAdapter(adapter); 
     adapter.notifyDataSetChanged(); 

     listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { 
      @Override 
      public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { 
       Log.e("longClick", "ok"); 
       return true; 
      } 
     }); 

     listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){ 
      @Override 
      public void onItemClick(AdapterView<?> a, View v, final int position, long id) { 
       //here i display a Dialog 
      } 
     }); 



    //some code 

Also, wenn ich „Klick“ auf einen Artikel der Liste erscheint der Dialog (ok). Aber wenn ich „lange auf“ auf ein Element der Liste, der Dialog erscheint, und ich sehe nicht das Log ...

(Das Element, das ich für meine Listview verwenden bekam nur 2 EditText)

PS: die XML, wo meine Listview

gezeigt
<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" android:layout_height="match_parent" 
    android:background="@color/white"> 

    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:weightSum="100" 
     android:id="@+id/screen"> 

     <FrameLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 

      <ImageView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:id="@+id/imageView4" 
       android:background="@drawable/w_aff2" /> 
     </FrameLayout> 

     <LinearLayout 
      android:orientation="vertical" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:weightSum="100"> 

      <LinearLayout 
       android:orientation="vertical" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_weight="20" 
       android:weightSum="100"> 

       <ListView 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:id="@+id/listView" /> 
      </LinearLayout> 

      <FrameLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="80"> 

      <LinearLayout 
       android:orientation="horizontal" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:weightSum="3"> 

       <FrameLayout 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:layout_weight="1" 
        android:layout_margin="14dp"> 

        <ImageButton 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:id="@+id/button_back" 
         android:layout_gravity="center" 
         android:background="@drawable/grey_button" 
         android:src="@drawable/i_back_blue" /> 
       </FrameLayout> 

       <FrameLayout 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:layout_weight="1" 
        android:layout_margin="14dp"> 

       </FrameLayout> 

       <FrameLayout 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:layout_weight="1" 
        android:layout_margin="14dp"> 

        <ImageButton 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:id="@+id/button_add" 
         android:src="@drawable/i_add_blue" 
         android:background="@drawable/grey_button" 
         android:layout_gravity="center" /> 
       </FrameLayout> 
      </LinearLayout> 
     </FrameLayout> 

    </LinearLayout> 

</LinearLayout> 
    </RelativeLayout> 

PS2: im Log sehen Sie nicht, ich, dass zum Beispiel, aber wenn ich irgendetwas anderes passiert nichts gesagt, nur der Dialog erscheint ...

PS3: Die XML-i

für jedes Element des Listview verwenden
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/item_screen" 
    android:background="@color/white"> 

    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:weightSum="3" 
     android:id="@+id/item_layout" 
     android:layout_marginRight="16dp" 
     android:layout_marginLeft="16dp" 
     android:padding="7dp"> 

     <FrameLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:id="@+id/layout_label"> 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:text="New Text" 
       android:id="@+id/item_label" 
       android:layout_weight="1" 
       android:textColor="@color/grey_dark" 
       android:gravity="center_vertical" 
       android:textStyle="bold" 
       android:textSize="16dp" /> 
     </FrameLayout> 

     <FrameLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:id="@+id/empty_layout"> 

     </FrameLayout> 

     <FrameLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:id="@+id/layout_value" 
      android:paddingTop="8dp" 
      android:paddingBottom="8dp"> 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:text="New Text" 
       android:id="@+id/item_value" 
       android:textColor="@color/grey_dark" 
       android:textStyle="bold" 
       android:gravity="center_vertical|center_horizontal" 
       android:textSize="16dp" /> 
     </FrameLayout> 

    </LinearLayout> 

</LinearLayout> 

SOLUTION ich onSwipeRight hatte() auf meine Zuhörer, es dem onItemLongClickListener

hier blockiert ist der Code, dem Zuhörer versperrte

listView.setOnTouchListener(new OnSwipeTouchListener(getApplicationContext()) { 
    public void onSwipeRight(){ 
     //some code 
    } 
}); 

Big dank ddb für seine Zeit :)

+0

Wie werden Sie das Protokoll zu sehen, Sie tun Fehlerebene Protokoll. –

+1

Können Sie das XML teilen, wo die Liste angezeigt wird? –

+0

müssen Sie wahrscheinlich Logcat-Filter von Debug zu Fehler ändern. – br00

Antwort

1

Warum passieren Sie eine new AdapterView.OnItemLongClickListener?

tun wie dies unten statt

listView.setOnItemLongClickListener(new OnItemLongClickListener() { 
       @Override 
       public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { 
        Log.e("longClick", "ok"); 
        return true; 
       } 
      }); 
+0

Android Studio fügt das "AdapterView" von selbst hinzu, wenn ich es entferne muss ich ** importieren android.widget.Adapteransicht.OnItemLongClickListener ** und es funktioniert immer noch nicht:/ – Option

+0

ok, als versuchen, 'neue ListView.OnItemLongClickListener' zu übergeben – ddb

+0

es funktioniert immer noch nicht :( nur der onItemClickListener wird ausgelöst – Option

0

nicht listView.setLongClickable(true); setzen Sie, listView.setClickable(true); in Ihrem Java.

Weder

android:longClickable="true" 
android:clickable="true" 

In Ihrem xml

+0

Ich habe dies hinzugefügt, weil ich einen Beitrag gesehen habe, der gesagt hat, das zu tun: / – Option