我想從列表視圖和數據庫中刪除所選項目。 我已經編寫了用於列表視圖和數據庫插入的代碼。如何從數據庫和列表視圖中刪除所選項目
但問題是刪除我正在使用OnItemClick監聽器刪除特定項目。
而對於刪除項目,我將位置變量傳遞給數據庫刪除方法。 它的工作,但是當你刪除列表的上層項目,並且你試圖刪除較低的項目它不會刪除。
可能是因爲我是通過位置變量數據庫中刪除method.and數據庫我與主鍵ID檢查它和較低的項目上項目ID的刪除後不會改變,因此出現這個問題
建議我一些很好的解決方案
這裏是代碼片段代碼
public void onStart() {
super.onStart();
initfrag();}
private void initfrag() {
// TODO Auto-generated method stub
dh = new com.myapp.database.DatabaseHelper(getActivity());
nameArray = new ArrayList<String>();
db = new com.myapp.database.DatabaseHelper(getActivity());
list = (ListView)getView().findViewById(R.id.fevlist);
adapter=new ListAdapter(getActivity(), nameArray);
Log.d("Reading: ", "Reading all contacts..");
List<Fevorites> contacts = db.getAllContacts();
for (Fevorites cn : contacts) {
s = "Name: " + cn.getID() + " ,Phone: " + cn.getPhoneNumber()+"\n";
nameArray.add(s);
list.setAdapter(adapter);
}
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView adapterView, View view, int position, long id) {
// TODO Auto-generated method stu
nameArray.remove(position);
Toast t = Toast.makeText(getActivity(), nameArray.get(position), Toast.LENGTH_SHORT);
t.show();
// dh.Deleteitem();
adapter.notifyDataSetChanged();
adapter=new ListAdapter(getActivity(), nameArray);
list.setAdapter(adapter);
}
});
}
下面是Fevorites
public class Fevorites {
//private variables
int _id;
String _name;
String _phone_number;
// Empty constructor
public Fevorites(){
}
// constructor
public Fevorites(int id, String name, String _phone_number){
this._id = id;
this._name = name;
this._phone_number = _phone_number;
}
// constructor
public Fevorites(String name, String _phone_number){
this._name = name;
this._phone_number = _phone_number;
}
// getting ID
public int getID(){
return this._id;
}
// setting id
public void setID(int id){
this._id = id;
}
// getting name
public String getName(){
return this._name;
}
// setting name
public void setName(String name){
this._name = name;
}
// getting phone number
public String getPhoneNumber(){
return this._phone_number;
}
// setting phone number
public void setPhoneNumber(String phone_number){
this._phone_number = phone_number;
}
}
Databasehelper類
public DatabaseHelper(Context context) {
super(context,DATABASE_NAME , null,DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_ARTIST + " TEXT,"
+ KEY_SONG + " TEXT" + ")";
db.execSQL(CREATE_CONTACTS_TABLE);
}
public void addContact() {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_ARTIST, "XYZ "); // Contact Name
values.put(KEY_SONG, "XYZ testing "); // Contact Phone
// Inserting Row
db.insert(TABLE_CONTACTS, null, values);
db.close(); // Closing database connection
}
public void Deleteitem(String string)
{
SQLiteDatabase db = this.getWritableDatabase();
String query = "DELETE FROM favorite WHERE id="+string;
db.execSQL(query);
}
// Getting All Contacts
public List<Fevorites> getAllContacts() {
List<Fevorites> contactList = new ArrayList<Fevorites>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_CONTACTS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Fevorites contact = new Fevorites();
contact.setID(Integer.parseInt(cursor.getString(0)));
contact.setName(cursor.getString(1));
contact.setPhoneNumber(cursor.getString(2));
// Adding contact to list
contactList.add(contact);
} while (cursor.moveToNext());
}
// return contact list
return contactList;
}
Fevorites getContact(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_CONTACTS, new String[] { KEY_ID,
KEY_SONG, KEY_ARTIST }, KEY_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Fevorites contact = new Fevorites(Integer.parseInt(cursor.getString(0)),
cursor.getString(1), cursor.getString(2));
// return contact
return contact;
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
這是自定義列表適配器類
* 列表適配器類*
public class ListAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<String> name;
private static LayoutInflater inflater=null;
public ListAdapter(Activity a, ArrayList<String> nameArray) {
activity = a;
name = nameArray;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return name.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public static class ViewHolder{
public TextView text;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
ViewHolder holder;
if(convertView==null){
vi = inflater.inflate(R.layout.fevorites_list, null);
holder=new ViewHolder();
holder.text=(TextView)vi.findViewById(R.id.list_item_entry_title);
vi.setTag(holder);
}
else
holder=(ViewHolder)vi.getTag();
holder.text.setText(name.get(position));
return vi;
}
}
在'onItemClick'中使用'int id'參數而不是'int position'來更好地知道數據庫中的項目ID? – fiddler
項目的位置在您每次刪除項目時都會發生變化,因此請通過方法adapter.getItemId()使用每個項目的ID。並傳遞給你的dbhelper刪除行通過編號 – Houcine
@Houcine我試過adapter.getItemID()像這樣,但它不會工作 dh.Deleteitem(adapter.getItemId(position)) –