Lập trình Android: Đọc XML pdf

5 258 1
Lập trình Android: Đọc XML pdf

Đang tải... (xem toàn văn)

Thông tin tài liệu

Trung tâm Tin học – ĐH KHTN Lấy dữ liệu từ File Ngoài DataBase bằng SQLite như 1 bài viết mà mình đã đăng. Ta còn có thể lấy dữ kiệu trực tiếp bằng File XML. Cách lấy dữ liệu kiểu này khá đơn giản nhưng lại gây 1 số khó khăn cho quá trình quản lý. Nên có thể tùy ứng dụng mà bạn sử dụng. Các bạn tạo 1 Project như sau: Project name: AccessFile Build Target: Android 2.3.3 Application name: AccessFile Package name: com.dac.AccessFile Create Activity: AccessFile Trong phần giao diện đơn giản như sau: <?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:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" android:id="@+id/Selection" /> <ListView android:id="@android:id/list" android:layout_width="fill_parent" android:layout_height="fill_parent" android:drawSelectorOnTop="false" /> </LinearLayout> Để tạo cơ sở dữ liệu XML ta tạo folder mới mang trên raw trong folder res. Sau đó ta tạo 1 file Words.xml trong folder raw và có code như sau: <?xml version="1.0" encoding="utf-8"?> <words> <word value="lorem" /> <word value="ipsum" /> <word value="dolor" /> <word value="sit" /> <word value="amet" /> Lập trình Android – http://laptrinhdidong.vn Page 1 Trung tâm Tin học – ĐH KHTN <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> Vậy ta đã tạo được 1 tập hợp các từ làm dữ liệu. Vậy nên ta code trong file AccessFile.java như sau: package com.dac.AccessFile; 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; Lập trình Android – http://laptrinhdidong.vn Page 2 Trung tâm Tin học – ĐH KHTN import android.view.View; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; public class AccessFile extends ListActivity { TextView selection; ArrayList<String> items=new ArrayList<String>(); /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); selection=(TextView)findViewById(R.id.Selection); try { InputStream in=getResources().openRawResource(R.raw.words); DocumentBuilder builder=DocumentBuilderFactory .newInstance() .newDocumentBuilder(); Document doc=builder.parse(in, null); NodeList words=doc.getElementsByTagName("word"); for (int i=0;i<words.getLength();i++) { Lập trình Android – http://laptrinhdidong.vn Page 3 Trung tâm Tin học – ĐH KHTN items.add(((Element)words.item(i)).getAttribute("value")); } in.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()); } } Kết quả: Lập trình Android – http://laptrinhdidong.vn Page 4 Trung tâm Tin học – ĐH KHTN Vậy ta đã lấy được dữ liệu từ file XML và khi bấm vào 1 tên bất kỳ thì TextView sẽ hiện ra tên đó. Mọi ý kiến đóng góp các bạn vui lòng gữi bài viết vào forum www.laptrinhdidong.vn . Rất mong nhận được sự phản hồi của các bạn Lập trình Android – http://laptrinhdidong.vn Page 5 . android:id="@+id/Selection" /> <ListView android:id="@android:id/list" android:layout_width="fill_parent" android:layout_height="fill_parent" android:drawSelectorOnTop="false" /> </LinearLayout> Để. android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" . sau: < ?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"

Ngày đăng: 08/08/2014, 03:21

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan