+44(0) 1234 567 890 info@domainname.com

Tuesday, January 22, 2013

Hướng dẫn lập trình cơ bản với Android phần 11

7:38 AM

Share it Please

B5: Chỉnh sửa file strings.xml trong res\value:

Mã:

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

<resources>

<string name="app_name">Example 3</string>

<string name="work_hint">Enter the workhere</string>

<string name="hour_edit">Hour</string>

<string name="minute_edit">Minute</string>

<string name="button_content">Add work</string>

</resources>

B6: Time to coding. Đi tới src\at.exam tạo một class mới là CustomViewGroup với nội dung sau:

Mã:

package at.exam;

import android.content.Context;

import android.view.LayoutInflater;

import android.widget.CheckBox;

import android.widget.LinearLayout;

import android.widget.TextView;

public class CustomViewGroup extends LinearLayout {

public CheckBox cb;

public TextView workContent;

public TextView timeContent;

public CustomViewGroup(Context context) {

          super(context);

                    //Sử dụng LayoutInflater để gán giao diện trong list.xml cho class này

                    LayoutInflater li = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); li.inflate(R.layout.list, this, true);

                   //Lấy về các View qua Id

         cb = (CheckBox) findViewById(R.id.check_work);

         workContent = (TextView)findViewById(R.id.work_content);

         timeContent = (TextView) findViewById(R.id.time_content);

}

}

Đoạn code trên giúp ta định nghĩa giao diện của custom ViewGroup mới dựa trên file list.xml. Mọi người cũng có thể tạo giao diện bằng code, ko cần sử dụng XML nhưng sẽ phức tạp hơn và mình cũng ko giới thiệu ở đây.

B7: Tạo 1 class Work cũng trong at.exam để thể hiện công việc:

package at.exam;

public class Work {

private String workContent;

private String timeContent;

private boolean isChecked;

public Work(String workContent, String timeContent){

this.workContent = workContent;

this.timeContent = timeContent;

isChecked = false;

}

public String getContent() {

return workContent;

}

public String getTime() {

return timeContent;

}

public void setChecked(boolean isChecked) {

this.isChecked = isChecked;

}

public boolean isChecked() {

return isChecked;

}

}

Code rất đơn giản nên mình sẽ không chú thích nữa.

B8: Chúng ta đã tạo xong custem ViewGroup, bây giờ chính là lúc sử dụng. Tạo 1 class mới tên là ListWorkApdapter trong at.exam:

Mã:

package at.exam;

import java.util.ArrayList;

import android.content.Context;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.ArrayAdapter;

import android.widget.CheckBox;

import android.widget.CompoundButton;

import android.widget.TextView;

import android.widget.CompoundButton.OnCheckedChangeListener;

public class ListWorkAdapter extends ArrayAdapter<Work>{

ArrayList<Work> array;

int resource;

Context context;

public ListWorkAdapter(Context context, int textViewResourceId,ArrayList<Work> objects) {

          super(context, textViewResourceId, objects); this.context = context;

          resource = textViewResourceId; array = objects;

}

//Phương thức xác định View mà Adapter hiển thị, ở đây chính là CustomViewGroup

//Bắt buộc phải Override khi kế thừa từ ArrayAdapter

@Override

public View getView(int position, View convertView, ViewGroup parent) {

View workView = convertView;

if (workView == null) { workView = new

CustomViewGroup(getContext());

}

//Lấy về đối tượng Work hiện tại

final Work work = array.get(position);

if (work != null) {

TextView workContent = ((CustomViewGroup) workView).workContent;

TextView timeContent = ((CustomViewGroup) workView).timeContent;

CheckBox checkWork = ((CustomViewGroup) workView).cb;

//Set sự kiện khi đánh dấu vào checkbox trên list

checkWork.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override

public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {

         work.setChecked(isChecked);

}});

//Lấy về nội dung cho TextView và CheckBox dựa vào đối tượng Work hiện tại

workContent.setText(work.getContent()); timeContent.setText(work.getTime()); checkWork.setChecked(work.isChecked());

}

return workView;

}

}

ListWorkAdapter sẽ được sử dụng thay thế cho ArrayAdapter được bind với ListView. Thông thường ArrayAdapter chỉ cho hiển thị String bằng TextView, nhưng với việc kế thừa và override phương thức getView, ta có thể định nghĩa lại hiển thị cho các thành phần của ListView.

0 comments:

Post a Comment

Tags

