gpt4 book ai didi

android - 如何在 ListView 中由用户删除 Android 应用程序中 Sqlite 数据库中的一行

转载 作者:搜寻专家 更新时间:2023-10-30 22:01:00 25 4
gpt4 key购买 nike

我用 SQLite 数据库制作了一个 Android 应用程序。我希望用户能够在 ListView 中选择数据库的一行并能够删除该特定行。我已经有了数据库和 ListView 。但我找不到如何删除一行。有人可以帮帮我吗?这是我的数据库助手:

package com.persoonlijk.rooster.test2;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
import java.util.ArrayList;
import java.util.List;

public class DataManipulator
{
public static final String KEY_ROWID = "id";
public static final String KEY_DAG = "dag";
public static final String KEY_UUR = "uur";
public static final String KEY_VAK = "vak";
public static final String KEY_LOKAAL = "lokaal";

private static final String DATABASE_NAME = "mydatabase.db";
static final String TABLE_NAME = "newtable";
private static final int DATABASE_VERSION = 11;

private static Context context;
static SQLiteDatabase db;

private SQLiteStatement insertStmt;
private static final String INSERT = "insert into " + TABLE_NAME + " (dag,uur,vak,lokaal) values (?,?,?,?)";
public DataManipulator(Context context) {
DataManipulator.context = context;
OpenHelper openHelper = new OpenHelper(DataManipulator.context);
DataManipulator.db = openHelper.getWritableDatabase();
this.insertStmt = DataManipulator.db.compileStatement(INSERT);
}

public long insert(String dag,String uur,String vak,String lokaal) {
this.insertStmt.bindString(1, dag);
this.insertStmt.bindString(2, uur);
this.insertStmt.bindString(3, vak);
this.insertStmt.bindString(4, lokaal);
return this.insertStmt.executeInsert();
}

public void deleteAll() {
db.delete(TABLE_NAME, null, null);
}

public List<String[]> selectAll()
{
List<String[]> list = new ArrayList<String[]>();
Cursor cursor = db.query(TABLE_NAME, new String[] { "id","dag","uur","vak","lokaal" }, null, null, null, null, "dag asc");
int x=0;
if (cursor.moveToFirst()) {
do {
String[] b1=new String[] {cursor.getString(0),cursor.getString(1),cursor.getString(2),
cursor.getString(3),cursor.getString(4)};
list.add(b1);
x=x+1;
} while (cursor.moveToNext());
}
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
cursor.close();
return list;
}

public void delete(int rowId) {
db.delete(TABLE_NAME, null, null);
}

private static class OpenHelper extends SQLiteOpenHelper {
OpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}


@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_NAME + " (id INTEGER PRIMARY KEY, dag TEXT, uur TEXT, vak TEXT, lokaal TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}

}

这是我对数据库中数据的 ListView : 包 com.persoonlijk.rooster.test2;

 import java.util.ArrayList;
import java.util.List;

import com.persoonlijk.rooster.test2.SaveData;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.view.View.OnClickListener;
import android.app.Activity;

public class MainActivity extends ListActivity{
TextView selection;
public int idToModify;
DataManipulator dm;

List<String[]> list = new ArrayList<String[]>();
List<String[]> names2 =null ;
String[] stg1;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.check);

dm = new DataManipulator(this);
names2 = dm.selectAll();

stg1=new String[names2.size()];
int x=0;
String stg;

for (String[] dag : names2) {
stg = dag[1]+" - "+dag[2]+ " - "+dag[3]+" - "+dag[4];
stg1[x]=stg;
x++;
}

ArrayAdapter<String> adapter = new ArrayAdapter<String> (this,android.R.layout.simple_list_item_1,stg1);
this.setListAdapter(adapter);
selection=(TextView)findViewById(R.id.selection);

}

public void onListItemClick(ListView parent, View v, int position, long id) {
selection.setText(stg1[position]);
}

//menuknoppen

@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(Menu.NONE, 0, 0, "Voeg gegevens toe");
menu.add(Menu.NONE, 1, 1, "Verwijder gegevens");
return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 0:
startActivity(new Intent(this, SaveData.class));
return true;
case 1:
startActivity(new Intent(this, VerwijderData.class));
return true;
}
return false;
}

}

这就是代码现在的样子: 包 com.persoonlijk.rooster.test2;

import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends ListActivity{
TextView selection;
public int idToModify;
DataManipulator dm;

List<String[]> list = new ArrayList<String[]>();
List<String[]> names2 =null ;
String[] stg1;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.check);

dm = new DataManipulator(this);
names2 = dm.selectAll();

stg1=new String[names2.size()];
int x=0;
String stg;

for (String[] dag : names2) {
stg = dag[1]+" - "+dag[2]+ " - "+dag[3]+" - "+dag[4];
stg1[x]=stg;
x++;
}

ArrayAdapter<String> adapter = new ArrayAdapter<String> (this,android.R.layout.simple_list_item_1,stg1);
this.setListAdapter(adapter);
selection=(TextView)findViewById(R.id.selection);
}

