Mostrar el valor de la matriz de cadenas XML en listView en lugar de la posición almacenada relevante

Firstly sorry if the question title is unclear, I couldn't think how to word it concisely.

I have an app with basic database functionality to add a name, date of birth and job type of a person to a database. The job type is chosen from a spinner populated from a string array in the XML resource file;

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

In the database the job name isn't stored, its position in the string array is stored (ie 'Job 1' is saved as the integer 0 etc)

When I then display the the contents of the database in a listView I would like to display the actual job rather than its position, but I don't know how to do this.

The code I am using for the listView is from the Android Developers example;

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. */
public void onCreate(Bundle savedInstanceState) {
    mDbHelper = new PersonDbAdapter(this);;

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


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

    // 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[]{,};

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

The cursor fetchAllPeople just gets all data on every person in the database.

My question is; how do I display the actual job rather than just the position that is stored in the database. Any help would be appreciated.

EDIT: Neatened up code

preguntado el 31 de julio de 12 a las 12:07

2 Respuestas

Supongo que el TextView(?!?) in the row layout where you want to place the job title is the view with the id el to array, also I assume PersonDbAdapter.KEY_TYPE represents the column from the cursor with the job int values). In this case you would use a SimpleCursorAdapter.ViewBinder to transform the job int values stored in the database into actual job titles, like this:

people.setViewBinder(new SimpleCursorAdapter.ViewBinder() {

            public boolean setViewValue(View view, Cursor cursor,
                    int columnIndex) {
                if (view.getId() == {
                    int jobInt = cursor.getInt(columnIndex);
                    ((TextView) view).setText(mTypeArray[jobInt]); // if the view with the id is a TextView!!!
                    return true;
                return false;

Respondido 31 Jul 12, 14:07

Your assumptions were correct. Sorry for the confusion; I tried altering my code to a clearer example and forgot to change some bits. This works perfectly! Thankyou for the help - Bruce0

I am not getting you properly if you want to get the data from the database you should use:

Cursor previous_repotData = getdataBaseRecord();
Stirng stdate = getString(previous_repotData.getColumnIndexOrThrow("startDate"))); 

Respondido 31 Jul 12, 19:07

Sorry if my question wasn't clear. I have got it working using Luksprog's answer now. Thankyou for your input though - Bruce0

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.