2010-03-29 5 views
3

질문이 있습니다. 한동안 붙어 있습니다. 목록에 체크 박스를 추가 할 수있는 방법을 알고 있습니다. 예를 들어 항목 목록이 있으면 체크 할 수 있기를 원합니다. 내 XML 코드는이 다음이다 : 당신은 어떤 아이디어가 있다면listview에 체크 박스를 추가하는 방법은 무엇입니까?

<LinearLayout android:id="@+id/topLayout" 
    android:orientation="vertical" android:layout_width="fill_parent" 
    android:layout_centerHorizontal="true" android:layout_height="wrap_content"> 

</LinearLayout> 

<LinearLayout 
    android:id="@+id/middleLayout" 
    android:orientation="horizontal" 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent"> 

    <LinearLayout android:id="@+id/leftMiddleLayout" 
      android:orientation="vertical" android:layout_below="@+id/topLayout" 
      android:layout_above="@+id/bottomLayout" 
      android:layout_width="60px" android:layout_height="wrap_content" 
      > 

      <ListView android:id="@+id/checkboxList" android:layout_width="fill_parent" 
        android:layout_height="wrap_content" ></ListView> 

      <CheckBox android:id="@+id/checkbox" 
      android:layout_width="wrap_content" android:layout_height="wrap_content" 
      android:checked="false" 
      android:text="test"> 
      </CheckBox> 

    </LinearLayout> 

    <LinearLayout android:id="@+id/rightMiddleLayout" 
      android:orientation="vertical" android:layout_below="@+id/topLayout" 
      android:layout_above="@+id/bottomLayout" 
      android:layout_width="280px" android:layout_height="wrap_content" 
      > 

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

      <TextView android:id="@+id/text" android:layout_width="fill_parent" 
        android:layout_height="wrap_content"/>     
    </LinearLayout> 
</LinearLayout> 

<LinearLayout android:id="@+id/bottomLayout" 
    android:layout_alignParentBottom="true" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" android:layout_height="wrap_content" 
    android:paddingBottom="5pt" 
    > 

    <EditText android:id="@+id/insertNewItem" 
     android:layout_width="220px" android:layout_height="wrap_content" /> 

    <TextView android:layout_width="10px" android:layout_height="wrap_content" /> 

    <Button android:id="@+id/addItemButton" android:layout_width="wrap_content" 
     android:layout_height="fill_parent" android:text="Add Item"/> 
</LinearLayout> 

은 알려 주시기 바랍니다 그 내 학업을 위해 : ((

감사합니다

!
+0

해결할 수 없으며 해결할 수없는 문제인 것처럼 보입니다. . 나는 바보 같아 보인다. 지금 –

답변

3
public class myAdapter extends SimpleCursorAdapter { 
    public myAdapter(Context context, int layout, Cursor cursor, String[] from, int[] to) { 
     super(context, layout, cursor, from, to); 
} 

@Override 
public void bindView(View view, Context context, Cursor cursor) { 
    cb=(CheckBox)view.findViewById(R.id.cb); 
    cb.setText(dbgetStringValue); 
    cbText=(TextView)view.findViewById(R.id.cbText); 
      cbText.setText(dbgetStringValue); 
    cb.setChecked(false); 
    cb.setOnCheckedChangeListener(new OnCheckedChangeListener() { 
     public void onCheckedChanged(CompoundButton cb, boolean isChecked) {    
      if(cb.isChecked()) {      
       // action 
      } 
      else if(isChecked==false) { 
       // action 
      } 
     }   
    }); 
} 
} 

그게 뭡니까?

0

setAdapter()를 사용하여 listView에서 ListAdapter (예 : 사용자 정의 BaseAdapter)를 사용하는 경우 어댑터의 getView()에서 LayoutInflater를 사용할 수 있습니다. 목록 항목에 대해 원하는 레이아웃을 제공하십시오. 이것은 CheckBox를 포함 할 수 있습니다.

+0

감사하다. –

+0

OMG, 나는이 문제를 해결할 수 없다 ... 나는 그것에 붙어있다. (( –

0

또는 ListAdapter를 하위 클래스로 확장하고 bindView를 재정의 할 수 있습니다. ChecBoxes에 .setText를 설정한다면!

+0

오, 고마워! 그 예를 어디서 찾을 수 있는지 알고 있니? 문제는 내가 각 항목 옆에있는 확인란을 넣을 방법을 찾을 수 없다는 것입니다 : JSON을 사용하여 데이터베이스에서 항목을 읽고, 나는 항목이 있습니다. ( –

3

Make your Android UI Fast and Efficient에서 android dev 사이트에서이 비디오를 시청하는 것이 좋습니다. 문제를 해결하고 가능한 빨리 어댑터를 구현할 수있는 적절한 방법을 보여줄 코드를 제공합니다.

+0

대단히 감사합니다 !! –

4
final ListView lView = (ListView) findViewById(R.id.ListView01); 
lView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, items)); 
lView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); 

여기 항목은 배열입니다.

관련 문제