如何使用简单的适配器在列表视图中设置复选框的状态?

时间:2014-12-20 10:24:17

标签: android checkbox android-listview

我正在使用listview,每个列表项都有一个复选框和4个textviews。我想用存储的数据填充列表。我知道如何使用SimpleAdapter用text填充textviews。但是我想知道如何在创建列表时预定义复选框的状态(选中或不选中)。

使用此代码我将文本放入textviews中。请告诉我是否可以以相同的方式设置复选框的状态。

        final String LEAVE_ID = "leave_id";
		final String MEMBER_NAME = "member_name";
		final String LEAVE_CODE = "leave_code";
		final String LEAVE_DATE = "leave_date";
		final String CHECK_BOX="check_box";

		//setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

		list = new ArrayList<HashMap<String, String>>();
		HashMap<String, String> map = new HashMap<String, String>();
		leaveList = (ListView) findViewById(R.id.list_view_for_leaves);
		Button clearButton = (Button) findViewById(R.id.clear_button);
		Button deleteButton = (Button) findViewById(R.id.delete_button);
		clearButton.setOnClickListener(this);
		deleteButton.setOnClickListener(this);
		
		map = new HashMap<String, String>();
		map.put(LEAVE_ID, "1234");
		map.put(MEMBER_NAME, "Nithin");
		map.put(LEAVE_CODE, "Vacation Leave");
		map.put(LEAVE_DATE, "12-12-14");
		list.add(map);

		map = new HashMap<String, String>();
		map.put(LEAVE_ID, "1232");
		map.put(MEMBER_NAME, "Mithil");
		map.put(LEAVE_CODE, "Sick Leave");
		map.put(LEAVE_DATE, "20-12-14");
		list.add(map);		
		
		adapter = new SimpleAdapter(this, list,
				R.layout.list_item_leave, new String[] { LEAVE_ID, MEMBER_NAME,
						LEAVE_CODE, LEAVE_DATE }, new int[] { R.id.leave_id,
						R.id.member_name, R.id.leave_type, R.id.leave_date });
		leaveList.setAdapter(adapter);
		adapter.notifyDataSetChanged();
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tableRow1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <CheckBox
        android:id="@+id/leave_check"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#dcdcdc" 
        android:checked="false"/>

    <TextView
        android:id="@+id/leave_id"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#dcdcdc"
        android:gravity="center"
        
        android:text="@string/project_no"
        android:textColor="#000000"
        android:textSize="15sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/member_name"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#d3d3d3"
        android:gravity="center"
        
        android:text="@string/project_name"
        android:textColor="#000000"
        android:textSize="15sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/leave_type"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#dcdcdc"
        android:gravity="center"        
        android:text="@string/type"
        android:textColor="#000000"
        android:textSize="15sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/leave_date"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#d3d3d3"
        android:gravity="center"
        
        android:text="@string/client"
        android:textColor="#000000"
        android:textSize="15sp"
        android:textStyle="bold" />

</TableRow>

0 个答案:

没有答案