2011-10-19 13 views
0

私はSingleSelectListトラブル

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:descendantFocusability="blocksDescendants" 
    > 
    <CheckBox 
     android:id="@+id/chk" 
     android:button="@drawable/q_list_check_box" 
     android:layout_alignParentLeft="true" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="5dp" 
     android:layout_marginRight="5dp" 
     android:layout_gravity="center_vertical" 
     android:gravity="center_vertical" 
     /> 
    <TextView 
     android:id="@+id/txtChoice" 
     android:textColor="@color/white" 
     android:layout_gravity="center_vertical|left" 
     android:gravity="center_vertical|left" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 
</LinearLayout> 

のように、選択した

public class SingleList extends ListView { 

    int selected; 
    List<String> data; 

    public SingleList(Context context) { 
     super(context); 

     this.selected = -1; 
     this.data = new ArrayList<String>(); 
    } 

    public SingleList(Context context, AttributeSet attributes) { 
     super(context, attributes); 

     this.selected = -1; 
     this.data = new ArrayList<String>(); 
    } 

    public int getSelectedIndex() { 
     return this.selected; 
    } 

    public void setSelectedIndex(int selected) { 
     this.selected = selected; 
    } 

    public List<String> getData() { 
     return this.data; 
    } 

    public void setData(List<String> data) { 
     this.data = data; 
     this.selected = -1; 
    } 

    public String getSelectedValue() { 
     if (this.selected > -1) 
      return this.data.get(selected); 
     return ""; 
    } 

    public void addDataItem(String item) { 
     this.data.add(item); 
    } 

    public void initialize_list(List<String> data) { 
     setData(data); 
     this.setAdapter(new SingleListAdapter()); 
    } 

    private class SingleListAdapter extends BaseAdapter { 

     public SingleListAdapter() { 
     } 

     @Override 
     public int getCount() { 
      return data.size(); 
     } 

     @Override 
     public Object getItem(int position) { 
      return data.get(position); 
     } 

     @Override 
     public long getItemId(int position) { 
      return data.get(position).hashCode(); 
     } 

     @Override 
     public View getView(final int position, final View convertView, 
       final ViewGroup parent) { 

      View view = convertView; 

      if (view == null) { 
       LayoutInflater inflater = (LayoutInflater) getContext() 
         .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       view = inflater.inflate(R.layout.q_list_row_text, null); 

      } 
      final CheckBox chkChoice = (CheckBox) view.findViewById(R.id.chk); 
      view.setOnClickListener(new OnClickListener() { 

       @Override 
       public void onClick(View v) { 

        if (selected != -1) { 
         View tempView = SingleListAdapter.this.getView(
           selected, convertView, parent); 
         CheckBox tempChk = (CheckBox) tempView 
           .findViewById(R.id.chk); 
         tempChk.performClick(); 
        } 

        chkChoice.performClick(); 
        selected = position; 
       } 
      }); 

      chkChoice.setOnCheckedChangeListener(new OnCheckedChangeListener() { 

       @Override 
       public void onCheckedChanged(CompoundButton buttonView, 
         boolean isChecked) { 

        if (isChecked) { 
         ((View) chkChoice.getParent()) 
           .setBackgroundResource(R.drawable.backgroun_checked); 
        } else { 
         ((View) chkChoice.getParent()) 
           .setBackgroundResource(R.drawable.background_unchecked); 
        } 

       } 
      }); 

      TextView txtChoice = (TextView) view.findViewById(R.id.txtChoice); 
      txtChoice.setText(data.get(position)); 

      return view; 
     } 
    } 

} 

// int型のようなSingleSelectListのコードでcustomrowとリストビューを拡張することによってSingleSelectリストを構築しようとしていますが、現在保持しているonclicklistenerチェックされた項目をクリックし、その行をチェック解除してnewoneをチェックするためにクリックする行にしたい。 誰かが私が間違っていることを私に知らせることはできますか?

+0

あなたは何をしたいですか?リスト行cilkのcheckBoxをチェックするかチェックを外すだけですか? – user370305

+0

@ user370305はい、チェックされたCheckBoxを含む行の背景を他の色に変更します(R.drawable_background_checkedは黄色で、R.drawable_background_uncheckedは透明です)。現時点では色は変わりますが、複数チェックボックスがありますが、1つだけ許可する必要があります。 – Damir

+0

'ListView.CHOICE_MODE_SINGLE'はあなたの問題を解決しません..? view.onCLick .. getView()内の –

答えて

1

ブール型ArrayListを使用して、チェックボックスの状態を維持することができます。そのArrayListのサイズはリストビューの項目と同じでなければなりません。 onListItemClick()でこのArrayListの状態を維持(選択/選択解除)することができます。最も重要なことに、アダプタのgetView()でこのArrayListを使用してチェックボックスの状態を設定する必要があります。 ArrayListのインデックスとして "position"を使用することができます。例えば

ArrayList<Boolean> lst_arrCheck = new ArrayList<Boolean>(); 
@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    for (int i = 0; i < listItems.size(); i++) // Do this before setting the adapter e.g. in OnCreate() 
    { 
     lst_arrCheck.add(false); 
    } 
} 
@Override 
protected void onListItemClick(ListView l, View v, int position, long id) 
{ 
    super.onListItemClick(l, v, position, id); 
    // Get the item that was clicked 

    lst_arrCheck.set(position, !(lst_arrCheck.get(position))); 
} 
... 
@Override 
public View getView(int position, View convertView, ViewGroup parent) 
{ 
    ... 
    CheckBox cb_test = (CheckBox)findViewById(R.id.cb_test); 
    cb_test.setChecked(lst_arrCheck.get(position)); 
... 
} 
+1

GOD BLESS YOU !!! – Damir

+0

Damirに感謝、私はあなたのために働いてうれしいです。 – Khawar