我想从列表视图和数据库中删除选定的项目。我为列表视图和数据库插入编写了代码。
但问题是删除我正在使用 OnItemClick 侦听器删除特定项目。
对于删除项目,我将 Position 变量传递给数据库删除方法。它可以工作,但是当您删除列表的上部项目而不是尝试删除下部项目时,它不会删除。
可能是因为我将位置变量传递给数据库删除方法。在数据库中我用主键 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;
}
}
数据库助手类
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;
}
}