문제

튜토리얼을 읽고 SQLLITE와 "SimplecursOradapter"를 사용하여 목록을 항목으로 채 웁니다. 이것이 튜토리얼이 가르쳐 준 코드입니다.

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);
    }

그러나 ... XML 데이터로 채우고 싶다면 어떻게해야합니까? 같은 방법입니까? 누군가 나에게 예제 (코드)를 줄 수 있습니까? 감사해요.

도움이 되었습니까?

해결책

예제는 a를 사용하는 것입니다 CursorAdapter a Cursor 객체는 NotesDbAdapter (내가 정확하게 기억한다면) fetchAllNotes 방법. 목록을 만들기 위해 RAW XML을 전달할 방법이 있는지 모르겠지만 이름/값 쌍을 HashMap SimplelistAdapter를 사용하여 목록을 작성합니다.

XML 및 OR JSON을 구문 분석하고 해시 테이블을 만들어 목록을 채울 수 있습니다. 다음 예제는 XML을 사용하지 않으며 실제로는 동적이지 않지만 런타임에 목록을 조립하는 방법을 보여줍니다. 그것은에서 가져 왔습니다 onCreate 확장되는 활동의 방법 ListActivity. 모든 대문자 값은 클래스 상단에 정의 된 정적 상수 문자열이며 키로 사용됩니다.

// -- 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 );
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top