YosiFZ
YosiFZ

Reputation: 7900

List with buttons

In my app i have a list with custom row,in each row i have a button and i want to set all of the buttons to the same listener. when i define a listener to the button (btn in code) my app get crash this is how i set the list:

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++;
          }
        });

    } 

this is my row 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" /> 

Edit

In this line :

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

btn is null,any idea why it's happen?

Upvotes: 0

Views: 151

Answers (2)

Samir Mangroliya
Samir Mangroliya

Reputation: 40426

in each row i have a button....In your adapter(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();
          }
        });
    }

in Your row.xml

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

Upvotes: 2

Vyacheslav Shylkin
Vyacheslav Shylkin

Reputation: 9801

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

you must use Your_Item_Layout.findViewById(R.id.cellbtn);

Upvotes: 0

Related Questions