2013-03-07 125 views
1

我想要當我檢查複選框代碼返回僅在同一行中的文本視圖的字符串。Toast當在自定義適配器列表視圖中檢查

我正在使用LayoutInflater和另一個具有用於創建包含字符串的列表的代碼。

我的代碼:

public class InteractiveArrayAdapter extends ArrayAdapter<model> { 


private final List<model> list; 
    private final Activity context; 

    public InteractiveArrayAdapter(Activity context, List<model> list) { 
    super(context, R.layout.rep, list); 
    this.context = context; 
    this.list = list; 

    } 


    static class ViewHolder { 
     protected TextView text; 
     protected CheckBox checkbox; 
     } 

    public View getView(int position, View convertView, ViewGroup parent) { 
     View view = null; 
     if (convertView == null) { 
      LayoutInflater inflator = context.getLayoutInflater(); 
      view = inflator.inflate(R.layout.rep, null); 
      final ViewHolder viewHolder = new ViewHolder(); 
      viewHolder.text = (TextView) view.findViewById(R.id.TextView07); 
      viewHolder.checkbox = (CheckBox) view.findViewById(R.id.CheckBox05); 
      viewHolder.checkbox 
       .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 

       @Override 
       public void onCheckedChanged(CompoundButton buttonView, 
        boolean isChecked) { 
        model element = (model) viewHolder.checkbox 
         .getTag(); 
        element.setSelected(buttonView.isChecked()); 

       } 
       }); 
      view.setTag(viewHolder); 
      viewHolder.checkbox.setTag(list.get(position)); 
     } else { 
      view = convertView; 
      ((ViewHolder) view.getTag()).checkbox.setTag(list.get(position)); 
     } 
     ViewHolder holder = (ViewHolder) view.getTag(); 
     holder.text.setText(list.get(position).getName()); 
     holder.checkbox.setChecked(list.get(position).isSelected()); 
     return view; 


     } 
} 

我的xml:

<?xml version="1.0" encoding="utf-8"?> 
    <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<TableLayout 
android:id="@+id/h103" 
android:layout_width="match_parent" 
android:layout_height="27dp" 
android:layout_x="0dp" 
android:layout_y="0dp" 
android:background="@drawable/back1" > 

<TableRow 
    android:id="@+id/TableRow05" 
    android:layout_width="match_parent" 
    android:layout_height="27dp" 
    android:layout_marginTop="4dp" > 

    <CheckBox 
     android:id="@+id/CheckBox05" 
     android:layout_width="20dp" 
     android:layout_height="20dp" 
     android:layout_marginLeft="20dp" 
     android:layout_marginTop="-4dp" 
     android:button="@drawable/test" /> 

    <TextView 
     android:id="@+id/TextView07" 
     android:layout_width="220dp" 
     android:layout_height="22dp" 
     android:layout_marginLeft="23dp" 
     android:layout_marginTop="-6dp" 
     android:text="" /> 

    <TextView 
     android:id="@+id/TextView08" 
     android:layout_width="110dp" 
     android:layout_height="22dp" 
     android:layout_marginLeft="0dp" 
     android:layout_marginTop="-4dp" 
     android:text="095110263" /> 

</TableRow> 

</TableLayout> 

</AbsoluteLayout> 
+0

你有什麼問題? – PCoder 2013-03-07 03:20:22

+0

閱讀我的帖子的第一行:)。 – hesham 2013-03-07 10:53:46

回答

0

您必須對checkbox.onclicklistener工作,而不是checkbox.onCheckedChanged因爲如果你使用它在列表視圖,然後它會自動調用那麼您選中的複選框將會隱藏起來。



    new OnClickListener() 
    { 
     @Override 
     public void onClick(View view) 
     {} 
    } 


在onclicklistener

,你視圖參數是一個複選框。這裏視圖有一個getparent()方法,它返回一個視圖,其中設置了複選框。您可以通過打印日誌來檢查它。如果你有複選框的父母,那麼你也可以得到其中有一個子視圖是複選框的父母的所有子視圖。從所有的孩子的意見,你可以得到值,以顯示他們在吐司。如果複選框的父項在列表項的視圖中有另一個父項,則還可以通過調用getparent()方法獲取父視圖的父項。

請記住,視圖有父子關係,所以如果你想要所有的孩子,那麼你必須得到父母。

+0

我覺得有更容易的形式, – hesham 2013-03-07 11:21:50

+0

請分享您的解決方案。它可以幫助我編寫好的和分佈式的代碼。 – 2013-03-07 16:03:52

0

先加1個按鈕烏爾列表視圖的底部btnOk

btnOk.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) 
      { 
       // TODO Auto-generated method stub 
       StringBuffer responseText = new StringBuffer(); 


       ArrayList<model> almodel = adapter.almodel; 
       responseText.append("The following were selected...\n"); 

       for(int i=0;i<almodel.size();i++) 
       { 
        model m = almodel.get(i); 
        if(m.isSelected()) 
        { 

         responseText.append("\n" + m.getName()); 

        } 

        } 


     Toast.makeText(getApplicationContext(),responseText,Toast.LENGTH_LONG).show(); 
相關問題