2012-02-24 106 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);