2016-10-14 5 views
0

ボタンを含むリストビューアを作成しました。これは、アダプタボタン付きアンドロイドリストビューは機能しません

のxmlファイルアダプタ部

public class ProductAdapter extends BaseAdapter { 
bAdd.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      //Toast.makeText(v.getContext(),"Button "+position,Toast.LENGTH_SHORT).show(); 
      Cart cart = CartHelper.getCart(); 
      Log.d(TAG, "Adding product: " + product.getName()); 
      cart.add(product, 1); 
      Intent intent = new Intent(context, ShoppingCartActivity.class); 
      context.startActivity(intent); 
     } 
    }); 

ある

public class MainActivity extends AppCompatActivity { 
. 
. 
. 
ListView lvProducts = (ListView) findViewById(R.id.lvProducts); 
lvProducts.addHeaderView(getLayoutInflater().inflate(R.layout.product_list_header, lvProducts, false)); 

    ProductAdapter productAdapter = new ProductAdapter(this); 
    productAdapter.updateProducts(Constant.PRODUCT_LIST); 

    lvProducts.setAdapter(productAdapter); 

    lvProducts.setOnItemClickListener(new OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, 
           int position, long id) { 
      Product product = Constant.PRODUCT_LIST.get(position - 1); 
      Intent intent = new Intent(MainActivity.this, ProductActivity.class); 
      Bundle bundle = new Bundle(); 
      bundle.putSerializable("product", product); 
      Log.d(TAG, "View product: " + product.getName()); 
      intent.putExtras(bundle); 
      startActivity(intent); 
     } 
    }); 

私はボタンをクリックしたときに動作しますが、私は行 をクリックしたとき、これは私のメインの活動ではありません
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="200px" 
android:layout_margin="5dp" 
android:minHeight="50dp" 
android:orientation="horizontal" 
android:weightSum="1"> 

<TextView 
    android:id="@+id/tvProductName" 
    android:layout_width="0dip" 
    android:layout_height="match_parent" 
    android:layout_weight="0.50" 
    android:gravity="center" 
    android:text=""/> 

<ImageView android:id="@+id/ivProductImage" 
    android:layout_width="0dip" 
    android:layout_height="match_parent" 
    android:layout_weight="0.25" 
    android:gravity="center"/> 

<TextView 
    android:id="@+id/tvProductPrice" 
    android:layout_width="0dip" 
    android:layout_height="match_parent" 
    android:layout_weight="0.24" 
    android:gravity="center" 
    android:text=""/> 

<Button 
    android:id="@+id/tvAddItem" 
    android:layout_width="69dp" 
    android:layout_height="wrap_content" 
    android:gravity="center" 
    android:text="A" 
    /> 
</LinearLayout> 
+0

あなたは、アダプタ –

+0

[アンドロイドの可能性のある重複のあなたのXMLコードを貼り付けてくださいすることができますどのように一覧でボタンのonclickイベントを設定する方法ListViewの項目](http://stackoverflow.com/questions/12596199/android-how-to-set-onclick-event-for-button-in-list-item-of-listview) –

+0

あなたは "ボタンをクリックしたときに機能しましたが、行をクリックしたときには機能しませんでした。あなたの問題がボタンであり、それがボタンをクリックしていたら、私は問題が –

答えて

3

でカスタムレイアウトファイルで、あなたのボタンの

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

を設定します。

0

アダプターのレイアウトにはクリックリスナーview.ieが含まれているため、ボタンビューを使用している場合、リストビューのクリックリスナーがブロックされます。だから、あなたは以下のようにブロック子孫を追加必要なアダプタの親のレイアウト、

android:descendantFocusability="blocksDescendants" 
+0

それは動作しません...私はfocusabilityに関する少しの研究をしました...私は以下を試しました... android:focusable = "false" '...それは動作します – beginner

+0

@beginner: OK :) – Jeevanandhan

0

あなたは以下のようなボタンとあなたの親のレイアウトの両方のリスナーを追加する必要があります。

public class ProductAdapter extends BaseAdapter implements View.OnClickListener { 
    bAdd.setOnClickListener(this); 
    layout.setOnClickListener(this); 
    @Override 
      public void onClick(View v) { 
       //Toast.makeText(v.getContext(),"Button "+position,Toast.LENGTH_SHORT).show(); 
       Cart cart = CartHelper.getCart(); 
       Log.d(TAG, "Adding product: " + product.getName()); 
       cart.add(product, 1); 
       Intent intent = new Intent(context, ShoppingCartActivity.class); 
       context.startActivity(intent); 
      } 
     } 
    } 
関連する問題