2011-12-07 13 views
3

私が見つけたのはherehereですが、問題は少し異なります。android:ListViewのチェックボックス(選択した要素の奇妙な動作)

ListViewでは、コンテンツのチェックボックスにアダプタ(基底アダプタから拡張)を挿入しようとします。

リスト表示レイアウト:リストの要素の

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    style="@style/mainLayout" 
> 

    <Button 
     android:id="@+id/close_cat" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:onClick="buttonClick" 
     android:text="@string/back" /> 

    <ListView 
     android:id="@android:id/list" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" /> 

</LinearLayout> 

XML:

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

    <TableLayout 
     android:id="@+id/blocCheck" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="25px" 
     android:layout_marginTop="25px" 
     android:background="@color/black" > 

     <TableRow 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:baselineAligned="true" 
      android:paddingLeft="4sp" > 

      <LinearLayout 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center_vertical" 
       android:layout_weight="1" 
       android:orientation="horizontal" 
       android:paddingLeft="20sp" > 

       <ImageView 
        android:id="@+id/iv_cat" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" /> 

       <TextView 
        android:id="@+id/category" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_gravity="left" 
        android:layout_marginLeft="15dip" 
        android:text="tfgldkjhglf" 
        android:textSize="20sp" /> 
      </LinearLayout> 

      <CheckBox 
       android:id="@+id/check_cat" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="right" 
       android:layout_marginRight="10sp" 
       android:onClick="MyHandler" /> 
     </TableRow> 
    </TableLayout> 

</LinearLayout> 

私は、私の見解では、アダプタを設定します。

list = getListView(); 
list.setAdapter(new CatAdapter(this, listCat)); 

マイアダプタ:

public class CatAdapter extends BaseAdapter { 

    private LayoutInflater mInflater; 
    private List<CatAdapterObject> mListAppInfo; 
    private Context mContext; 


    /** 
    * Constructor 
    * @param context 
    * @param data 
    * @param resource 
    * @param from 
    * @param to 
    */ 
    public CatAdapter (Context context, List<CatAdapterObject> list){ 
     mContext = context; 
     mListAppInfo = list; 
     mInflater = LayoutInflater.from(mContext);  
    } 

    /** 
    * number of elements 
    */ 
    @Override 
    public int getCount() { 
     return mListAppInfo.size(); 
    } 

    /** 
    * get an item in the list 
    */ 
    @Override 
    public Object getItem (int position){ 
     return mListAppInfo.get(position).getId(); 
    } 

    /** 
    * get id of the selected item 
    */ 
    @Override 
    public long getItemId(int position) { 
     return mListAppInfo.get(position).getId(); 
    } 

    /** 
    * get the view 
    */ 
    @Override 
    public View getView (int position, View convertView, ViewGroup parent){ 

     LinearLayout layoutItem; 

     if (convertView == null) { 
      layoutItem = (LinearLayout) mInflater.inflate(R.layout.category, parent, false); 
     } else { 
      layoutItem = (LinearLayout) convertView; 
     } 

     // get the current category 
     CatAdapterObject entry = mListAppInfo.get(position); 

     //view setting 
     TextView category = (TextView) layoutItem.findViewById(R.id.category); 
     CheckBox cb   = (CheckBox) layoutItem.findViewById(R.id.check_cat); 
     ImageView iv_cat = (ImageView) layoutItem.findViewById(R.id.iv_cat); 

     //elements setting 
     category.setText(entry.getName()); 

     //checkbox 
     cb.setChecked(entry.isChecked()); 
     cb.setTag(position); 

     //picture 
     Bitmap icon = Utils.getResizedBitmap(BitmapFactory.decodeResource(mContext.getResources(),entry.getPict()),45,45,true);  
     iv_cat.setImageBitmap(icon); 

     return layoutItem; 

    } 
} 

アクティビティが実行されると、リストが完全に表示されます。私は問題なく上下にスクロールすることができます。しかし、チェックボックスの1つをチェックしてスクロールダウンすると、「緩い」とチェックされたボックスとそのリストの最新のボックスの背景色が変更されました。ボックスをチェックすると、法の下に(私の活動のファイルでは)と呼ばれている

public void MyHandler(View v) { 

     //クリックされたcheckbox 
     cb = (CheckBox) v; 

     Log.d("CHECKBOX","before :"+Utils.implode(",",this.cat_id_list)); 

     //get position in the list 
     Integer position = Integer.parseInt(cb.getTag().toString()); 

     //instance of the view (list's child) 
     View o = list.getChildAt(position).findViewById(R.id.blocCheck); 

     // check if box is checked 
     if (cb.isChecked()) { 
      o.setBackgroundResource(R.color.pink); 

      this.cat_id_list.add(position.toString()); 

     // 背景色を替え、リストから削除 
     } else { 

      o.setBackgroundResource(R.color.black); 

      Collections.sort(this.cat_id_list); 
      int index = Collections.binarySearch(this.cat_id_list,position.toString()); 
      this.cat_id_list.remove(index); 

     } 

     Log.d("CHECKBOX","after :"+Utils.implode(",",this.cat_id_list)); 
    } 

