Android ToDoList (custom arrayadapter)

Source: Internet
Author: User
Tags add time resource tostring

Environment: Android Studio 0.5.1

Arrayadapter uses a generic (template) to bind the adapter view to an array of objects of the specified class;

Custom Arrayadapter need to rewrite the GetView () method to assign object attributes to the layout view;

ToDoList add time after each item, you need to create Todoitem object, use custom Arrayadapter;

Steps:

1. Create a Todoitem object

Location: Java->package->todoitem

Package Mzx.spike.todolist.app;  
      
Import Java.text.SimpleDateFormat;  
Import java.util.Date;  
      
/** 
 * Created by the 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;  
    }  
}

Detailed

Two private variables, storing tasks and dates (date), two methods of construction, overriding the ToString method;

2. Modify Todolist_item layout (XML)

Location: Res->layout->todolist_item.xml

<?xml version= "1.0" encoding= "Utf-8"?> <relativelayout xmlns:android= "Http://schemas.android.com/ap" K/res/android "android:layout_width=" match_parent "android:layout_height=" match_parent "> <TextV Iew android:id= "@+id/rowdate" android:layout_width= wrap_content "android:layout_height=" Match_par Ent "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:l Ayout_width= "Match_parent" android:layout_height= "match_parent" android:padding= "10DP" ANDROID:SCR ollbars= "vertical" android:requiresfadingedge= "vertical" android:textcolor= "@color/notepad_text" a Ndroid:layout_toleftof= "@+id/rowdate "/> </RelativeLayout> 

Detailed

1. Use Relativelayout (related) layout;

2. TextView Storage date (dates);

3. Todolistitemview (custom, Java) storage tasks (Task);

4. Layout_toleftof property, representing the left side of a view;

5. Fadingedge label, Fade Edge, abandon, be replaced by Requiresfadingedge label;

3. Create Todoitemadapter, custom adapters

Location: 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 the Administrator on 14-3-17.  
      
    * * public class Todoitemadapter extends arrayadapter<todoitem> {int resource; Public Todoitemadapter (context, int _resource, list<todoitem> items) {Super (context, _resource, I  
        TEMS);  
    This.resource = _resource; @Override public View getview (int position, View Convertview, ViewGroup parent) {Linearlayou  
      
        T 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; }  
}

Detailed

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/OS/extra/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.