码迷,mamicode.com
首页 > 其他好文 > 详细

ListView使用CursorAdapter增加和删除item

时间:2015-06-12 16:59:18      阅读:90      评论:0      收藏:0      [点我收藏+]

标签:

 1     @Override
 2     protected void onCreate(Bundle savedInstanceState) {
 3         // TODO 自动生成的方法存根
 4         super.onCreate(savedInstanceState);
 5         requestWindowFeature(Window.FEATURE_NO_TITLE);
 6         setContentView(R.layout.resume);
 7         
 8         myDataBaseHelper = new MyDataBaseHelper(this, "Resume1.db", null, 1);
 9         db = myDataBaseHelper.getReadableDatabase();
10         
11         ViewUtils.inject(this);
12         
13         cursor = db.query("Resume", null, null, null, null, null, null);
14         adapter = new DataCursorAdapter(this, cursor);
15         ListView listView=(ListView) findViewById(R.id.list_view);
16         listView.setAdapter(adapter);        
17     }
18     
19     @Override
20     protected void onResume() {
21         // TODO 自动生成的方法存根    
22         super.onResume();
23         cursor = db.query("Resume", null, null, null, null, null, null);
24         adapter.changeCursor(cursor);
25     }

DataCursorAdapter部分代码:

 1     @Override
 2     public View newView(Context context, Cursor cursor, ViewGroup parent) {
 3         // TODO 自动生成的方法存根
 4         ViewHolder viewHolder=new ViewHolder();
 5         LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 6         View view=inflater.inflate(R.layout.resume_item, parent,false);
 7         viewHolder.nameListET=(TextView) view.findViewById(R.id.nameListET);
 8         viewHolder.relationListSpinner=(TextView) view.findViewById(R.id.relationListSpinner);
 9         viewHolder.schoolListET=(EditText) view.findViewById(R.id.schoolListET);
10         viewHolder.workListET=(EditText) view.findViewById(R.id.workListET);
11         viewHolder.deleteResume=(Button) view.findViewById(R.id.deleteResume);
12         view.setTag(viewHolder);
13         return view;
14     }
15 
16     @Override
17     public void bindView(View view, final Context context, final Cursor cursor) {
18         // TODO 自动生成的方法存根
19         
20         viewHolder = (ViewHolder) view.getTag();
21         name = cursor.getString(cursor.getColumnIndex("name"));
22         relation = cursor.getString(cursor.getColumnIndex("relation"));
23         school = cursor.getString(cursor.getColumnIndex("school"));
24         work = cursor.getString(cursor.getColumnIndex("work"));
25         final String itemId = cursor.getString(cursor.getColumnIndex("_id"));
26         
27         viewHolder.nameListET.setText(name);
28         viewHolder.relationListSpinner.setText(relation);
29         viewHolder.schoolListET.setText(school);
30         viewHolder.workListET.setText(work);
31         viewHolder.deleteResume.setTag(itemId);
32         
33         viewHolder.deleteResume.setOnClickListener(new OnClickListener() {
34             
35             @Override
36             public void onClick(View v) {
37                 // TODO 自动生成的方法存根
38                 myDataBaseHelper = new MyDataBaseHelper(context, "Resume1.db", null, 1);
39                 db = myDataBaseHelper.getReadableDatabase();
40                 db.delete("Resume", "_id= "+v.getTag(), null);
41                 cursor.requery();
42                 notifyDataSetChanged();
43 
44             }
45         });
46         
47     }
48     
49     @Override
50     public void changeCursor(Cursor cursor) {
51         // TODO 自动生成的方法存根
52         super.changeCursor(cursor);
53     }
54 
55     class ViewHolder{
56         
57         TextView nameListET;
58         TextView relationListSpinner;
59         EditText schoolListET;
60         EditText workListET;
61         Button deleteResume;
62         
63     }

 

ListView使用CursorAdapter增加和删除item

标签:

原文地址:http://www.cnblogs.com/liyiran/p/4571726.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!