给listview添加短按长按事件

来源:互联网 发布:c罗欧洲杯夺冠知乎 编辑:程序博客网 时间:2024/06/06 00:43

其他的文件和上篇使用的文件代码相同,参照上篇的代码即可,

这里展示出更改的代码

PersonDao.java(更改的为add()方法的返回值,以前为boolean,现在改为long)

package com.demo.introductiontothedb.dao;import java.util.ArrayList;import java.util.List;import com.demo.introductiontothedb.PersonDBOpenHelper;import com.demo.introductiontothedb.domain.PersonInfo;import android.content.ContentValues;import android.content.Context;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import android.text.TextUtils;public class PersonDao {//增删改查//javaweb 1.加载jdbc驱动.连接 2.准备sql 3.查询private PersonDBOpenHelper helper;//任何人使用 dao 都要传递一个上下文public PersonDao(Context context) {helper = new PersonDBOpenHelper(context);}/** * 添加一条记录 */public long add(String name,String phone,String address){SQLiteDatabase db = helper.getWritableDatabase();//db.execSQL("insert into personInfo (name,phone,address) values (?,?,?)",//new Object[]{name,phone,address});ContentValues values = new ContentValues();//map集合values.put("name", name);values.put("phone", phone);values.put("address", address);long result = db.insert("personInfo", null, values);db.close();return result;}/** * 根据名字查找一条记录 */public int find(String name){int id = -1;SQLiteDatabase db = helper.getReadableDatabase();//Cursor cursor = db.rawQuery("select id from personInfo where name=?", //new String[]{name});Cursor cursor = db.query("personInfo", new String[]{"id"}, "name=?", new String[]{name}, null, null, null);if(cursor.moveToFirst()){id = cursor.getInt(0);}cursor.close();db.close();return id;}/** * 删除一条记录 */public boolean delete(int id){SQLiteDatabase db = helper.getWritableDatabase();//db.execSQL("delete from personInfo where id=?", new Object[]{id});int result = db.delete("personInfo", "id=?", new String[]{id + ""});db.close();if(result > 0){return true;}else{return false;}}/** * 更改一条记录 */public boolean update(String name,String phone,int id){SQLiteDatabase db = helper.getWritableDatabase();//db.execSQL("update personInfo set name=?,phone=? where id=?",//new Object[]{name,phone,id});//String table, ContentValues values, String whereClause, String[] whereArgs)ContentValues values = new ContentValues();values.put("name", name);values.put("phone", phone);int result = db.update("personInfo", values, "id=?", new String[]{id + ""});db.close();if(result > 0){return true;}else{return false;}}//查找全部public List<PersonInfo> findAll(){int money = 0;List<PersonInfo> personInfos = new ArrayList<PersonInfo>();SQLiteDatabase db = helper.getWritableDatabase();Cursor cursor = db.query("personInfo", new String[]{"id","name","phone","address","money"}, null, null, null, null, null);while(cursor.moveToNext()){int id = cursor.getInt(cursor.getColumnIndex("id"));String name = cursor.getString(cursor.getColumnIndex("name"));String phone = cursor.getString(cursor.getColumnIndex("phone"));String address = cursor.getString(cursor.getColumnIndex("address"));String moneyStr = cursor.getString(cursor.getColumnIndex("money"));if(TextUtils.isEmpty(moneyStr)){money = 0;}else{money = Integer.parseInt(moneyStr);}PersonInfo personInfo = new PersonInfo(id, name, phone, address, money);personInfos.add(personInfo);}cursor.close();db.close();return personInfos;}}

MainActivity.java(这里添加了短按常按事件,要注意数据库的更新,适配器的更新以及适配器界面的刷新)

package com.demo.introductiontothedb;import java.util.List;import com.demo.introductiontothedb.dao.PersonDao;import com.demo.introductiontothedb.domain.PersonInfo;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.AdapterView.OnItemLongClickListener;import android.widget.BaseAdapter;import android.widget.Button;import android.widget.ListView;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends Activity {public static final String TAG = "MainActivity";private PersonDao personDao;private List<PersonInfo> personInfos;private ListView lv;private MyAdapter adapter;private Button btn_add;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//1.得到listviewlv = (ListView) findViewById(R.id.lv);btn_add = (Button) findViewById(R.id.btn_add);personDao = new PersonDao(this);personInfos = personDao.findAll();//2.设置listview的数据适配器adapter = new MyAdapter();lv.setAdapter(adapter);//添加点击事件lv.setOnItemClickListener(new OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> parent, View view,int position, long id) {//取出position位置的personInfo对象PersonInfo personInfo = personInfos.get(position);int personId = personInfo.getId();Toast.makeText(MainActivity.this, "排名:"+personId, 0).show();}});//添加长按事件,长按删除一条记录lv.setOnItemLongClickListener(new OnItemLongClickListener() {@Overridepublic boolean onItemLongClick(AdapterView<?> parent, View view,int position, long id) {PersonInfo personInfo = personInfos.get(position);int personId = personInfo.getId();//数据库内删除personDao.delete(personId);//从数据适配器集合里面清除personInfos.remove(position);//注意,这里要用position...//适配器界面更新adapter.notifyDataSetChanged();//false 当前的事件不会被终止掉//true 当前的事件执行到这里就终止了return true;}});//按钮的添加事件btn_add.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {//数据库添加记录int id = (int)personDao.add("西野翔", "454845", "某岛国");//界面的集合personInfos.add(new PersonInfo(id,"周防雪子","911844","也是岛国",3000));//适配器更新adapter.notifyDataSetChanged();}});}//控制器  数据适配器private class MyAdapter extends BaseAdapter{//数据适配器里面有多少条数据@Overridepublic int getCount() {return personInfos.size();}@Overridepublic Object getItem(int position) {return null;}@Overridepublic long getItemId(int position) {return 0;}//返回当前位置对应的view对象  显示的内容/** * 与传统写listview比,该方法优势在于,用户 * 在界面上翻看多少listview的内容, * 该方法就会执行多少次,大大的提高了效率节省了资源。 * 不会像传统的不管看还是不看,一次性把personInfos * 里面的全部内容加载到界面上 */@Overridepublic View getView(int position, View convertView, ViewGroup parent) {//position 代表的是位置//Log.i(TAG, "getView" + position);//TextView tv = new TextView(MainActivity.this);//PersonInfo personInfo = personInfos.get(position);//tv.setText(personInfo.toString());//tv.setTextSize(16);//tv.setTextColor(Color.BLUE);//return tv;// 将xml文件创建成一个独立的view对象.View view = View.inflate(MainActivity.this, R.layout.list_view, null);// 从刚才转化出来的view对象里面寻找name//注意,一定从刚刚转换的view里面找,MainActivity里面可没有这textviewTextView name = (TextView)view.findViewById(R.id.name);// 从刚才转化出来的view对象里面寻找phoneTextView phone = (TextView)view.findViewById(R.id.phone);// 从刚才转化出来的view对象里面寻找addressTextView address = (TextView)view.findViewById(R.id.address);//从刚才转化出来的view对象里面寻找moneyTextView money = (TextView)view.findViewById(R.id.money);//得到该positiion位置的一个personinfo对象PersonInfo personInfo = personInfos.get(position);//绑定每个条目控件的信息name.setText(personInfo.getName());phone.setText(personInfo.getPhone());address.setText(personInfo.getAddress());money.setText("$:"+personInfo.getMoney());return view;}}}


0 0