Android 从ViewBinder中的cursor.getInt()获取错误值
我的android应用程序中有一个ListFragment,可以从sqlite数据库获取数据 布局如下所示:Android 从ViewBinder中的cursor.getInt()获取错误值,android,Android,我的android应用程序中有一个ListFragment,可以从sqlite数据库获取数据 布局如下所示: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/ll_main" android:layout_width="fill_parent" android:layout_height="wrap_content" and
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll_main"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp">
<ImageView android:id="@+id/iv_fav"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:paddingRight="10dp"/>
<TextView android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sample Data"
android:textSize="17sp"
android:layout_toRightOf="@id/iv_fav"/>
<TextView
android:id="@+id/tv_verz"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Verzeichnis:"
android:textSize="12sp"
android:layout_below="@id/tv_title"
android:layout_toRightOf="@id/iv_fav"/>
<TextView android:id="@+id/tv_folder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sample Data"
android:textSize="12sp"
android:paddingLeft="5dp"
android:layout_toRightOf="@id/tv_verz"
android:layout_below="@id/tv_title"/>
</RelativeLayout>
现在的问题是cursor.getInt(columnIndexId)没有从数据库中为我提供正确的ID。如果单击数据库中id为1的第一项,则返回12。第二项相同,应该是2,但我得到12。第三项,应该是3,我得到12。我是否在我的列表中走得更远,比如第10项,我得到的是16分,而不是几个项目之后的18分,依此类推
为什么呢?我做错了什么?我真的不明白…这可能是setViewValue方法中的view.setOnClickListener()的问题吗?derolli,是的,我的问题是这个。请帮帮我。
private void displayListView(View view) {
Cursor cursor = dbHelper.fetchAllBooks();
String[] columns = new String[] {
BookDbAdapter.KEY_TITLE,
BookDbAdapter.KEY_FOLDER,
BookDbAdapter.KEY_FAV
};
int[] to = new int[] {
R.id.tv_title,
R.id.tv_folder,
R.id.iv_fav
};
dataAdapter = new SimpleCursorAdapter(getActivity(), R.layout.single_row_item, cursor, columns, to, 0);
dataAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
@Override
public boolean setViewValue(View view, final Cursor cursor, final int columnIndex) {
if (columnIndex == cursor.getColumnIndex(BookDbAdapter.KEY_FAV)) {
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int columnIndexId = cursor.getColumnIndex(BookDbAdapter.KEY_ROWID);
int columnIndexFav = cursor.getColumnIndex(BookDbAdapter.KEY_FAV);
Toast.makeText(getActivity(), "star clicked! " + cursor.getInt(columnIndexId), Toast.LENGTH_SHORT).show();
int starred = 0;
if (cursor.getInt(columnIndexFav) == 0) {
starred = 1;
ImageView img = (ImageView) v;
img.setImageDrawable(getResources().getDrawable(R.drawable.star_on));
} else {
ImageView img = (ImageView) v;
img.setImageDrawable(getResources().getDrawable(R.drawable.star_off));
}
dbHelper.setFav(cursor.getInt(columnIndexId), starred);
}
});
ImageView img = (ImageView) view;
if (cursor.getInt(4) == 1) {
img.setImageDrawable(getResources().getDrawable(R.drawable.star_on));
return true;
} else {
img.setImageDrawable(getResources().getDrawable(R.drawable.star_off));
return true;
}
}
return false;
}
});
setListAdapter(dataAdapter);
}