Android-ToDoList (custom ArrayAdapter)

Source: Internet
Author: User

ToDoList (custom ArrayAdapter)


Address: http://blog.csdn.net/caroline_wendy/article/details/21401907


For more information about the pre-project, see: Http://blog.csdn.net/caroline_wendy/article/details/21330733


Environment: Android Studio 0.5.1


ArrayAdapterUse a generic (Template)Adapter ViewBind to a specifiedArray of class objects;

Custom ArrayAdapter needs to be rewrittenGetView ()Method to allocate object attributes to the Layout View;


ToDoListAdd a time after each item. You need to createToDoItemObject.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 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;    }}

Details:

Two private variables, storageTask and date), The two constructor methods are rewritten.ToStringMethod;


2. Modify todolist_item layout (xml)

Location: res-> layout-> todolist_item.xml


 
     
      
  
 

Details:

1. UseRelativeLayout (related)Layout;

2.TextViewStorage date );

3.ToDoListItemView (custom, java)Storage task );

4.Layout_toLeftOfAttribute, indicating to the left of a view;

5. fadingEdge tag, remove the edge,Abandon, IsRequiresFadingEdgeLabel substitution;


3. Create ToDoItemAdapter and customize the adapter

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 Administrator on 14-3-17. */public class ToDoItemAdapter extends ArrayAdapter
 
   {    int resource;    public ToDoItemAdapter(Context context, int _resource, List
  
    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;    }}
  
 

Details:

1. constructor, parameters:View content, resource ID, array;

2. RewriteGetView ()To allocate object attributes to the Layout View;

3.GetItem, Get the project from position. The first update needs to fill the view, and then convert it;

4. Copy the corresponding attribute and find it from the view.Resource reference (findViewById), Assign values to resources;

5. Return to the view;



4. Modify ToDoListActivity implementation and replace String with ToDoItem object.

Location: java> package> ToDoListActivity

Package mzx. spike. todolist. app; import android. app. fragmentManager; import android. OS. bundle; import android. support. v7.app. actionBarActivity; import android. view. menu; import android. view. menuItem; import java. util. arrayList; public class ToDoListActivity extends ActionBarActivity implements NewItemFragment. onNewItemAddedListener {// use the ToDoItem object instead of String private ToDoItemAdapter aa; private ArrayList
 
  
ToDoItems; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_to_do_list); // get the fragment reference FragmentManager fm = getFragmentManager (); ToDoListFragment toDoListFragment = (ToDoListFragment) fm. findFragmentById (R. id. toDoListFragment); toDoItems = new ArrayList
  
   
(); Int resID = R. layout. todolist_item; // three parameters: aa = new ToDoItemAdapter (this, resID, toDoItems); toDoListFragment. setListAdapter (aa);} // override the interface method public void onNewItemAdded (String newItem) {ToDoItem newToDoItem = new ToDoItem (newItem); toDoItems. add (0, newToDoItem); aa. policydatasetchanged () ;}@ Override public boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. to_do_list, menu); return true ;}@ Override public boolean onOptionsItemSelected (MenuItem item) {// Handle action bar item clicks here. the action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest. xml. int id = item. getItemId (); if (id = R. id. action_settings) {return true;} return super. onOptionsItemSelected (item );}}
  
 

Details:

ReplaceString is ToDoItem, ReplaceArrayAdapter <> ToDoItemAdapter;


5. Execute the program


Code: http://download.csdn.net/detail/u012515223/7054943




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.