專業(yè)從事成都網(wǎng)站設(shè)計、做網(wǎng)站,高端網(wǎng)站制作設(shè)計,小程序開發(fā),網(wǎng)站推廣的成都做網(wǎng)站的公司。優(yōu)秀技術(shù)團隊竭力真誠服務(wù),采用html5+CSS3前端渲染技術(shù),成都響應(yīng)式網(wǎng)站建設(shè),讓網(wǎng)站在手機、平板、PC、微信下都能呈現(xiàn)。建站過程建立專項小組,與您實時在線互動,隨時提供解決方案,暢聊想法和感受。
本文地址: http://blog.csdn.net/caroline_wendy/article/details/21401907
前置項目參見: http://blog.csdn.net/caroline_wendy/article/details/21330733
環(huán)境: Android Studio 0.5.1
ArrayAdapter使用泛型(模板)把Adapter視圖綁定到一個指定類的對象的數(shù)組;
定制ArrayAdapter需要重寫getView()方法, 向布局視圖分配對象屬性;
ToDoList在每一項后面添加時間, 需要創(chuàng)建ToDoItem對象, 使用定制的ArrayAdapter;
步驟:
位置: 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; } }
兩個私有變量, 存儲任務(wù)(task)和日期(date), 兩種構(gòu)造方法, 重寫了toString方法;
位置: 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(相關(guān))布局;
2. TextView存儲日期(date);
3. ToDoListItemView(定制, java)存儲任務(wù)(task);
4. layout_toLeftOf屬性, 表示位于某個視圖的左邊;
5. fadingEdge標(biāo)簽, 褪去邊緣, 遺棄, 被requiresFadingEdge標(biāo)簽代替;
位置: 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; } }
1. 構(gòu)造函數(shù), 參數(shù): 視圖內(nèi)容, 資源ID, 數(shù)組;
2. 重寫getView(), 向布局視圖分配對象屬性;
3. getItem, 從position獲取項目, 第一次更新需要填充視圖, 之后轉(zhuǎn)換即可;
4. 給相應(yīng)的屬性復(fù)制, 從視圖中找到資源引用(findViewById), 給資源賦值;
5. 返回視圖;
位置: 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<ToDoItem> 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<ToDoItem>(); int resID = R.layout.todolist_item; //三個參數(shù) 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;
代碼: http://download.csdn.net/detail/u012515223/7054943
分享文章:Android-ToDoList(定制ArrayAdapter)
URL網(wǎng)址:http://jinyejixie.com/article46/ghhshg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、虛擬主機、動態(tài)網(wǎng)站、網(wǎng)站營銷、網(wǎng)站建設(shè)、微信小程序
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)