1. Trang chủ
  2. » Công Nghệ Thông Tin

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

5 259 1

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

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 5
Dung lượng 68,5 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

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

Trang 1

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:

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

/>

Để 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:

< words >

< word value="lorem" />

< word value="ipsum" />

< word value="dolor" />

< word value="sit" />

Trang 2

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

Trang 3

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++) {

Trang 4

}

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ả:

Trang 5

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

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

w