我已删除适配器类中的编码,如下所示:
delBtn=(ImageButton) v.findViewById(R.id.del_btn);
delBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sqlCon.deleteItem(id);
}
});
以下是 sqliteconnection类中的代码,
public void deleteItem(long id) {
System.out.println("DELETE ");
db = sqlHp.getWritableDatabase();
db.delete(ItemDb.TABLE, "_id="+id , null);
db.close();
}
点击删除按钮后,如何刷新列表视图?感谢。
修改 * 在A级 *
public Cursor getAllItem(String user) {
String[] userID = new String[] {user};
db = sqlHp.getReadableDatabase();
cur=db.query(ItemDb.TABLE, null, "user=?", userID, null, null, "name");
return cur;
}
public Cursor getOneItem(long id) {
db = sqlHp.getReadableDatabase();
cur=db.query(ItemDb.TABLE, null, "_id="+ id, null, null, null,null);
return cur;
}
C类:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] from = new String[] { ItemDb.NAME, ItemDb.CONDITION, ItemDb.EMAIL};
int[] to = new int[] { R.id.etItemName, R.id.etItemCondition, R.id.etEmail };
itemAdapter = new ItemAdapter(this,R.layout.list_item, null, from, to);
this.setListAdapter(itemAdapter);
UserName = (String) getIntent().getSerializableExtra("UserName");
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = settings.edit();
editor.putString("UserName", UserName); // here string is the value you want to save
editor.commit();
Log.i(Tag, "UserName1: "+ UserName);
}
C类的另一部分
@Override
protected void onResume() {
super.onResume();
new GetItem().execute((Object[]) null);
}
@SuppressWarnings("deprecation")
@Override
protected void onStop() {
Cursor cursor = itemAdapter.getCursor();
if (cursor != null)
cursor.deactivate();
itemAdapter.changeCursor(null);
super.onStop();
}
private class GetItem extends AsyncTask<Object, Object, Cursor> {
ItemSQLiteConnector dbConnector = new ItemSQLiteConnector(ItemActivity.this);
@Override
protected Cursor doInBackground(Object... params) {
return dbConnector.getAllItem(UserName);
}
@Override
protected void onPostExecute(Cursor result) {
itemAdapter.changeCursor(result);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_item, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Intent addItem = new Intent(ItemActivity.this, AddEditItem.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(addItem);
return super.onOptionsItemSelected(item);
}
}
B组:
public class ItemAdapter extends SimpleCursorAdapter {
private int layout;
private ImageButton editBtn;
private ImageButton delBtn;
ImageButton addBtn;
String selection, UserName;
LayoutInflater inflator;
final ItemAdapter myadapter = this;
private SQLiteDatabase db;
public ItemAdapter(Context context, int layout, Cursor c,String[] from, int[] to) {
super(context, layout, c, from, to,0);
this.layout = layout;
inflator= LayoutInflater.from(context);
}
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View v = inflator.inflate(layout, parent, false);
return v;
}
@Override
public void bindView(View v, final Context context, Cursor c) {
final int id = c.getInt(c.getColumnIndex(ItemDb.ID));
final String user = c.getString(c.getColumnIndex(ItemDb.USER));
final String name = c.getString(c.getColumnIndex(ItemDb.NAME));
final String condition = c.getString(c.getColumnIndex(ItemDb.CONDITION));
final String email = c.getString(c.getColumnIndex(ItemDb.EMAIL));
final String category = c.getString(c.getColumnIndex(ItemDb.CATEGORY));
final byte[] image = c.getBlob(c.getColumnIndex(ItemDb.IMAGE));
ImageView iv = (ImageView) v.findViewById(R.id.photo);
if (image != null) {
if (image.length > 3) {
iv.setImageBitmap(BitmapFactory.decodeByteArray(image, 0,image.length));
}
}
TextView tname = (TextView) v.findViewById(R.id.name);
tname.setText(name);
TextView tcondition = (TextView) v.findViewById(R.id.condition);
tcondition.setText(condition);
TextView temail = (TextView) v.findViewById(R.id.email);
temail.setText(email);
final ItemSQLiteConnector sqlCon = new ItemSQLiteConnector(context);
editBtn=(ImageButton) v.findViewById(R.id.edit_btn);
editBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(context, AddEditItem.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("id", id);
intent.putExtra("user", user);
intent.putExtra("name", name);
intent.putExtra("condition", condition);
intent.putExtra("email", email);
intent.putExtra("category", category);
intent.putExtra("blob", image);
context.startActivity(intent);
}
});
delBtn=(ImageButton) v.findViewById(R.id.del_btn);
delBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sqlCon.deleteItem(id);
//Cursor cur=db.query(ItemDb.TABLE, null, "_id="+ id, null, null, null,null);
//myadapter.changeCursor(cur);
//myadapter.notifyDataSetChanged();
//Intent intent=new Intent(context,Tab.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//context.startActivity(intent);
}
});
v.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(context,ViewItem.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("id", id);
intent.putExtra("user", user);
intent.putExtra("name", name);
intent.putExtra("condition", condition);
intent.putExtra("email", email);
intent.putExtra("category", category);
intent.putExtra("blob", image);
context.startActivity(intent);
}
});
}
}
答案 0 :(得分:4)
调用notifyDataSetChanged()
(来自适配器对象或适配器类)。
示例:
WhateverMyAdapterClassIs myadapter = new WhateverMyAdapterClassIs();
...
delBtn=(ImageButton) v.findViewById(R.id.del_btn);
delBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sqlCon.deleteItem(id);
//Add code to remove the respective item from the List,ArrayList
//or whatever you use in the adapter then call notifyDataSetChanged()
myadapter.notifyDataSetChanged();
}
});
或者,如果您已扩展了适配器类,请添加removeItem函数并从中调用notifyDataSetChanged
:
public void removeItem(int index) {
myList.remove(index);
notifyDataSetChanged();
}
在您的情况下使用:
final ItemAdapter myadapter = this;
delBtn=(ImageButton) v.findViewById(R.id.del_btn);
delBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sqlCon.deleteItem(id);
//Add code to remove the respective item from the List,ArrayList
//or whatever you use in the adapter then call notifyDataSetChanged()
myadapter.notifyDataSetChanged();
}
});
答案 1 :(得分:1)
public class ItemAdapter extends SimpleCursorAdapter {
private int layout;
private ImageButton editBtn;
private ImageButton delBtn;
ImageButton addBtn;
String selection, UserName;
LayoutInflater inflator;
final ItemAdapter myadapter = this;
private SQLiteDatabase db;
private OnItemDeleted mOnItemDeleted;
public interface OnItemDeleted {
public void updateCursor();
}
public ItemAdapter(Context context, int layout, Cursor c,
String[] from, int[] to, OnItemDeleted onItemDeleted) {
super(context, layout, c, from, to,0);
this.layout = layout;
mOnItemDeleted = onItemDeleted;
inflator= LayoutInflater.from(context);
}
.........
delBtn=(ImageButton) v.findViewById(R.id.del_btn);
delBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sqlCon.deleteItem(id);
mOnItemDeleted.updateCursor();
//Intent intent=new Intent(context,Tab.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//context.startActivity(intent);
}
});
}
在调用类C
中itemAdapter = new ItemAdapter(this,R.layout.list_item, null, from, to, new MyOnItemDeleted());
......
public class MyOnItemDeleted implements ItemAdapter.OnItemDeleted
{
public void updateCursor()
{
new GetItem().execute((Object[]) null);
}
}