Domanda

ho letto un tutorial, e utilizza SqlLite e "SimpleCursorAdapter" per riempire la lista con gli elementi. Questo è il codice del tutorial mi ha insegnato.

private void fillData() {
        // Get all of the notes from the database and create the item list
        Cursor c = mDbHelper.fetchAllNotes();
        startManagingCursor(c);

        String[] from = new String[] { NotesDbAdapter.KEY_TITLE };
        int[] to = new int[] { R.id.text1 };

        // Now create an array adapter and set it to display using our row
        SimpleCursorAdapter notes =
            new SimpleCursorAdapter(this, R.layout.notes_row, c, from, to);
        setListAdapter(notes);
    }

Comunque ... quello che se voglio riempirlo con i dati XML? E 'lo stesso metodo? Qualcuno può darmi un esempio (in codice)? grazie.

È stato utile?

Soluzione

L'esempio sta usando una CursorAdapter perché un oggetto Cursor viene restituito dal NotesDbAdapter (se non ricordo male) Metodo fetchAllNotes. Non so se c'è un modo per passare in XML grezzo per creare una lista ma è possibile utilizzare coppie nome / valore in un HashMap per creare un elenco utilizzando il SimplelistAdapter.

È possibile analizzare il tuo XML e JSON o e costruire una tabella hash con esso e l'uso che per compilare un lista. Il seguente esempio non usa XML, in realtà non è dinamica a tutti, ma questo dimostra come assemblare un elenco in fase di esecuzione. E 'preso dal metodo onCreate di un'attività che si estende ListActivity. I tutti i valori maiuscoli sono stringhe costanti statici definiti nella parte superiore della classe, e sono utilizzati come i tasti.

// -- container for all of our list items
List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();

// -- list item hash re-used
Map<String, String> group;

// -- create record
group = new HashMap<String, String>();

group.put( KEY_LABEL, getString( R.string.option_create ) );
group.put( KEY_HELP,  getString( R.string.option_create_help ) );
group.put( KEY_ACTION, ACTION_CREATE_RECORD );

groupData.add(group);

// -- geo locate
group = new HashMap<String, String>();

group.put( KEY_LABEL, getString(R.string.option_geo_locate ) );
group.put( KEY_HELP, getString(R.string.option_geo_locate_help ) )
group.put( KEY_ACTION, ACTION_GEO_LOCATE );

groupData.add( group );

// -- take photo
group = new HashMap<String, String>();

group.put( KEY_LABEL, getString( R.string.option_take_photo ) );
group.put( KEY_HELP, getString(R.string.option_take_photo_help ) );
group.put( KEY_ACTION, ACTION_TAKE_PHOTO );

groupData.add( group );

// -- create an adapter, takes care of binding hash objects in our list to actual row views
SimpleAdapter adapter = new SimpleAdapter( this, groupData, android.R.layout.simple_list_item_2, 
                                                   new String[] { KEY_LABEL, KEY_HELP },
                                                   new int[]{ android.R.id.text1, android.R.id.text2 } );
setListAdapter( adapter );
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top