Android-ToDoList(定制ArrayAdapter)

ToDoList(定制ArrayAdapter)


专业从事成都网站设计、做网站,高端网站制作设计,小程序开发,网站推广的成都做网站的公司。优秀技术团队竭力真诚服务,采用html5+CSS3前端渲染技术,成都响应式网站建设,让网站在手机、平板、PC、微信下都能呈现。建站过程建立专项小组,与您实时在线互动,随时提供解决方案,畅聊想法和感受。

本文地址: http://blog.csdn.net/caroline_wendy/article/details/21401907


前置项目参见: http://blog.csdn.net/caroline_wendy/article/details/21330733


环境: 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


                

详解:

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

详解:

1. 构造函数, 参数: 视图内容, 资源ID, 数组;

2. 重写getView(), 向布局视图分配对象属性;

3. getItem, 从position获取项目,  第一次更新需要填充视图, 之后转换即可;

4. 给相应的属性复制, 从视图中找到资源引用(findViewById), 给资源赋值;

5. 返回视图;



4. 修改ToDoListActivity实现, 替换String为ToDoItem对象

位置: 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 {      //使用ToDoItem对象代替String     private ToDoItemAdapter aa;     private ArrayList toDoItems;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_to_do_list);          //获得fragment的引用         FragmentManager fm = getFragmentManager();         ToDoListFragment toDoListFragment = (ToDoListFragment)fm.findFragmentById(R.id.ToDoListFragment);          toDoItems = new ArrayList();          int resID = R.layout.todolist_item;         //三个参数         aa = new ToDoItemAdapter(this, resID, toDoItems);          toDoListFragment.setListAdapter(aa);     }      //重写了接口的方法     public void onNewItemAdded(String newItem) {         ToDoItem newToDoItem = new ToDoItem(newItem);         toDoItems.add(0, newToDoItem);         aa.notifyDataSetChanged();     }      @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);     }  } 

详解:

替换String为ToDoItem, 替换ArrayAdapter<>为ToDoItemAdapter;


5. 执行程序


代码: http://download.csdn.net/detail/u012515223/7054943



Android - ToDoList(定制ArrayAdapter)


网站栏目:Android-ToDoList(定制ArrayAdapter)
分享网址:http://scyanting.com/article/ghhshg.html