Notice
Recent Posts
Recent Comments
Link
관리 메뉴

설.현.아빠

[FILE] 고정된 파일의 내용 읽어와 리스트로 보여주기 본문

안드로이드/File

[FILE] 고정된 파일의 내용 읽어와 리스트로 보여주기

설.현.아빠 2011. 2. 11. 10:20




 

<?xml version="1.0" encoding="utf-8"?>

 

<!-- main.xml -->


<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/mainTextView"
  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>

 

 

<!-- /res/raw/example.xml -->

 

<words>
 <word value="lorem" />
 <word value="ipsum" />
 <word value="dolor" />
 <word value="sit" />
 <word value="amet" />
 <word value="consectetuer" />
 <word value="adipiscing" />
 <word value="elit" />
 <word value="morbi" />
 <word value="vel" />
 <word value="ligula" />
 <word value="vitae" />
 <word value="arcu" />
 <word value="aliquet" />
 <word value="mollis" />
 <word value="etiam" />
 <word value="vel" />
 <word value="erat" />
 <word value="placerat" />
 <word value="ante" />
 <word value="porttitor" />
 <word value="sodales" />
 <word value="pellentesque" />
 <word value="augue" />
 <word value="purus" />
</words>

 

 

 

// StaticFileRead.java

 

package lee.android.StaticFileRead;

import java.io.InputStream;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

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 StaticFileRead 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.mainTextView);

  try {
   InputStream inputstream = getResources().openRawResource(
     R.raw.example);
   // 단어 파일(example.xml)에 대한 InputStream 확보

   DocumentBuilder builder = DocumentBuilderFactory.newInstance()
     .newDocumentBuilder();
   Document doc = builder.parse(inputstream, null);
   // Android 에 내장된 XML Parser 사용하여 DOM 구조의 Document 객체로 변환.

   NodeList words = doc.getElementsByTagName("word");
   // "word" Elements 를 모두 추려냄.

   for (int i = 0; i < words.getLength(); i++) {
    items.add(((Element) words.item(i)).getAttribute("value"));
   }
   inputstream.close();
  } catch (Throwable t) {
   Toast.makeText(this, "Exception: " + t.toString(), 2000).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());
 }
}

 


Comments