从 ListView(SQLite 数据库)中长按行删除
Posted
技术标签:
【中文标题】从 ListView(SQLite 数据库)中长按行删除【英文标题】:Remove by longclick on row from ListView (SQLite database) 【发布时间】:2015-11-01 03:13:09 【问题描述】:我尝试了很长时间从列表视图 (SQLite) 中删除一行。
到目前为止我能做什么?
-
在列表视图中按行显示所有数据。
长按显示菜单的行之一(删除或更新)。
我到现在还不能做什么?
-
如果我长按该行,则从行中获取 id。
** 我稍后尝试完成的删除和更新过程,我现在只需要获取我单击的行的 id。
我已经包含了相关文件和长按行后的 prtscrn
DataListActivity.java -- ** 最相关的文件 **
package com.example.ido.grades;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;
public class DataListActivity extends ActionBarActivity
ListView listView;
SQLiteDatabase sqLiteDatabase;
CourseDbHelper courseDbHelper;
Cursor cursor;
ListDataAdaptar listDataAdaptar;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.data_list_layout);
listView = (ListView) findViewById(R.id.list_view);
listDataAdaptar = new ListDataAdaptar(getApplicationContext(),R.layout.row_layout);
listView.setAdapter(listDataAdaptar);
registerForContextMenu(listView);
courseDbHelper = new CourseDbHelper(getApplicationContext());
sqLiteDatabase = courseDbHelper.getReadableDatabase();
cursor = courseDbHelper.getInformation(sqLiteDatabase);
registerForContextMenu(listView);
if (!cursor.moveToFirst())
else
do
String year,semester,course,points,grade;
year = cursor.getString(0);
semester = cursor.getString(1);
course = cursor.getString(2);
points = cursor.getString(3);
grade = cursor.getString(4);
DataProvider dataProvider = new DataProvider(year,semester,course,points,grade);
listDataAdaptar.add(dataProvider);
while (cursor.moveToNext());
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo)
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_data_list, menu);
public boolean onContextItemSelected(MenuItem item)
switch (item.getItemId())
case R.id.update_item:
Toast.makeText(this,"update",Toast.LENGTH_LONG).show();
return true;
case R.id.delete_item:
Toast.makeText(this,"delete",Toast.LENGTH_LONG).show();
return true;
default:
return super.onOptionsItemSelected(item);
data_list_layout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_ android:paddingLeft="@dimen/activity_horizontal_margin"
tools:context="com.example.ido.grades.DataListActivity">
<ListView
android:layout_
android:layout_
android:id="@+id/list_view"
></ListView>
</RelativeLayout>
ListDataAdapter.java
package com.example.ido.grades;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class ListDataAdaptar extends ArrayAdapter
List list = new ArrayList();
public ListDataAdaptar(Context context, int resource)
super(context, resource);
static class LayoutHandler
TextView YEAR,SEMESTER,COURSE,POINTS,GRADE;
@Override
public void add(Object object)
super.add(object);
list.add(object);
@Override
public int getCount()
return list.size();
@Override
public Object getItem(int position)
return list.get(position);
@Override
public View getView(int position, View convertView, ViewGroup parent)
View row = convertView;
LayoutHandler layoutHandler;
if (row == null)
LayoutInflater layoutInflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = layoutInflater.inflate(R.layout.row_layout,parent,false);
layoutHandler = new LayoutHandler();
layoutHandler.YEAR = (TextView)row.findViewById(R.id.textYear);
layoutHandler.SEMESTER = (TextView)row.findViewById(R.id.textSemester);
layoutHandler.COURSE = (TextView)row.findViewById(R.id.textCourse);
layoutHandler.POINTS = (TextView)row.findViewById(R.id.textPoints);
layoutHandler.GRADE = (TextView)row.findViewById(R.id.textGrade);
row.setTag(layoutHandler);
else
layoutHandler = (LayoutHandler) row.getTag();
DataProvider dataProvider = (DataProvider) this.getItem(position);
layoutHandler.YEAR.setText(dataProvider.getYear());
layoutHandler.SEMESTER.setText(dataProvider.getSemester());
layoutHandler.COURSE.setText(dataProvider.getCourse());
layoutHandler.POINTS.setText(dataProvider.getPoints());
layoutHandler.GRADE.setText(dataProvider.getGrade());
return row;
CourseDbHelper.java
package com.example.ido.grades;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class CourseDbHelper extends SQLiteOpenHelper
private static final String DATABASE_NAME = "COURSEINFO.DB";
private static final int DATABASE_VERSION = 1;
public String CREATE_QUERY = "CREATE TABLE " + UserCourse.NewCourseInfo.TABLE_NAME + "("+ UserCourse.NewCourseInfo.YEAR+" TEXT,"
+ UserCourse.NewCourseInfo.SEMESTER+" TEXT,"+ UserCourse.NewCourseInfo.COURSE+" TEXT,"+ UserCourse.NewCourseInfo.POINTS+" TEXT,"
+ UserCourse.NewCourseInfo.GRADE+" TEXT);";
public CourseDbHelper (Context context)
super(context, DATABASE_NAME, null, DATABASE_VERSION);
Log.e("DATABASE_OPERATIONS","Database created/opened...");
@Override
public void onCreate(SQLiteDatabase db)
db.execSQL(CREATE_QUERY);
Log.e("DATABASE_OPERATIONS", "Table created");
public void putInformation(String year, String semester, String course, String points,String grade,SQLiteDatabase db)
ContentValues cv = new ContentValues();
cv.put(UserCourse.NewCourseInfo.YEAR, year);
cv.put(UserCourse.NewCourseInfo.SEMESTER, semester);
cv.put(UserCourse.NewCourseInfo.COURSE, course);
cv.put(UserCourse.NewCourseInfo.POINTS,points);
cv.put(UserCourse.NewCourseInfo.GRADE, grade);
db.insert(UserCourse.NewCourseInfo.TABLE_NAME, null, cv);
Log.e("DATABASE_OPERATIONS", "One raw inserted");
public Cursor getInformation(SQLiteDatabase db)
Cursor cursor;
String[] projections = UserCourse.NewCourseInfo.YEAR,UserCourse.NewCourseInfo.SEMESTER,
UserCourse.NewCourseInfo.COURSE, UserCourse.NewCourseInfo.POINTS,UserCourse.NewCourseInfo.GRADE;
cursor= db.query(UserCourse.NewCourseInfo.TABLE_NAME,projections,null,null,null,null,null );
return cursor;
public void deleteInformation(String id)
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
【问题讨论】:
【参考方案1】:要获取选中行的索引,在'onContextItemSelected(MenuItem item)'中添加如下代码:
AdapterContextMenuInfo info =
(AdapterContextMenuInfo) item.getMenuInfo();`
int mySelectedRowIndex = info.position;
通过以下语句,您可以在所选行中获取 ListView 数据:
(DataProvider)listDataAdaptar.getItem(mySelectedRowIndex);
没有 id(= 数据库主键),因此您只能从表 'UserCourse.NewCourseInfo.TABLE_NAME' 中删除与所选 ListView 行中的值匹配的每一行。
(我不知道您对 SQL 了解多少 - 所以请注意:如果一行的值与输入值匹配,则该行将被删除!如果您只想删除一行,也许您应该包含一个主键表)。
所以你必须改变'deleteInformation()'方法:
public void deleteInformation(String year, String semester,
String grade, String course, String points)
SQLiteDatabase db = null;
try
db = this.getWritableDatabase();
String where = UserCourse.NewCourseInfo.YEAR + " = ? AND "
+ UserCourse.NewCourseInfo.SEMESTER + " = ? AND "
+ UserCourse.NewCourseInfo.COURSE + " = ? AND "
+ UserCourse.NewCourseInfo.POINTS + " = ? AND "
+ UserCourse.NewCourseInfo.GRADE + " = ?";
int iNumberDeleted = db.delete(
UserCourse.NewCourseInfo.TABLE_NAME,
where,
new String[]year, semester, course, points, grade
);
// you might want to evaluate the number of affected rows
catch (Exception ex)
Log.d("DB_DELETE", "...your data..." + "\n" + ex.getMessage());
finally
if (db != null)
db.close();
【讨论】:
它的作品!我得到行数(从0开始)。如何通过我得到的 ID 号删除原始数据?我认为它会不那么困难..(我添加了 CourseDbHelper java 文件,删除功能应该是)。 @Idon89 - 我可以帮你解决这个问题 - 我会看看你的 CourseDBHelper @Idon89 - 就是这样 :) 没有花时间进行设置和测试,但我认为这应该可行 首先,感谢您的帮助!!!!!!!我对 sql 的了解不是很好.. 但是我尝试学习... 1. 关于您的警告,您的意思是在 CREATE_QUERY 中添加类似的内容:+ UserCourse.NewCourseInfo.ID+"TEXT"," 2. i只知道要删除所选原始数据的原始索引,而对于您的函数,我需要原始数据,我该如何获取所选行数据(年,学期...)?, @Idon89 - 请参阅我关于如何获取行数据的答案(第二个代码块) - 我希望您的 DataProvider 有 s.th。像 getters ;) 然后,如果您的表有一个主键,您可以通过移交这个主键值来删除该行。但是使 ID 列 'INTEGER PRIMARY KEY AUTOINCREMENT' 有用的链接:sqlite.org/lang.html以上是关于从 ListView(SQLite 数据库)中长按行删除的主要内容,如果未能解决你的问题,请参考以下文章
无法从 Listview 中删除项目(待办事项列表),长按没有任何反应
Android Sqlite:如何从 Listview 中获取 SQLite rowId?
Android 从 SQLite 数据库填充自定义 ListView