如何通过单击删除包含 SQLite 数据的列表视图行
How to Delete a list view row with SQLite data by click
我的代码不会从数据库中删除一行。我必须更改哪个块?
ListActivity.Java
这是我编写删除代码的部分。我卡在这里了。
public class ListDataActivity extends AppCompatActivity {
private ListView listView;
private DatabaseHelper databaseHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_data);
databaseHelper = new DatabaseHelper(this);
listView = findViewById(R.id.listViewId);
loadData();
}
public void loadData() {
ArrayList<String> listData = new ArrayList<>();
Cursor cursor = databaseHelper.displayAllData();
if(cursor.getCount() == 0){
Toast.makeText(getApplicationContext(),"no data is available",Toast.LENGTH_LONG).show();
}else{
while (cursor.moveToNext()){
listData.add(cursor.getString(0)+" \t "+cursor.getString(1)+" \t"+cursor.getString(2));
}
}
ArrayAdapter<String> adapter = new ArrayAdapter<String> (this,R.layout.list_item,R.id.textViewId,listData);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String selectedValue = adapterView.getItemAtPosition(i).toString();
Toast.makeText(getApplicationContext(),"Selected Value : "+selectedValue,Toast.LENGTH_LONG).show();
Intent intent = new Intent(getApplicationContext(),ListDataActivity.class);
AlertDialog.Builder alert = new AlertDialog.Builder(ListDataActivity.this);
alert.setTitle("Delete");
alert.setMessage("Do you want to delete this item from list?");
alert.setCancelable(false);
alert.setNegativeButton("No", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alert.setPositiveButton("Yes", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
listData.remove(i);
adapter.notifyDataSetChanged();
}
});
AlertDialog alertDialog = alert.create();
alertDialog.show();
}
});
}
}
Database.Java
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "student.db";
private static final String TABLE_NAME = "student_details";
private static final String ID = "_id";
private static final String NAME = "Name";
private static final String PHONE = "Phone";
private static final int VERSION_NUMBER = 5;
private static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + "(" + ID + " INTEGER PRIMARY KEY AUTOINCREMENT , " + NAME + " VARCHAR(255) NOT NULL," + PHONE + " INTEGER(11) NOT NULL )";
private static final String DROP_TABLE = "DROP TABLE IF EXISTS " + TABLE_NAME;
private static final String SELECT_ALL = "SELECT * FROM " + TABLE_NAME;
private Context context;
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, VERSION_NUMBER);
this.context = context;
}
public int deleteData(String name) {
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
int value = sqLiteDatabase.delete(TABLE_NAME, NAME + " = ", new String[]{name});
return value;
}
}
How to delete a row from listview as I have used click listener is there any way to delete the row from the listview. I used SQLite Database in my project.
这是我的答案:
public int deleteData(String name) {
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
int value = sqLiteDatabase.delete(TABLE_NAME, ID + " = ?", new String[]{name});
return value;
}
我的代码不会从数据库中删除一行。我必须更改哪个块?
ListActivity.Java
这是我编写删除代码的部分。我卡在这里了。
public class ListDataActivity extends AppCompatActivity {
private ListView listView;
private DatabaseHelper databaseHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_data);
databaseHelper = new DatabaseHelper(this);
listView = findViewById(R.id.listViewId);
loadData();
}
public void loadData() {
ArrayList<String> listData = new ArrayList<>();
Cursor cursor = databaseHelper.displayAllData();
if(cursor.getCount() == 0){
Toast.makeText(getApplicationContext(),"no data is available",Toast.LENGTH_LONG).show();
}else{
while (cursor.moveToNext()){
listData.add(cursor.getString(0)+" \t "+cursor.getString(1)+" \t"+cursor.getString(2));
}
}
ArrayAdapter<String> adapter = new ArrayAdapter<String> (this,R.layout.list_item,R.id.textViewId,listData);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String selectedValue = adapterView.getItemAtPosition(i).toString();
Toast.makeText(getApplicationContext(),"Selected Value : "+selectedValue,Toast.LENGTH_LONG).show();
Intent intent = new Intent(getApplicationContext(),ListDataActivity.class);
AlertDialog.Builder alert = new AlertDialog.Builder(ListDataActivity.this);
alert.setTitle("Delete");
alert.setMessage("Do you want to delete this item from list?");
alert.setCancelable(false);
alert.setNegativeButton("No", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alert.setPositiveButton("Yes", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
listData.remove(i);
adapter.notifyDataSetChanged();
}
});
AlertDialog alertDialog = alert.create();
alertDialog.show();
}
});
}
}
Database.Java
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "student.db";
private static final String TABLE_NAME = "student_details";
private static final String ID = "_id";
private static final String NAME = "Name";
private static final String PHONE = "Phone";
private static final int VERSION_NUMBER = 5;
private static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + "(" + ID + " INTEGER PRIMARY KEY AUTOINCREMENT , " + NAME + " VARCHAR(255) NOT NULL," + PHONE + " INTEGER(11) NOT NULL )";
private static final String DROP_TABLE = "DROP TABLE IF EXISTS " + TABLE_NAME;
private static final String SELECT_ALL = "SELECT * FROM " + TABLE_NAME;
private Context context;
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, VERSION_NUMBER);
this.context = context;
}
public int deleteData(String name) {
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
int value = sqLiteDatabase.delete(TABLE_NAME, NAME + " = ", new String[]{name});
return value;
}
}
How to delete a row from listview as I have used click listener is there any way to delete the row from the listview. I used SQLite Database in my project.
这是我的答案:
public int deleteData(String name) {
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
int value = sqLiteDatabase.delete(TABLE_NAME, ID + " = ?", new String[]{name});
return value;
}