Android的:改变依赖于数据库字段数据的ImageView的SRC [英] Android: Changing an ImageView src depending on database field data

查看:102
本文介绍了Android的:改变依赖于数据库字段数据的ImageView的SRC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是很新的Andr​​oid开发(入门2天前),并已通过一些教程了。我建立一个测试程序,从记事本运动(链接到补习)在Android SDK并作为说明列表的一部分,我想显示不同的图像取决于数据库字段我已经被称为notetype的内容。我想这一形象出现在每个记事本条目之前的列表视图。

I'm quite new to Android development (started 2 days ago) and have been through a number of tutorials already. I'm building a test app from the NotePad exercise (Link to tutorial) in the Android SDK and as part of the list of notes I want to display a different image depending on the contents of a database field i've called "notetype". I'd like this image to appear in the List view before each notepad entry.

在code。在我的.java文件是:

The code in my .java file is:

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)看起来是这样的:

And my layout xml file (notes_row.xml) looks like this:

<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"/>

我真的不知道我怎么会去获取正确绘制取决于注意到,已选择的类型。目前,我已经从一个微调选择类型的能力,所以什么获取存储在数据库中的一个整数。我创建对应于这些整数部分的图像,但它似乎不捡东西。

I just really have no clue how I'd go about fetching the right drawable depending on the type of note that has been selected. At the moment I have the ability to select the type from a Spinner, so what gets stored in the database is an integer. I've created some images that correspond to these integers but it doesn't seem to pick things up.

任何帮助将是AP preciated。如果您需要更多的信息,那么请让我知道。

Any help would be appreciated. If you need more info then please let me know.

推荐答案

您可能想尝试使用ViewBinder。 <一href="http://d.android.com/reference/android/widget/SimpleCursorAdapter.ViewBinder.html">http://d.android.com/reference/android/widget/SimpleCursorAdapter.ViewBinder.html

You might want to try using a 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;
        }
    }

}

然后把它添加到您的适配器

Then add it to your adapter with

note.setViewBinder(new MyViewBinder());

这篇关于Android的:改变依赖于数据库字段数据的ImageView的SRC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