我知道SQLite没有布尔值,我使用0和1的int来确定true或false。问题是我如何分配从DB获得的值并将其分配给开关按钮?我真的不明白SimpleCursorAdapter参数是如何组织布局的。 R.id.active应该是我的Switch按钮。
我的档案:
我的主要课程:
private SimpleCursorAdapter dataAdapter;
// The desired columns to be bound
String[] columns = new String[] {
AlarmDbAdapter.KEY_TIME,
AlarmDbAdapter.KEY_TITLE,
AlarmDbAdapter.KEY_ACTIVE,
};
// the XML defined views which the data will be bound to
int[] to = new int[] {
R.id.time,
R.id.alarmtitle,
R.id.active,
};
// create the adapter using the cursor pointing to the desired data
//as well as the layout information
dataAdapter = new SimpleCursorAdapter(
this, R.layout.alarmcomponent_layout,
cursor,
columns,
to,
0);
ListView listView = (ListView) findViewById(R.id.listView);
// Assign adapter to ListView
listView.setAdapter(dataAdapter);
在Adapter类中获取方法
Cursor mCursor = mDb.query(SQLITE_TABLE, new String[] {"rowid _id", KEY_TIME, KEY_TITLE, KEY_ACTIVE},
null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
activity_lazino.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".Lazino">
<ListView android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
alarmcomponent_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:id="@+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Time Here" />
<TextView
android:id="@+id/alarmtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/time"
android:text="Title Here"
/>
<TextView
android:id="@+id/activeText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Active?"
android:layout_below="@+id/time"
android:layout_alignRight="@+id/active"
android:layout_alignEnd="@+id/active" />
<Switch
android:id="@+id/active"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="40dp"
android:layout_marginEnd="40dp" />
</RelativeLayout>
答案 0 :(得分:0)
您无法直接使用SimpleCursorAdapter
来提供此功能:
一个简单的适配器,用于将游标中的列映射到XML文件中定义的TextViews或ImageViews。
但是,你可以做的是挂钩你自己的SimpleCursorAdapter.ViewBinder
以根据布尔值挂钩开关。
这是一个简单的例子:
private SimpleCursorAdapter.ViewBinder mViewBinder = new SimpleCursorAdapter.ViewBinder() {
@Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
boolean ret = false;
// Only handle the boolean Switch
// Rather than use a fixed number here, consider
// using a named constant which matches your column table.
if (columnIndex == 2) {
Switch btn = (Switch)view;
if (cursor.getInt(columnIndex) == 0) {
btn.setChecked(false);
} else {
btn.setChecked(true);
}
ret = true;
}
return ret;
}
}
答案 1 :(得分:0)
我找到解决方案的方式相当荒谬,我不知道为什么view.getParent();但这就像一个魅力。
为什么需要getParent()?
dataAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
View viewParent = (View) view.getParent();
Switch switchbtn = (Switch) viewParent.findViewById(R.id.active);
if(cursor.getString(3).equals("1") && !switchbtn.isChecked()) {
switchbtn.setChecked(true);
String text = (String)cursor.getString(columnIndex);
((TextView) view).setText(text);
//Just for more understanding on how the columnIndex hops
Log.wtf(null, "columnIndex: " + String.valueOf(columnIndex));
return true;
}else
return false;
}
});