Android进阶高手(二)适配器(Adapter)那是必须的
来源:互联网 发布:如何理解大数据时代 编辑:程序博客网 时间:2024/06/05 02:05
在Android应用开发过程中,适配器(Adapter)可是大有可为,尤其是在显示数据、读取数据库用列表显示更是使用频繁。但是今天我们要说的不是读取数据库中的信息,而是自己来添加一行数据让它显示在列表顶层。实现的功能有添加数据和删除数据两项。废话不多说,代码赶紧的!
首先先看下运行效果如下所示:
图一 图二
图三 图四
以上四张图演示了添加一行数据到列表中的功能,至于删除数据在这里就不演示了,留给童靴们去做。
开始介绍工程项目了,步骤如下:
1、新建项目命名为List
2、编写一个Bean封装其属性,命名为Item.java,代码如下:
package com.wyf.wpf;import java.text.SimpleDateFormat;import java.util.Date;public class Item {String task;Date created;public String getTask() {return task;}public Date getCreated() {return created;}public Item(String _task) {this(_task, new Date(java.lang.System.currentTimeMillis()));}public Item(String _task, Date _created) {task = _task;created = _created;}@Overridepublic String toString() {SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yy");String dateString = sdf.format(created);return "(" + dateString + ")" + task;}}
3、编写三个布局
(1)、main.xml:用于主布局
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ListView android:id="@+id/myListView" android:layout_width="fill_parent" android:layout_height="wrap_content"/></LinearLayout>
(2)、item.xml:用于显示列表中的每列数据
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal"> <TextView android:id="@+id/row" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:padding="5px" android:gravity="center" /> <TextView android:id="@+id/rowDate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:padding="5px" /></LinearLayout>
(3)、add.xml:用于弹出对话框提示添加数据的布局
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <EditText android:id="@+id/add" android:layout_width="fill_parent" android:layout_height="wrap_content" /></LinearLayout>
4、自定义自己的Adapter,命名为MyAdapter.java,代码如下:
package com.wyf.wpf;import java.text.SimpleDateFormat;import java.util.Date;import java.util.List;import android.content.Context;import android.util.Log;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.TextView;public class MyAdapter extends BaseAdapter{private LayoutInflater mInflater;private List<Item> items;public MyAdapter(Context context,List<Item> items){this.items=items;mInflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);}@Overridepublic int getCount() {// TODO Auto-generated method stubreturn items.size();}@Overridepublic Object getItem(int position) {// TODO Auto-generated method stubif(items==null){return null;}else{return items.get(position);}}@Overridepublic long getItemId(int position) {// TODO Auto-generated method stubreturn position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {// TODO Auto-generated method stubItem item=(Item) getItem(position);String nameString=item.getTask().toString();Date date=item.getCreated();SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yy"); String dateString = sdf.format(date);ViewHolder holder;if(convertView==null){holder=new ViewHolder();convertView=mInflater.inflate(R.layout.item,null);holder.nameText=(TextView)convertView.findViewById(R.id.row);holder.dateText=(TextView)convertView.findViewById(R.id.rowDate);convertView.setTag(holder);}else{ holder = (ViewHolder) convertView.getTag();}holder.nameText.setText(nameString);Log.i("TAG","nameString---->"+nameString);Log.i("TAG","holder.name---->"+holder.nameText);holder.dateText.setText(dateString);return convertView;}class ViewHolder{TextView nameText;TextView dateText;}}
5、编写Activity,这里就不解释了,直接上代码:
package com.wyf.wpf;import java.util.ArrayList;import android.app.Activity;import android.app.AlertDialog;import android.content.Context;import android.content.DialogInterface;import android.content.DialogInterface.OnClickListener;import android.os.Bundle;import android.view.KeyEvent;import android.view.LayoutInflater;import android.view.View;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.EditText;import android.widget.ListView;public class ListActivity extends Activity{private ArrayList<Item> Items;private ListView myListView;private MyAdapter adapter;private String[] str={"添加组","删除组"}; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); myListView = (ListView)findViewById(R.id.myListView); Items = new ArrayList<Item>(); for(int i=0;i<10;i++){ Item item=new Item("我是"+i); Items.add(item); } adapter=new MyAdapter(this,Items); myListView.setAdapter(adapter); myListView.setOnItemClickListener(new OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> arg0, View arg1, final int position,long arg3) {// TODO Auto-generated method stubnew AlertDialog.Builder(ListActivity.this).setItems(str, new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stubswitch(which){case 0:LayoutInflater mInflater=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);final View view=mInflater.inflate(R.layout.add,null);new AlertDialog.Builder(ListActivity.this).setView(view).setTitle("添加").setPositiveButton("添加",new OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stubEditText add=(EditText)view.findViewById(R.id.add);String str=add.getText().toString();updateAdapter(str);dialog.dismiss();}}).setNegativeButton("取消",new OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stubdialog.dismiss();}}).show();break;case 1:Items.remove(position);adapter.notifyDataSetChanged();dialog.dismiss();break;}}}).show();}}); } //更新adapter函数public void updateAdapter(String str){Item newItem; newItem = new Item(str); Items.add(0, newItem); adapter.notifyDataSetChanged();}}
上面的代码一般都不难,在这里就没必要详解了,只是需要注意一点,我没要刷新列表数据,就的使用adapter.notifyDataSetChanged();来更新。
今天就到此了,下班!哈哈!
- Android进阶高手(二)适配器(Adapter)那是必须的
- Android万能适配器(二)封装Adapter
- Android适配器进阶之二(分类适配器)
- android学习二:适配器Adapter的使用
- Android ListView 适配器(Adapter)优化
- Android ListView 适配器(Adapter)优化
- Android ListView 适配器(Adapter)优化
- Android Adapter适配器模板(笔记)
- Android入门——适配器(Adapter)
- Android listview入门(listview适配器Adapter)
- Kotlin:编写Android适配器(Adapter)
- Android 适配器Adapter的学习
- Android 适配器Adapter的学习
- Android适配器Adapter的使用
- Android适配器Adapter的写法
- 适配器模式(Adapter)
- 适配器模式(Adapter)
- 适配器(Adapter)模式
- 来信, 创业 和 移动互联网
- Benes Network在LDPC解码中的应用1
- oracle导入表失败,很诡异,找到原因
- C语言:用插入排序算法对一个数组进行排序
- 自定义函数
- Android进阶高手(二)适配器(Adapter)那是必须的
- 实战 SQL Server 2008 数据库误删除数据的恢复
- Android——动画的实现
- 鄞州新城区保障性住房 二期工程项目正式启动
- Android调试工具及方法
- Guide to installing 3rd party JARs
- 一段值得纪念的SQL语句
- 在MyEclipse中如何做到代码的注释规则是一致的
- 用C#创建COM组件全过程