假设从数据库获取到的数据为list
自定义Adapter继承自BaseAdapter,重写getView方法,实例化列表项并且绑定数据就可以了
/** 新建一个类继承BaseAdapter,实现视图与数据的绑定*/public class MyAdapter extends BaseAdapter {
private LayoutInflater mInflater//得到一个LayoutInfalter对象用来导入布局
private List list
/**构造函数*/
public MyAdapter(Context context,List list) {
this.mInflater = LayoutInflater.from(context)
this.list = list
}
@Override
publicint getCount() {
return list.size()//返回数组的长度
}
@Override
public Object getItem(int position) {
return list.get(position)
}
@Override
publiclong getItemId(int position) {
return 0
}
@Override
public View getView(finalint position, View convertView, ViewGroup parent) {
ViewHolder holder
if (convertView == null) {
convertView = mInflater.inflate(R.layout.item,null)
holder = new ViewHolder()
/**得到各个控件的对象*/
holder.title = (TextView) convertView.findViewById(R.id.ItemTitle)
holder.text = (TextView) convertView.findViewById(R.id.ItemText)
holder.bt = (Button) convertView.findViewById(R.id.ItemButton)
convertView.setTag(holder)//绑定ViewHolder对象
}
else{
holder = (ViewHolder)convertView.getTag()//取出ViewHolder对象
}
holder.title.setText(list.get(position).getId().toString())
holder.text.setText(list.get(position).getName().toString())
return convertView
}
/**存放控件*/
class ViewHolder{
public TextView title
public TextView text
}
}
从列表视图中删除数据之后,你必须调用refreshDrawableState()刷新Android的列表视图下面是例子:
final DatabaseHelper db = new DatabaseHelper (ActivityName.this)
db.open()
db.deleteContact(arg3)
mListView.refreshDrawableState()
db.close()
和deleteContact在方法DatabaseHelper类将是长得像
public boolean deleteContact(long rowId) {
return db.delete(TABLE_NAME, BaseColumns._ID + "=" + rowId, null) >0
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)