gpt4 book ai didi

Android:根据数据库字段数据更改 ImageView src

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:55:40 24 4
gpt4 key购买 nike

我是 Android 开发的新手(2 天前开始)并且已经学习了很多教程。我正在从 Android SDK 中的记事本练习 (Link to tutorial) 构建一个测试应用程序,作为笔记列表的一部分,我想根据我称为“notetype”的数据库字段的内容显示不同的图像.我希望这张图片出现在每个记事本条目之前的 ListView 中。

我的 .java 文件中的代码是:

private void fillData() {
Cursor notesCursor = mDbHelper.fetchAllNotes();

notesCursor = mDbHelper.fetchAllNotes();
startManagingCursor(notesCursor);

String[] from = new String[]{NotesDbAdapter.KEY_NOTENAME, NotesDbAdapter.KEY_NOTETYPE};

int[] to = new int[]{R.id.note_name, R.id.note_type};

// Now create a simple cursor adapter and set it to display
SimpleCursorAdapter notes =
new SimpleCursorAdapter(this, R.layout.notes_row, notesCursor, from, to);
setListAdapter(notes);
}

我的布局 xml 文件 (notes_row.xml) 如下所示:

<ImageView android:id="@+id/note_type"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/default_note"/>
<TextView android:id="@+id/note_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>

我真的不知道如何根据已选择的笔记类型获取正确的可绘制对象。目前我可以从 Spinner 中选择类型,所以存储在数据库中的是一个整数。我已经创建了一些与这些整数相对应的图像,但它似乎没有找到任何东西。

如有任何帮助,我们将不胜感激。如果您需要更多信息,请告诉我。

最佳答案

您可能想尝试使用 ViewBinder。 http://d.android.com/reference/android/widget/SimpleCursorAdapter.ViewBinder.html

这个例子应该有帮助:

private class MyViewBinder implements SimpleCursorAdapter.ViewBinder {

public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
int viewId = view.getId();
switch(viewId) {
case R.id.note_name:

TextView noteName = (TextView) view;
noteName.setText(Cursor.getString(columnIndex));

break;
case R.id.note_type:

ImageView noteTypeIcon = (ImageView) view;

int noteType = cursor.getInteger(columnIndex);
switch(noteType) {
case 1:
noteTypeIcon.setImageResource(R.drawable.yourimage);
break;
case 2:
noteTypeIcon.setImageResource(R.drawable.yourimage);
break;
etc…
}

break;
}
}

然后用

将它添加到你的适配器中
note.setViewBinder(new MyViewBinder());

关于Android:根据数据库字段数据更改 ImageView src,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2192082/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com