私のリストでは8つの要素がありますが、私は、リストの「mChildrenCount」をチェックしたときに、5だけがあります要素。

私は、チェックされた要素の "this.cat_id_list" idに保存し、選択した項目の背景色を変更したいだけです。

私はあなたの助けに感謝します!

ListView lv = ((ListActivity)context).getListView(); 
// Containing all check states 
SparseBooleanArray sba = lv.getCheckedItemPositions(); 

CheckBox cb = (CheckBox) convertView.findViewById(R.id.yourcheckbox); 

cb.setChecked(false); 

if(sba != null) 
    if(sba.get(position)) 
    cb.setChecked(true); 

アクティビティ側で必要なものにこれを言った:あなたのgetViewメソッドで

答えて

4

あなたが(このコードはあなたにそれがあなたのケースで動作するかのアイデアを与えるである)このようなものを持っています。

xmlのandroid:choiceModeまたはsetChoiceModeを使用して、ListViewを複数選択モードにしました。チェックボックスはクリック不可でフォーカス不可能にする必要があります。

ボタンのonClickリスナーを削除する必要があります。 onClick of buttonで何をしていても、そのロジックをListActivtiyのonListItemClickに追加する必要があります。

+0

ありがとうございます。私のXMLリストに対して "android:choiceMode =" multipleChoice ""を設定し、リストをクリックできないように設定して(list.setItemsCanFocus(false);)、getViewメソッドでコードを試しましたが、カーソルを設定する方法は分かっています。私は検索しているが、私はSparseBooleanArrayのロジックを理解していない。あなたは私に詳細を教えてくれますか? – johann

+0

いいえ?私はarrayAdapterとsimpleAdapter..withoutを成功させようとしました。 – johann

+0

CheckBoxをクリックできないアイテムをリストアイテムにしています....また、CursorAdapterを使用している場合は、コードを更新しました。カーソルは1つです。 – havexz

3

私も同じ問題を抱えていました。私は以下の方法で解決しました。
checkboxのcheckedプロパティを設定するためにBeanクラスを作成し、ArrayAdapterを使用しました。 ArrayAdpaterは、Beanクラスを拡張します。

Beanクラス

public class MCSSCheckState 
{ 
    private boolean isChecked; 
    public boolean getIsChecked() 
    { 
     return isChecked; 
    } 
    public void setIsChecked(boolean isChecked) 
    { 
     this.isChecked = isChecked; 
    } 
} 

Javaクラス

ArrayList<MCSSCheckState> mTitleList = new ArrayList<MCSSCheckState>(); 
MCSSCheckState check_state=new MCSSCheckState(); 
check_state.setIsChecked(false); 
mTitleList.add(i, check_state); 
ListAdapters adapter = new ListAdapters(this,R.id.camTitleTextView, mTitleList); 
ListView mListView = (ListView) findViewById(R.id.cameraListView); 
mListView.setAdapter(adapter); 

アダプタクラス

private class ListAdapters extends ArrayAdapter<MCSSCheckState> 
{ 
    private ArrayList<MCSSCheckState> items; 
    private int position;  
    public ListAdapters(Context context, int textViewResourceId, 
         ArrayList<MCSSCheckState> mTitleList) 
         { 
          super(context, textViewResourceId, mTitleList); 
          this.items = mTitleList; 
         } 
         @Override 
         public View getView(int position, View convertView, ViewGroup parent) 
         { 
          View v = convertView; 
          this.position = position; 
          if (v == null) 
          { 
           LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
           v = inflater.inflate(R.layout.camerslistinflater, null); 
          } 
          MCSSCheckState o = (MCSSCheckState) items.get(position); 
          if (o != null) 
          { 
           checkBox = (CheckBox) v.findViewById(R.id.checkBox1); 
           checkBox.setTag(position); 
           checkBox.setChecked(o.getIsChecked()); 
           checkBox.setOnClickListener(new OnClickListener() 
           { 
            @Override 
            public void onClick(View v) 
            { 
             MCSSCheckState obj = (MCSSCheckState) items.get(Integer.parseInt(v.getTag().toString())); 
             obj.setIsChecked(((CheckBox)v).isChecked()); 
             items.set(Integer.parseInt(v.getTag().toString()), obj);      
            } 
           }); 
          } 
          return v; 
         }); 
} 

これはあなたのお役に立てば幸いです。

+0

あなたのコードをありがとう。私はあなたの豆のクラスを理解していません。なぜSparseBooleanArrayクラスを使用しないのですか?私のリストでは、チェックボックスだけでなく、写真とテキストも設定したいと思っています。 – johann