2016-10-25 1 views
0

Wenn ich auf die Schaltfläche klicken, wird keine Nachricht angezeigt, aber wenn ich auf das Listenansichtselement klicke und dann auf die Schaltfläche klickt, wird die Nachricht angezeigt.Schaltfläche von benutzerdefinierten ListView reagiert nicht

was ist wollen, ist meine Datenbank zu aktualisieren, indem Sie auf die Schaltfläche jeder listview Elemente klicken. Ich habe android:focusable="false" android:focusableInTouchMode="false" in list_items.xml Taste verwendet.

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

      Button saleBtn = (Button) view.findViewById(R.id.sale_btn); 
      if (saleBtn != null) { 
       saleBtn .setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View view) { 
         Toast.makeText(MainActivity.this, "clicked", 
           Toast.LENGTH_SHORT).show(); 
        } 
       }); 
      } 
     } 

    }); 

Der Artikel Layout für list_items.xml sich wie folgt:

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

    android:orientation="horizontal"> 


    <ImageView 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" /> 

    <Button 
     android:id="@+id/sale_btn" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:focusable="false" 
     android:focusableInTouchMode="false" 
     android:text="Sale" 
     android:textAppearance="?android:textAppearanceSmall" 
     android:textStyle="bold" /> 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="3" 
     android:orientation="vertical"> 


     <TextView 
      android:id="@+id/product_text_view" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="8dp" 
      android:layout_marginRight="8dp" 
      android:layout_marginTop="8dp" 
      android:fontFamily="sans-serif-medium" 
      android:textAppearance="?android:textAppearanceSmall" 
      android:textColor="#2B3D4D" 
      android:textStyle="bold" 
      tools:text="Product" /> 

     <TextView 
      android:id="@+id/quantity_text_view" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="8dp" 
      android:layout_marginRight="8dp" 
      android:layout_marginTop="8dp" 
      android:fontFamily="sans-serif-medium" 
      android:textAppearance="?android:textAppearanceSmall" 
      android:textColor="#2B3D4D" 
      android:textStyle="bold" 
      tools:text="Quantity" /> 

     <TextView 
      android:id="@+id/price_text_view" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="8dp" 
      android:layout_marginRight="8dp" 
      android:layout_marginTop="8dp" 
      android:fontFamily="sans-serif-medium" 
      android:textAppearance="?android:textAppearanceSmall" 
      android:textColor="#2B3D4D" 
      android:textStyle="bold" 
      tools:text="Price" /> 
    </LinearLayout> 
</LinearLayout> 
+0

Bitte geben Sie Ihre Artikel Layout. –

+0

http://pastebin.com/i1emQfn8 Dies ist der Link für die list_item.xml – osh

+0

Sieht gut aus, haben Sie versucht, diesen Code in einem Schritt debuggen? –

Antwort

0

haben Sie

versucht
View button = productListView.getChildAt(position).findViewById(R.id.sale_btn); 
button.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         Toast.makeText(MainActivity.this, "clicked", 
          Toast.LENGTH_SHORT).show(); 
        } 
       }); 

stattdessen den Knopf zu finden, die Aussicht und die ID mit denen nur prompt Toast, wenn Sie drücken die erste Schaltfläche in der Liste, die Sie anstelle der einzelnen Schaltfläche in der Liste mit getChildAt() erstellt haben, können Sie die einzelne Schaltfläche in der Liste abrufen

+0

Danke für die Hilfe. Ja, ich habe das auch versucht. gleicher Ausgang – osh

+0

@osh huh das ist komisch du verwendest Listenansicht richtig ?? – Annonymous177

+0

ja. Hier ist die .java-Datei http://pastebin.com/dPbzMR9Y – osh

Verwandte Themen