我想从列表视图和数据库中删除所选项目。 我已经为List-view和Database inserted编写了代码。
但问题是删除我使用OnItemClick Listener删除特定项目。
删除项目i将位置变量传递给数据库删除方法。 它工作但是当你删除列表的上部项目而不是你试图删除下面的项目时它不会删除。
可能是因为我将位置变量传递给数据库删除方法。在数据库中我用主键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;
}
}
答案 0 :(得分:2)
通常情况下,您会检索List
某种对象(在您的情况下说:收藏列表),然后将其传递给adapter
进行显示它在ListView
。所以你的CustomAdapter
将是这样的:
public class ListAdapter extends BaseAdapter {
private Context context;
private ArrayList<Favorites> items;
private LayoutInflater inflater=null;
public ListAdapter(Context context, ArrayList<Favorites> items) {
this.context = context;
this.items = items;
inflater = LayoutInflater.from(context);
}
public int getCount() {
return items.size();
}
public Favorites getItem(int position) {
return items.get(position);
}
public long getItemId(int position) {
return items.get(position).getId();
}
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, parent, false);
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(items.get(position).getName());
return vi;
}
}
然后,实例化适配器并将其传递给listView,如下所示:
//retreive the list of contacts
List<Fevorites> contacts = db.getAllContacts();
ListAdapter adapter = new ListAdapter(ActivityName.this , contacts);
list.setAdapter(adapter);
注意:将ActivityName
替换为您的活动名称