我已经用这个圈了几天,我无法理解错误。 使用以下代码,当您按下"是"按钮删除您触摸的项目,它显示所有内容并且似乎执行所有操作,但项目未从列表中删除。
活动类:
package com1032.em00224.knit;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class NoteActivity extends Activity {
private ListView list;
DatabaseHandler db;
int id_To_Update = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_note);
db = new DatabaseHandler(this);
ArrayList array_list = db.getAllNotes();
ArrayAdapter arrayAdapter =
new ArrayAdapter(this,android.R.layout.simple_list_item_1, array_list);
//adding it to the list view.
list = (ListView)findViewById(R.id.listView1);
list.setAdapter(arrayAdapter);
list.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View arg1, final int position,
long arg3) {
// TODO Auto-generated method stub
AlertDialog.Builder adb = new AlertDialog.Builder(NoteActivity.this);
adb.setTitle("Delete Note?");
adb.setMessage(R.string.deleteNote);
adb.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
db.deleteNote(position + 1);
arrayAdapter.notifyDataSetChanged();
Toast.makeText(getApplicationContext(), "Note Deleted", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(), com1032.em00224.knit.NoteActivity.class);
startActivity(intent);
}
});
adb.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
adb.show();
}
});
}
数据库助手:
package com1032.em00224.knit;
import java.util.ArrayList;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase;
public class DatabaseHandler extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "NoteDatabase.db";
public static final String NOTES_TABLE_NAME = "notes";
public static final String NOTES_COLUMN_ID = "id";
public static final String NOTES_COLUMN_NOTE = "note";
public DatabaseHandler(Context context)
{
super(context, DATABASE_NAME , null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(
"create table notes " +
"(id integer primary key, note text)"
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS notes");
onCreate(db);
}
public boolean insertNote (String note)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("note", note);
db.insert("notes", null, contentValues);
return true;
}
public Cursor getData(int id){
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery( "select * from contacts where id="+id+"", null );
return res;
}
public int numberOfRows(){
SQLiteDatabase db = this.getReadableDatabase();
int numRows = (int) DatabaseUtils.queryNumEntries(db, NOTES_TABLE_NAME);
return numRows;
}
public boolean updateNote (Integer id, String note)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("note", note);
db.update("notes", contentValues, "id = ? ", new String[] { Integer.toString(id) } );
return true;
}
public Integer deleteNote (Integer id)
{
SQLiteDatabase db = this.getWritableDatabase();
return db.delete("notes",
"id = ? ",
new String[] { Integer.toString(id) });
}
public ArrayList getAllNotes()
{
ArrayList array_list = new ArrayList();
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery( "select * from notes", null );
res.moveToFirst();
while(res.isAfterLast() == false){
array_list.add(res.getString(res.getColumnIndex(NOTES_COLUMN_NOTE)));
res.moveToNext();
}
return array_list;
}
}
其他所有内容,例如添加新项目,都可以正常运行。
编辑 - 它现在删除所选的一个,但它并不总是实际删除,似乎没有模式 - 我添加了一些system.out.printlns
以获得更清晰的图片但它说当它不是时会删除正确的那个。
答案 0 :(得分:0)
在某些项目中,我遇到了同样的问题。所以有两种可能的解决方案。首先,从数据库中删除您的数据后,还要从listview / Adapter中删除项目并调用notifyDataSetChanged:
db.deleteNote(position);
arrayAdapter.remove(arrayAdapter.getItem(itemToRemove));
arrayAdapter.notifyDataSetChanged();
所以,我的问题是,这有时不起作用,直到现在,我不知道我做错了什么。无论如何,如果这不起作用,请在删除后刷新整个数据:
private void refreshData(){
ArrayList array_list = db.getAllNotes();
ArrayAdapter arrayAdapter =
new ArrayAdapter(this,android.R.layout.simple_list_item_1, array_list);
list.setAdapter(arrayAdapter);
list.invalidateViews();
}
所以这应该在你的onClick中看起来像这样:
adb.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
db.deleteNote(position);
refreshData();
Toast.makeText(getApplicationContext(), "Note Deleted", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(), com1032.em00224.knit.NoteActivity.class);
startActivity(intent);
}
});
这只是从头开始,我现在无法测试代码,因为我这里没有IDE。所以不要忘记做一些解决方法(比如打开和关闭数据库等)。