0

質問のタイトルが不明確である場合は、最初に申し訳ありません。簡潔に表現する方法が思いつきませんでした。

個人の名前、生年月日、職種をデータベースに追加するための基本的なデータベース機能を備えたアプリがあります。ジョブの種類は、XML リソース ファイルの文字列配列から取り込まれたスピナーから選択されます。

<string-array name="job_type_array">
    <item>Job 1</item>
    <item>Job 2</item>
    <item>Job 3</item>
    ...
</string-array>

データベースにはジョブ名は保存されず、文字列配列内の位置が保存されます (つまり、「ジョブ 1」は整数 0 として保存されます)。

次に、データベースの内容を listView に表示すると、その位置ではなく実際のジョブを表示したいのですが、これを行う方法がわかりません。

listView に使用しているコードは、Android Developers の例からのものです。

public class PersonList extends ListActivity {
private static final int ACTIVITY_CREATE=0;
private static final int ACTIVITY_EDIT=1;

private static final int INSERT_ID = Menu.FIRST;
private static final int DELETE_ID = Menu.FIRST + 1;

public String[] mTypeArray;
public List typeList;

private PersonDbAdapter mDbHelper;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.horses_list);
    mDbHelper = new PersonDbAdapter(this);
    mDbHelper.open();

    mTypeArray = getResources().getStringArray(R.array.job_type_array);
    typeList = Arrays.asList(mTypeArray);

    fillData();
    registerForContextMenu(getListView());
}

private void fillData() {
    // Get all of the rows from the database and create the item list
    Cursor personCursor = mDbHelper.fetchAllPeople();
    startManagingCursor(personCursor);

    // Create an array to specify the fields we want to display in the list (only    Name and job)
    String[] from = new String[]{PersonDbAdapter.KEY_NAME, PersonDbAdapter.KEY_TYPE};

    // and an array of the fields we want to bind those fields to
    int[] to = new int[]{R.id.rowPersonName, R.id.rowPersonJobType};

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

カーソル fetchAllPeople は、データベース内のすべての人のすべてのデータを取得します。

私の質問は; データベースに保存されているポジションだけでなく、実際のジョブを表示するにはどうすればよいですか。どんな助けでも大歓迎です。

編集:コードをまとめた

4

2 に答える 2

1

役職を配置する行レイアウトのTextView(?!?) は、id を持つビューであると仮定しますR.id.rowHorseType(to配列ではPersonDbAdapter.KEY_TYPE、カーソルからの列をジョブの int 値で表すと仮定します)。この場合、次のように、データベースに保存されている役職の値を実際の役職SimpleCursorAdapter.ViewBinderに変換するために a を使用します。int

people.setViewBinder(new SimpleCursorAdapter.ViewBinder() {

            @Override
            public boolean setViewValue(View view, Cursor cursor,
                    int columnIndex) {
                if (view.getId() == R.id.rowHorseType) {
                    int jobInt = cursor.getInt(columnIndex);
                    ((TextView) view).setText(mTypeArray[jobInt]); // if the view with the id R.id.rowHorseType is a TextView!!!
                    return true;
                }
                return false;
            }
        });
于 2012-07-31T13:15:55.047 に答える
0

使用する必要のあるデータベースからデータを取得したい場合、適切に取得できません。

Cursor previous_repotData = getdataBaseRecord();
Stirng stdate = getString(previous_repotData.getColumnIndexOrThrow("startDate"))); 
于 2012-07-31T12:48:05.143 に答える