Список с кнопками - PullRequest
       1

Список с кнопками

0 голосов
/ 24 февраля 2012

В моем приложении у меня есть список с пользовательской строкой, в каждой строке у меня есть кнопка, и я хочу установить все кнопки для одного и того же слушателя.когда я определяю слушателя кнопки (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 равно нулю, есть идеи, почему это произошло?

Ответы [ 2 ]

2 голосов
/ 24 февраля 2012

в каждой строке у меня есть кнопка .... В вашем адаптере (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 голосов
/ 24 февраля 2012

Почему Button btn = (Button) this.findViewById (R.id.cellbtn);

вы должны использовать Your_Item_Layout.findViewById (R.id.cellbtn);

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...