如何从ListView中删除的项目? [英] How to delete item from listView?
问题描述
我取从SQLite的列表视图中的数据。我需要删除此项目时,在办理入住手续
用户点击
框和preSS删除按钮或仅点击复选框,
我不知道如何做到这一点?下面是我的示例code,以供参考。
MyTable.java
公共类MyTable的扩展ListActivity {
MySQLiteHelper M =新MySQLiteHelper(本);
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
意向意图= getIntent();
的setResult(RESULT_OK,意图);
ArrayAdapter<型号:GT;适配器=新InteractiveArrayAdapter(这一点,
getModel());
setListAdapter(适配器);
}
私人列表<型号:GT; getModel()
{
名单<型号:GT;名单=新的ArrayList<型号>();
最后MySQLiteHelper M =新MySQLiteHelper(getBaseContext());
最后的名单,其中,LocWiseProfileBeans> LocWiseProfile = m.getAllLocWiseProfile();
对于(最终LocWiseProfileBeans CN:LocWiseProfile){
list.add((得到(cn.getLocname())));
//list.add(得到(cn.getSelectedprofile()));
//最初选择的项目之一
list.get(0).setSelected(真正的);
}
返回列表;
}
私模的get(String s)将{
返回新模式(S);
}
InteractiveArrayAdapter.java
公共类InteractiveArrayAdapter扩展ArrayAdapter<型号:GT; {
私人最终名单,其中,型号>清单;
私人最终活动范围内;
公共InteractiveArrayAdapter(活动背景下,列表与LT;型号>名单)
{
超(背景下,R.layout.locprofile,清单);
this.context =背景;
this.list =清单;
}
静态类ViewHolder {
受保护的TextView文本;
保护复选框复选框;
}
@覆盖
公共查看getView(INT位置,查看convertView,ViewGroup中父){
查看查看= NULL;
如果(convertView == NULL){
LayoutInflater充气= context.getLayoutInflater();
鉴于= inflator.inflate(R.layout.locprofile,NULL);
最后ViewHolder viewHolder =新ViewHolder();
viewHolder.text =(TextView中)view.findViewById(R.id.label);
viewHolder.checkbox =(复选框)view.findViewById(R.id.check);
viewHolder.checkbox
.setOnCheckedChangeListener(新CompoundButton.OnCheckedChangeListener(){
@覆盖
公共无效onCheckedChanged(CompoundButton buttonView,
布尔器isChecked){
模型元素=(模型)viewHolder.checkbox
.getTag();
element.setSelected(buttonView.isChecked());
}
});
view.setTag(viewHolder);
viewHolder.checkbox.setTag(list.get(位置));
} 其他 {
鉴于= convertView;
((ViewHolder)view.getTag())checkbox.setTag(list.get(位置))。
}
ViewHolder支架=(ViewHolder)view.getTag();
holder.text.setText(list.get(位置).getName());
holder.checkbox.setChecked(list.get(位置).isSelected());
返回查看;
}
}
MySQliteHelper.java
公共静态最后弦乐TABLE_NAME =loc_wise_profile;
公共静态最后弦乐TABLE_NAME2 =监督员;
公共静态最后弦乐COLUMN_ID =_id;
公共静态最后弦乐COLUMN1 =loc_name;
公共静态最后弦乐COLUMN2 =lattitude;
公共静态最后弦乐COLUMN3 =经度;
公共静态最后弦乐COLUMN4 =selectedprofile;
公共静态最后弦乐COLUMN5 =contactno;
公共静态最后弦乐COLUMN6 =消息;
//公共静态最后弦乐COLUMN7 =已启用;
公共静态最后弦乐COLUMN8 =NotificeationMessage;
SQLiteDatabase分贝;
私有静态最后弦乐DATABASE_NAME =语言环境;
私有静态最终诠释DATABASE_VERSION = 2;
//数据库创建SQL语句
私有静态最后弦乐DATABASE_CREATE =CREATE TABLE IF NOT EXISTS
+ TABLE_NAME +(+ COLUMN_ID
+整数主键自动增量,
+ COLUMN1 +文字不为空,
+ COLUMN2 +双非空
+ COLUMN3 +双非空
+ COLUMN4 +文字不为空,
+ COLUMN5 +文字不为空,
+ COLUMN6 +文字不为空,
// + COLUMN7 +文字不为空,
+ COLUMN8 +文字不为空
+);;
公共MySQLiteHelper(上下文的背景下)
{
超(背景下,DATABASE_NAME,空,DATABASE_VERSION);
// TODO自动生成构造函数存根
}
@覆盖
公共无效的onCreate(SQLiteDatabase数据库){
// TODO自动生成方法存根
database.execSQL(DATABASE_CREATE);
//database.execSQL(DATABASE_CREATE2);
}
//添加新的位置明智的个人资料
公共无效insertLocWiseProfile(LocWiseProfileBeans LOC){
DB = this.getWritableDatabase();
ContentValues值=新ContentValues();
values.put(COLUMN1,loc.getLocname()); // 联系人姓名
values.put(COLUMN2,loc.getLattitude()); //联系电话
values.put(COLUMN3,loc.getLongitude());
values.put(COLUMN4,loc.getSelectedprofile());
values.put(COLUMN5,loc.getContactno());
values.put(COLUMN6,loc.getMessage());
//values.put(COLUMN7,loc.getEnabled());
values.put(COLUMN8,loc.getNotificationMessage());
//插入行
db.insert(TABLE_NAME,空,价值观);
db.close(); //闭幕数据库连接
}
//获取所有联系人
公开名单< LocWiseProfileBeans> getAllLocWiseProfile(){
名单< LocWiseProfileBeans> LocWiseProfileList =新的ArrayList< LocWiseProfileBeans>();
//选择所有查询
字符串selectQuery =SELECT * FROM+ TABLE_NAME;
SQLiteDatabase DB = this.getWritableDatabase();
光标光标= db.rawQuery(selectQuery,NULL);
//遍历所有行和添加到列表
如果(cursor.moveToFirst()){
做 {
LocWiseProfileBeans LOC =新LocWiseProfileBeans();
//loc.set
loc.setId(cursor.getInt(0));
loc.setLocname(cursor.getString(1));
loc.setLattitude(cursor.getDouble(2));
loc.setLongitude(cursor.getDouble(3));
loc.setSelectedprofile(cursor.getString(4));
loc.setContactno(cursor.getString(5));
loc.setMessage(cursor.getString(6));
// loc.setEnabled(cursor.getString(7));
//添加联系人列出
LocWiseProfileList.add(LOC);
}而(cursor.moveToNext());
}
//返回联系人列表
返回LocWiseProfileList;
}
@覆盖
公共无效onUpgrade(SQLiteDatabase数据库,诠释oldVersion,诠释静态网页){
// TODO自动生成方法存根
Log.w(MySQLiteHelper.class.getName(),从升级版本的数据库
+ oldVersion +到+静态网页
+,这将销毁所有旧数据);
database.execSQL(DROP TABLE IF EXISTS+ TABLE_NAME);
的onCreate(数据库);
}
公共无效删除(int值)
{
尝试{
SQLiteDatabase DB = this.getWritableDatabase();
db.delete(TABLE_NAME,COLUMN_ID +=+值,NULL);
}
赶上(例外五){}
}
公共无效用deleteAll(){
尝试{
//db.execSQL("delete *从+ TABLE_NAME);
SQLiteDatabase DB = this.getWritableDatabase();
db.delete(TABLE_NAME,NULL,NULL);
}赶上(例外五){}
}
}
您需要更改从 getView
原型:
公开查看getView(INT位置,查看convertView,父母的ViewGroup)
到
公开查看getView(最终诠释的立场,观点convertView,父母的ViewGroup)
然后添加到你的 viewHolder.checkbox.setOnCheckedChangeListener
监听器:
模型元素=(模型)viewHolder.checkbox.getTag();
element.setSelected(buttonView.isChecked());
InteractiveArrayAdapter.this.remove(InteractiveArrayAdapter.this.getItem(位置));
list.remove(位置); //这里要考虑是否能承受变化表
//如果你想增加code,以从数据库中删除的元素太
我还没有尝试过自己,但似乎会工作。
修改:按照你要求我加入了code来删除数据库中的条目:
在你的 MySQliteHelper
类中添加下面的方法:
公共无效deleteByName(字符串名称){
SQLiteDatabase DB = this.getWritableDatabase();
db.delete(TABLE_NAME,COLUMN1 +=?,新的String [] {名});
db.close();
}
然后改变听者的code:
模型元素=(模型)viewHolder.checkbox.getTag();
element.setSelected(buttonView.isChecked());
InteractiveArrayAdapter.this.remove(InteractiveArrayAdapter.this.getItem(位置));
MySQLiteHelper M =新MySQLiteHelper(InteractiveArrayAdapter.this.context);
m.deleteNyName(list.get(位置)的ToString());
list.remove(位置); //这里要考虑是否能承受变化表
在这里,我假设你的名字是唯一的。如果这是不正确的,你需要重构你的型号
类(其中,顺便说一句,已经是太混乱,我只是野生猜测,model.toString()将返回的名称的字符串)。
I fetch data from SQLite in Listview .I need to delete this item when user click on check
box and press the delete button or only clicking on the check box,
I don't understand how to do this? Here is my sample code for reference.
MyTable.java
public class MyTable extends ListActivity {
MySQLiteHelper m=new MySQLiteHelper(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
setResult(RESULT_OK, intent);
ArrayAdapter<Model> adapter = new InteractiveArrayAdapter(this,
getModel());
setListAdapter(adapter);
}
private List<Model> getModel()
{
List<Model> list = new ArrayList<Model>();
final MySQLiteHelper m=new MySQLiteHelper(getBaseContext());
final List<LocWiseProfileBeans> LocWiseProfile= m.getAllLocWiseProfile();
for (final LocWiseProfileBeans cn : LocWiseProfile) {
list.add((get(cn.getLocname())));
//list.add(get(cn.getSelectedprofile()));
// Initially select one of the items
list.get(0).setSelected(true);
}
return list;
}
private Model get(String s) {
return new Model(s);
}
InteractiveArrayAdapter.java
public class InteractiveArrayAdapter extends ArrayAdapter<Model> {
private final List<Model> list;
private final Activity context;
public InteractiveArrayAdapter(Activity context, List<Model> list)
{
super(context, R.layout.locprofile, list);
this.context = context;
this.list = list;
}
static class ViewHolder {
protected TextView text;
protected CheckBox checkbox;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
view = inflator.inflate(R.layout.locprofile, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.text = (TextView) view.findViewById(R.id.label);
viewHolder.checkbox = (CheckBox) view.findViewById(R.id.check);
viewHolder.checkbox
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
Model element = (Model) viewHolder.checkbox
.getTag();
element.setSelected(buttonView.isChecked());
}
});
view.setTag(viewHolder);
viewHolder.checkbox.setTag(list.get(position));
} else {
view = convertView;
((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));
}
ViewHolder holder = (ViewHolder) view.getTag();
holder.text.setText(list.get(position).getName());
holder.checkbox.setChecked(list.get(position).isSelected());
return view;
}
}
MySQliteHelper.java
public static final String TABLE_NAME = "loc_wise_profile";
public static final String TABLE_NAME2 = "supervisor";
public static final String COLUMN_ID = "_id";
public static final String COLUMN1 = "loc_name";
public static final String COLUMN2 = "lattitude";
public static final String COLUMN3 = "longitude";
public static final String COLUMN4 = "selectedprofile";
public static final String COLUMN5 = "contactno";
public static final String COLUMN6 = "message";
//public static final String COLUMN7 = "enabled";
public static final String COLUMN8 = "NotificeationMessage";
SQLiteDatabase db;
private static final String DATABASE_NAME = "locale";
private static final int DATABASE_VERSION = 2;
// Database creation sql statement
private static final String DATABASE_CREATE = "create table IF NOT EXISTS "
+ TABLE_NAME + "( " + COLUMN_ID
+ " integer primary key autoincrement, "
+ COLUMN1 + " text not null, "
+ COLUMN2 + " double not null, "
+ COLUMN3 + " double not null, "
+ COLUMN4 + " text not null ,"
+ COLUMN5 + " text not null ,"
+ COLUMN6 + " text not null ,"
//+ COLUMN7 + " text not null ,"
+ COLUMN8 + " text not null "
+ ");";
public MySQLiteHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase database) {
// TODO Auto-generated method stub
database.execSQL(DATABASE_CREATE);
//database.execSQL(DATABASE_CREATE2);
}
// Adding new Location wise Profile
public void insertLocWiseProfile(LocWiseProfileBeans loc) {
db= this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN1, loc.getLocname()); // Contact Name
values.put(COLUMN2, loc.getLattitude()); // Contact Phone Number
values.put(COLUMN3, loc.getLongitude());
values.put(COLUMN4, loc.getSelectedprofile());
values.put(COLUMN5, loc.getContactno());
values.put(COLUMN6, loc.getMessage());
//values.put(COLUMN7, loc.getEnabled());
values.put(COLUMN8, loc.getNotificationMessage());
// Inserting Row
db.insert(TABLE_NAME, null, values);
db.close(); // Closing database connection
}
// Getting All Contacts
public List<LocWiseProfileBeans> getAllLocWiseProfile() {
List<LocWiseProfileBeans> LocWiseProfileList = new ArrayList<LocWiseProfileBeans>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_NAME;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
LocWiseProfileBeans loc= new LocWiseProfileBeans();
//loc.set
loc.setId(cursor.getInt(0));
loc.setLocname(cursor.getString(1));
loc.setLattitude(cursor.getDouble(2));
loc.setLongitude(cursor.getDouble(3));
loc.setSelectedprofile(cursor.getString(4));
loc.setContactno(cursor.getString(5));
loc.setMessage(cursor.getString(6));
// loc.setEnabled(cursor.getString(7));
// Adding contact to list
LocWiseProfileList.add(loc);
} while (cursor.moveToNext());
}
// return contact list
return LocWiseProfileList;
}
@Override
public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
Log.w(MySQLiteHelper.class.getName(), "Upgrading database from version "
+ oldVersion + " to " + newVersion
+ ", which will destroy all old data");
database.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(database);
}
public void delete(int value)
{
try{
SQLiteDatabase db= this.getWritableDatabase();
db.delete(TABLE_NAME, COLUMN_ID+"="+value, null);
}
catch(Exception e){}
}
public void deleteAll(){
try{
//db.execSQL("delete * from "+ TABLE_NAME);
SQLiteDatabase db= this.getWritableDatabase();
db.delete(TABLE_NAME, null, null);
}catch(Exception e){}
}
}
You need to change the getView
prototype from:
public View getView(int position, View convertView, ViewGroup parent)
to
public View getView(final int position, View convertView, ViewGroup parent)
Then add to your viewHolder.checkbox.setOnCheckedChangeListener
listener:
Model element = (Model) viewHolder.checkbox.getTag();
element.setSelected(buttonView.isChecked());
InteractiveArrayAdapter.this.remove(InteractiveArrayAdapter.this.getItem(position));
list.remove(position); //here consider whether you can afford changing list
// If you want to add code to remove the element from the database too
I have not tried it myself, but it seems that will work.
EDIT: Following you request I am adding the code to delete the entry from your database:
In your MySQliteHelper
class add the following method:
public void deleteByName(String name) {
SQLiteDatabase db= this.getWritableDatabase();
db.delete(TABLE_NAME, COLUMN1 +"=?", new String [] { name });
db.close();
}
Then change the code of the listener:
Model element = (Model) viewHolder.checkbox.getTag();
element.setSelected(buttonView.isChecked());
InteractiveArrayAdapter.this.remove(InteractiveArrayAdapter.this.getItem(position));
MySQLiteHelper m = new MySQLiteHelper(InteractiveArrayAdapter.this.context);
m.deleteNyName(list.get(position).toString());
list.remove(position); //here consider whether you can afford changing list
Here I assume your names are unique. In case this is not true you will need to refactor your Model
class (which, by the way, is already too confusing and I am just wild guessing that model.toString() will return the string of the name).
这篇关于如何从ListView中删除的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!