1>实体类---即ListView中各个Item中填充的内容
package com.demo.cxc.compoundview.com.demo.cxc.entity; import java.text.SimpleDateFormat; import java.util.Date; /** * Created by CXC on 15/4/14. */ public class ToDoItem { private String task; private Date createdDate; //构造函数 public ToDoItem(String task){ this(task,new Date(java.lang.System.currentTimeMillis())); } public ToDoItem(String task,Date createdDate){ this.task=task; this.createdDate=createdDate; } //getters && setters public String getTask() { return task; } public void setTask(String task) { this.task = task; } public Date getCreatedDate() { return createdDate; } public void setCreatedDate(Date createdDate) { this.createdDate = createdDate; } @Override public String toString() { SimpleDateFormat sdf=new SimpleDateFormat("dd/MM/yy"); String dateString =sdf.format(this.createdDate); return "("+dateString+") "+this.task; } }
package com.demo.cxc.compoundview.com.demo.cxc.adapter; 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 com.demo.cxc.compoundview.R; import com.demo.cxc.compoundview.com.demo.cxc.entity.ToDoItem; import org.w3c.dom.Text; import java.text.SimpleDateFormat; import java.util.List; /** * Created by CXC on 15/4/14. */ public class ToDoItemAdapter extends ArrayAdapter<ToDoItem> { private int resource_id; public ToDoItemAdapter(Context context, int resource_id, List<ToDoItem> items) { super(context, resource_id, items); this.resource_id = resource_id; } @Override public View getView(int position, View convertView, ViewGroup parent) { LinearLayout todoView; ToDoItem toDoItem=getItem(position); if(convertView==null){//不是一次更新,则填充一个新视图 todoView=new LinearLayout(getContext()); LayoutInflater layoutInflater=(LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); layoutInflater.inflate(resource_id,todoView,true); }else {//否则更新该视图 todoView=(LinearLayout)convertView; } //得到对象的各个属性值 TextView task_tv=(TextView)todoView.findViewById(R.id.task_tv); TextView date_tv=(TextView)todoView.findViewById(R.id.date_tv); //填充该Item 中各个View的内容 task_tv.setText(toDoItem.getTask()); date_tv.setText(new SimpleDateFormat("dd/MM/yy").format(toDoItem.getCreatedDate())); return todoView; } }
3.1>布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <EditText android:id="@+id/new_item_et" android:layout_width="0dp" android:layout_height="wrap_content" android:hint="Please input what to do ..." android:layout_weight="1"/> <Button android:id="@+id/commit_bt" android:text="Ok" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> <ListView android:id="@+id/todo_list" android:layout_width="match_parent" android:layout_height="wrap_content"></ListView> </LinearLayout>
package com.demo.cxc.compoundview; import android.content.DialogInterface; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.ListView; import com.demo.cxc.compoundview.com.demo.cxc.adapter.ToDoItemAdapter; import com.demo.cxc.compoundview.com.demo.cxc.entity.ToDoItem; import java.util.ArrayList; import java.util.List; public class MainActivity extends ActionBarActivity implements View.OnClickListener { private EditText new_item_et; private Button commit_bt; private ListView todo_list; private List<ToDoItem> toDoItemList; private ToDoItemAdapter toDoItemAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initViews(); } /*初始化控件,变量*/ private void initViews() { new_item_et=(EditText)findViewById(R.id.new_item_et); commit_bt=(Button)findViewById(R.id.commit_bt); todo_list=(ListView)findViewById(R.id.todo_list); commit_bt.setOnClickListener(this); toDoItemList=new ArrayList<ToDoItem>(); toDoItemAdapter=new ToDoItemAdapter(this,R.layout.todo_item,toDoItemList); //ListView注册Adapter todo_list.setAdapter(toDoItemAdapter); } @Override protected void onResume() { super.onResume(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, 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(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } @Override public void onClick(View v) { ToDoItem newItem=new ToDoItem(new_item_et.getText().toString()); toDoItemList.add(0,newItem); toDoItemAdapter.notifyDataSetChanged(); } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/task_tv" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1"/> <TextView android:id="@+id/date_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>