Thủ thuật pc Góc Làm Đẹp Làm Đẹp Da Thủ Thuật Blogspot - Wordpress - Joomla Thủ Thuật Network Giải trí Thủ Thuật Hay thu-thuat-blogspot ASP.NET Danh bạ Lập trình CSDL Thủ Thuật Blog Lập trình Thủ thuật Excel Phần Mềm Góc Phụ Nữ SEO TT Windows Lý Thuyết CCNA Dịch Vụ Hot CCNA Sức Khỏe - Đời Sống Office Thủ Thuật Mobile Thủ Thuật Win 8 Download Xây dựng liên kết Android Thủ thuật Word Website thu-thuat-internet Forum PHP Tư Vấn Làm Đẹp Mẹo Vặt tien-ich-widget Đọc báo ASP.NET MVC Ca nhạc Hỏi Đáp Thơ Vui Thủ Thuật Game vui-nhon Download Phần Mềm - Crack VBA Excel thu-thuat-facebook Cuộc sống Download Game Photoshop Thủ thuật Blogspot VBA trong Excel Widget Blogspot Địa Điểm Hot C# CSS Giới Tính Tài Liệu Đại học Chuyện lạ Clip Hài - Video Hot Góc Kỹ Năng Góc Sinh Viên Liên Minh Huyền Thoại Sao Sức Khỏe Giới Tính Thủ Thuật Tin Học template-blogger Chuyện Của Sao Design Pattern Kiến thức cơ bản PM hệ thống Thủ thuật Facebook Tin nổi bật Đề thi LAB CCNA Love story Học và hỏi Môn toán Spring.NET Tủ Sách Video lam-quen-voi-blogger Ảnh Vui Bảng Giá Con trai Cẩm nang du học Dependency Injection Hình ảnh Kinh nghiệm học Kỹ Năng Xin Việc Luật Pháp ORM Tin nóng Tiện ích Truyện cười Việt Nam hinh-anh-hai-huoc tho-vui-con-coc thu-thuat-phan-mem Blogspot Distributed Technologies Hợp Tác Kinh Doanh Khám phá MVP NHibernate Photoshop Action Thủ Thuật Win 7 Trẻ Tư Vấn Tiêu Dùng VietYeah TV Xã Hội thu-thuat-SEO Blogger Templates Game flash Khối B Nghịch Nhân vật Phong Cách Slider Thư giãn Thủ thuật Youtube WCF facebook hinh-anh-dep thu-thuat-may-tinh Đời Sống 2010 Con gái Download Game Mobile Drivers External Post Học Nấu Ăn Học vui Làm Đẹp Tóc MVC N-Tier PM hệ điều hành Sao Quốc tế Thủ thuật internet Thông Tin Tin Tức Hot Tivi Truyện Hay Tuyển Dụng Xả Stress Youtube anh-bia-facebook san-pham-google ung-dung-website Đáp án Đề thi thử AOP Abstract Factory Cool stuffs Design Principles Khối A Thông tin mạng Tình yêu lập trình viên phan-mem-van-phong thu-thuat-windows-7 thu-thuat-windows-8 Điện Tử Ảnh Nghệ Thuật 2009 2011 2012 Adapter Pattern Công Nghệ Factory Method Game Hình sự Khối D Kiếm Tiền Qua Mạng Kỹ Năng Nơi Công Sở LinQ Menu Miễn phí Photoshop PSD Sexy Singleton Sành điệu Thủ Thuật Di Động Tin nhắn SMS phan-mem-do-hoa phan-mem-tien-ich Đọc xong vọc liền .NET Remoting 10-thing series 2013 Ajax Bridge Pattern Bóng đá Castle Windsor Chơi Game Online Cover Facebook Có Thể Bạn Chưa Biết Download Phim Dịch vụ Fedora Garbage Collection Google Plus Góc Tâm Sự HTML Hibernate Hài hước IoC JSF Javascript Jquery Khối C Kỹ Năng Bán Hàng Kỹ Năng Thuyết Trình Lazy Load Linux Lock Log4Net MWC Memory Management Mock Multi-Thread MySQL Môn sử Mặc Đẹp Mẹ Và Bé Mỹ Tâm NUnit Ngọc Trinh Nhịp sống trẻ PM văn phòng POCO Passive View Photoshop tách ảnh Phạm Băng Băng Phầm Mềm Crack PostSharp Primefaces Shop Bít Tuốt Strategy Pattern Supervising Controller Sự kiện Thể thao Thủ Thuật Office Tin đặc biệt Toán Tình bạn Tình dục Tốt nghiệp Unit Testing VBA Word Virtual Server Văn WMC 2012 Windows Service acc fshare acc mien phi anh-nghe-thuat cntt di dong di động dropbox dung luong dropbox fshare hinh nen hinh nen dep hình nền job mac os mang xa hoi may ao mạng xã hội social tang dung luong dropbox thu-thuat-mobile thu-thuat-windows-xp tháng 2 top truyen-cuoi tăng dung lượng dropbox vmware Ảnh đẹp