<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical"
	android:layout_width="fill_parent" 
	android:layout_height="fill_parent" >
	<TextView
		android:id="@+id/selection"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
	/>
	<ListView
		android:id="@android:id/list"
		android:layout_width="fill_parent" 
		android:layout_height="fill_parent"
		android:drawSelectorOnTop="false"
	/>
</LinearLayout>


package com.commonsware.android.resources;

import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParser;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class XMLResourceDemo extends ListActivity {
	TextView selection;
	ArrayList<string> items=new ArrayList<string>();
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        selection=(TextView)findViewById(R.id.selection);
        
        try{
        	XmlPullParser xpp =getResources().getXml(R.xml.words);
        	while(xpp.getEventType()!=XmlPullParser.END_DOCUMENT){
        		if(xpp.getEventType()==XmlPullParser.START_TAG){
        			if(xpp.getName().equals("word")){
        				items.add(xpp.getAttributeValue(0));
        			}
        		}
        		
        		xpp.next();
        	}
        }
        catch(Throwable t){
        	Toast.makeText(this, "실패 : "+t.toString(),4000).show();
        }
        setListAdapter(new ArrayAdapter<string>(this,android.R.layout.simple_list_item_1,items));
    }
    
    public void onListItemClick(ListView parent, View v , int position, long id){
    	selection.setText(items.get(position).toString());
    }
}



'Android > 기본' 카테고리의 다른 글

Access Web Image  (0) 2012.04.28
Read JSON  (0) 2012.04.28
StaticFile  (0) 2012.04.28
Read Write File (자동 저장할때 사용)  (0) 2012.04.28
Access Web  (0) 2012.04.28

+ Recent posts