//menuknoppen
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(Menu.NONE, 0, 0, "Voeg gegevens toe");
menu.add(Menu.NONE, 1, 1, "Verwijder gegevens");
return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 0:
startActivity(new Intent(this, SaveData.class));
return true;
case 1:
startActivity(new Intent(this, VerwijderData.class));
return true;

}
return false;
}

}

现在,在我的 MainActivity 菜单中,您可以单击“verwijder”,然后转到 VerwijderData Activity 。这个 VerwijderData Activity 曾经是 MainActivity。在 VerwijderData Activity 中,用户删除了数据,这是我的应用程序不会自行刷新的 Activity 。这是来自 VerwijderData Activity 的代码:包 com.persoonlijk.rooster.test2;

import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class VerwijderData extends ListActivity{
TextView selection;
public int idToModify;
DataManipulator dm;
ArrayAdapter<String> adapter;

List<String[]> list = new ArrayList<String[]>();
List<String[]> names2 =null ;
String[] stg1;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.verwijder);

dm = new DataManipulator(this);
names2 = dm.selectAll();

stg1=new String[names2.size()];
int x=0;
String stg;

for (String[] dag : names2) {
stg = dag[1]+" - "+dag[2]+ " - "+dag[3]+" - "+dag[4];
stg1[x]=stg;
x++;
}

adapter = new ArrayAdapter<String> (this,android.R.layout.simple_list_item_1,stg1);
this.setListAdapter(adapter);
selection=(TextView)findViewById(R.id.selection);

}


public void onListItemClick(ListView parent, View v, int position, long id) {
String[] delete = names2.get(position);
String idString = delete[0];
long idLong = Long.valueOf(idString);
Log.d("Deleting...", idLong + "");
dm.delete(idLong);
names2.remove(position);

stg1=new String[names2.size()];
int x=0;
String stg;

for (String[] dag : names2) {
stg = dag[1]+" - "+dag[2]+ " - "+dag[3]+" - "+dag[4];
stg1[x]=stg;
x++;
}
adapter.notifyDataSetChanged();

}

}

自从我第一次提出问题以来,我的申请发生了一些变化,希望您仍然理解我的意思。

最佳答案

首先,我们需要重新处理您的数据库删除方法。它不会像现在这样做任何事情。

public boolean delete(long rowId) {
/* this is what your database delete method should look like
this method deletes by id, the first column in your database*/
return db.delete(TABLE_NAME, KEY_ROWID + "=" + rowId, null) > 0;
}

现在剩下的就是Activity的onListItemClick方法

    public void onListItemClick(ListView parent, View v, int position, long id) {
/* it looks like you're using an ArrayAdapter so you're gonna need to delete
both from the listview and from the database.
first we refer specifically to the corresponding item in the list by position
which is provided by the listItemClick parameter*/
String[] delete = names2.get(position);
/* we do this because we want the id to delete by in the database by id
based on your dm.selectAll() method, it should be at the first index of the array*/
String idString = delete[0];
// you've made it into a string, but we need it back to being a long value
long idLong = Long.valueOf(idString);
Log.d("Deleting...", idLong + "");
/* i put in the log statement for debugging purposes, it will put the id value on Logcat
so if something goes wrong, we can at least rule out the step since we see the value being
removed, but!
finally we can delete it from our database because we have the parameter we need*/
dm.delete(idLong);
/* There it's done, but surely you want it off the listview too so no one is confused.
unfortunately you've made stg1 an array so we can't just remove the index from it*/
names2.remove(position);

stg1=new String[names2.size()];
int x=0;
String stg;

for (String[] dag : names2) {
stg = dag[1]+" - "+dag[2]+ " - "+dag[3]+" - "+dag[4];
stg1[x]=stg;
x++;
}
/* can you see what happened? the `List` collection is able have its components dynamically
added and removed, so i removed from that, then i remade the stg1 collection using your method.*/
adapter.notifyDataSetChanged();
// ^ that command refreshes the listview to see changes.
}

考虑到所有因素,您已经使这项任务比当前设置所需的繁琐得多。首先,您在输入到适配器之前已经制作了大量的中间集合,然后您将一个数组作为数据源,它不能被动态调整。

更令人满意的是使用 CursorAdapter 进行设置。然后您可以使用 ListItemClick 方法中提供的 id 参数,因为它就是我们想要的确切 ID。此外,您只需要从一个数据库源中删除,然后只需刷新适配器即可。


让你的适配器像其他一些变量一样成为一个字段,以便在整个类中引用它:

public class MainActivity extends ListActivity{    
TextView selection;
public int idToModify;
DataManipulator dm;
......
ArrayAdapter<String> adapter; <--------- add that

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.check);
.........
adapter = new ArrayAdapter<String> (this,android.R.layout.simple_list_item_1,stg1); <------ edit that
this.setListAdapter(adapter);
}

}

关于android - 如何在 ListView 中由用户删除 Android 应用程序中 Sqlite 数据库中的一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14111769/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com