我创建了一个列表视图。在按钮上单击,数据将添加到列表中,并存储在SQLite数据库中。我还在列表的右侧添加了图像,点击其中删除了特定的行数据,但我还想从SQLite中删除数据。我怎么能这样做?
Main Activity.Java
public class MainActivity extends Activity {
EditText editText;
Button Button,Button1;
ListView listView;
ArrayList<String> listItems;
BaseAdapter adapter;
private DataBaseHandler mHelper;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mHelper=new DataBaseHandler(this);
editText = (EditText) findViewById(R.id.editText);
Button = (Button) findViewById(R.id.Button);
listView = (ListView) findViewById(R.id.listview);
listItems = new ArrayList<String>();
adapter =new BaseAdapter()
{
@Override
public View getView(final int arg0, View arg1, ViewGroup arg2)
{
LayoutInflater inflater = getLayoutInflater();
arg1 = inflater.inflate(R.layout.custom, null);
TextView textview = (TextView)arg1. findViewById(R.id.textView1);
textview.setText(listItems.get(arg0));
ImageView image = (ImageView)arg1. findViewById(R.id.imageView1);
image.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
listItems.remove(arg0);
listView.setAdapter(adapter);
Toast.makeText(MainActivity.this, "Item has been successfully deleted", Toast.LENGTH_LONG)
.show();
}
});
return arg1;
}
@Override
public long getItemId(int arg0) {
return 0;
}
@Override
public Object getItem(int arg0) {
return null;
}
@Override
public int getCount() {
//code to set the list item size according to array data size
return listItems.size();
}
};
listView.setAdapter(adapter);
Button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
listItems= mHelper.addListItem(editText.getText().toString());
adapter.notifyDataSetChanged();
listView.setSelection(listView.getAdapter().getCount()-1);
editText.getText().clear();
}
});
}
}
DATABASE HANDLER.JAVA
public class DataBaseHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "MyDatabase.db";
private static final String TABLE_LIST = "MyListItem";
private static final String KEY_ID = "id";
private static final String KEY_ListItem = "listitem";
public static final String KEY_NAME = null;
public DataBaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
String CREATE_LIST_TABLE = "CREATE TABLE " + TABLE_LIST + "(" + KEY_ID
+ " integer primary key autoincrement," + KEY_ListItem + " TEXT" + ")";
db.execSQL(CREATE_LIST_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_LIST);
onCreate(db);
}
ArrayList<String> addListItem(String listItem)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_ListItem, listItem);
db.insert(TABLE_LIST, null, values);
ArrayList<String> items=new ArrayList<String>();
String selectQuery = "SELECT * FROM " + TABLE_LIST;
Cursor cursor = db.rawQuery(selectQuery, null);
cursor.moveToFirst();
while (!cursor.isAfterLast())
{
items.add(cursor.getString(1));
cursor.moveToNext();
}
cursor.close();
db.close();
return items;
}
Cursor getListItem()
{
String selectQuery = "SELECT * FROM " + TABLE_LIST;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
return cursor;
}
}
答案 0 :(得分:2)
要删除单个数据条目(即Row),请在数据库处理程序中创建一个方法:
public void delete(int id) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_NAME, KEY_ID + "=?",
new String[] { String.valueOf(id) }); // KEY_ID= id of row and third parameter is argument.
db.close();
}
此方法将删除已给出ID的单行。
答案 1 :(得分:0)
您需要在DataBaseHandler类中创建方法deleteItem
。这将做类似的事情,
db.delete(TABLE_LIST , KEY_ID + "=" + yourIdToBeDeleted, null);
然后从删除按钮/图片的onClick
中调用此方法:
mHelper.deleteItem(idOfItemToBeDeleted);
希望它有所帮助。