2012-02-24 50 views
0

在我的應用程序中,我有一個自定義行的列表,每行都有一個按鈕,我想將所有按鈕設置爲同一個監聽器。當我定義了一個監聽器按鈕(代碼BTN)我的應用程序得到崩潰 這是我的設置列表:帶按鈕的列表

private void updateResultsInUi() {   
     lv = getListView(); 
     lv.setTextFilterEnabled(true); 
     lv.setItemsCanFocus(false); 
     lv.setCacheColorHint(0x00000000); 

     ItemsAdapter adapter = new ItemsAdapter(this,R.layout.item_row, Catalog.instance()); 
     lv.setAdapter(adapter); 

     lv.setTextFilterEnabled(true); 
     lv.setClickable(true); 

     Button btn = (Button)this.findViewById(R.id.cellbtn); 
     btn.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       int l = 0; 
       l++; 
      } 
     }); 

    } 

這是我行的xml:

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout 
    android:layout_height="100dp" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent"> 

    <LinearLayout android:layout_height="wrap_content" 
     android:weightSum="1" android:layout_marginLeft="5dp" 
     android:layout_width="match_parent" 
     android:orientation="horizontal" android:layout_weight="1"> 

     <LinearLayout android:orientation="vertical" 
      android:layout_height="100dp" 
      android:layout_width="wrap_content"> 

      <Button android:id="@+id/cellbtn" 
       android:layout_width="76px" 
       android:layout_height="37px" android:background="@drawable/hazmanacell" 
       android:layout_marginTop="20dp"/> 

     </LinearLayout> 
     <LinearLayout android:layout_height="wrap_content" 
        android:orientation="vertical" android:layout_width="0dp" 
        android:layout_weight="1" android:layout_marginRight="10dp"> 

       <TextView android:textSize="14dp" android:layout_weight="0.50" 
       android:textStyle="bold" android:layout_height="wrap_content" 
       android:id="@+id/toptext" android:text="stam1" 
       android:layout_width="fill_parent" 
       android:gravity="right" android:textColor="@color/black" /> 

       <TextView android:textSize="12dp" android:layout_weight="0.50" 
        android:layout_height="wrap_content" 
        android:text="stam1" android:layout_width="fill_parent" 
        android:id="@+id/centertext" android:gravity="right" 
        android:textColor="@color/black" /> 


     </LinearLayout> 

    </LinearLayout> 
    <ImageView android:id="@+id/imageViewTwitterPic" 
    android:src="@drawable/icon" 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" android:gravity="center_vertical" /> 

編輯

在這一行:

Button btn = (Button)this.findViewById(R.id.cellbtn); 

BTN爲空,任何想法,爲什麼它的發生?

+0

請張貼在logcat的錯誤 – 2012-02-24 14:15:37

+0

我編輯我的帖子.. – MTA 2012-02-24 14:21:14

+0

我知道這很好 – 2012-02-24 14:28:57

回答

2

每一行,我有一個按鈕,在....在您的適配器(ItemsAdapter)

@Override 
    public View getView(final int position, View convertView, ViewGroup parent) { 

     Button btn = (Button)view.findViewById(R.id.cellbtn); 
     btn.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       Toast.makeText(context,"clicked",Toast.LENGTH_SHORT).show(); 
      } 
     }); 
    } 

在你row.xml

<Button 
     android:id="@+id/cellbtn" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:focusable="false" 
     android:textColor="#FFFFFF" /> 
+0

此外,我會建議觀看[this](http://www.youtube.com/watch?v=N6YdwzAvwOA)視頻,特別是部分在列表視圖和性能方面,他詳細解釋瞭如何使用自定義適配器正確管理任何列表視圖。 – Theblacknight 2012-02-24 14:25:52

+0

當我做你喜歡的建議,我錯誤在這一行:'btn.setOnClickListener(新的OnClickListener(){' – MTA 2012-02-24 14:30:52

+0

自己做更多的努力,因爲我給你我的運行代碼... – 2012-02-24 14:32:42

0

爲什麼 Button btn =(Button)this.findViewById(R.id.cellbtn);

您必須使用Your_Item_Layout.findViewById(R.id.cellbtn);