Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android系統教程 >> Android開發教程 >> Android中的ToDoList(定制ArrayAdapter)

Android中的ToDoList(定制ArrayAdapter)

編輯:Android開發教程

環境: Android Studio 0.5.1

ArrayAdapter使用泛型(模板)把Adapter視圖綁定到一個指定類的對象的數組;

定制ArrayAdapter需要重寫getView()方法, 向布局視圖分配對象屬性;

ToDoList在每一項後面添加時間, 需要創建ToDoItem對象, 使用定制的ArrayAdapter;

步驟:

1. 創建ToDoItem對象

位置: java->package->ToDoItem

package mzx.spike.todolist.app;  
      
import java.text.SimpleDateFormat;  
import java.util.Date;  
      
/** 
 * Created by Administrator on 14-3-17. 
 */
public class ToDoItem {  
    String task;  
    Date created;  
      
    public String getTask() {  
        return task;  
    }  
      
    public Date getCreated() {  
        return created;  
    }  
      
    public ToDoItem(String _task) {  
        this(_task, new Date(java.lang.System.currentTimeMillis()));  
    }  
      
    public ToDoItem(String _task, Date _created) {  
        task = _task;  
        created = _created;  
    }  
      
    @Override
    public String toString() {  
        SimpleDateFormat sdf = new SimpleDateFormat("dd/mm/yy");  
        String dateString = sdf.format(created);  
        return "(" + dateString + ") " + task;  
    }  
}

詳解:

兩個私有變量, 存儲任務(task)和日期(date), 兩種構造方法, 重寫了toString方法;

2. 修改todolist_item布局(xml)

位置: res->layout->todolist_item.xml

<?xml version="1.0" encoding="utf-8"?>  
      
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">  
      
    <TextView
        android:id="@+id/rowDate"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:background="@color/notepad_paper"
        android:padding="10dp"
        android:scrollbars="vertical"
        android:requiresFadingEdge="vertical"
        android:textColor="#F000"
        android:layout_alignParentRight="true"
     />  
      
    <mzx.spike.todolist.app.ToDoListItemView
        android:id="@+id/row"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="10dp"
        android:scrollbars="vertical"
        android:requiresFadingEdge="vertical"
        android:textColor="@color/notepad_text"
        android:layout_toLeftOf="@+id/rowDate"
     />  
</RelativeLayout>

詳解:

1. 使用RelativeLayout(相關)布局;

2. TextView存儲日期(date);

3. ToDoListItemView(定制, java)存儲任務(task);

4. layout_toLeftOf屬性, 表示位於某個視圖的左邊;

5. fadingEdge標簽, 褪去邊緣, 遺棄, 被requiresFadingEdge標簽代替;

3. 創建ToDoItemAdapter, 定制適配器

位置: java->package->ToDoItemAdapter

package mzx.spike.todolist.app;  
      
import android.content.Context;  
import android.view.LayoutInflater;  
import android.view.View;  
import android.view.ViewGroup;  
import android.widget.ArrayAdapter;  
import android.widget.LinearLayout;  
import android.widget.TextView;  
      
import java.text.SimpleDateFormat;  
import java.util.Date;  
import java.util.List;  
      
/** 
 * Created by Administrator on 14-3-17. 
 */
public class ToDoItemAdapter extends ArrayAdapter<ToDoItem> {  
    int resource;  
      
    public ToDoItemAdapter(Context context, int _resource, List<ToDoItem> items) {  
        super(context, _resource, items);  
        this.resource = _resource;  
    }  
      
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {  
        LinearLayout toDoView;  
      
        ToDoItem item = getItem(position);  
      
        String taskString = item.getTask();  
        Date createdDate = item.getCreated();  
        SimpleDateFormat sdf = new SimpleDateFormat("dd/mm/yy");  
        String dateString = sdf.format(createdDate);  
      
        if (convertView == null) {  
            toDoView = new LinearLayout(getContext());  
            String inflater = Context.LAYOUT_INFLATER_SERVICE;  
            LayoutInflater li;  
            li = (LayoutInflater)getContext().getSystemService(inflater);  
            li.inflate(resource, toDoView, true);  
        } else {  
            toDoView = (LinearLayout)convertView;  
        }  
      
        TextView dateView = (TextView)toDoView.findViewById(R.id.rowDate);  
        TextView taskView = (TextView)toDoView.findViewById(R.id.row);  
      
        dateView.setText(dateString);  
        taskView.setText(taskString);  
      
        return toDoView;  
    }  
}

詳解:

查看本欄目更多精彩內容:http://www.bianceng.cn/OS/extra/

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved