好的,所以我试图从ListView中删除元素,一切顺利,直到我尝试删除最后一个元素,但只有当我删除第二个最后一个元素并尝试删除最后一个元素时。这是我的代码:
public class TestActivity extends ListActivity {
ListView list;
PAdapter adapter;
static SQLiteDatabase db;
static ArrayList<Profesor> datos;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
db = openOrCreateDatabase("DBLogin", MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS Profesor (Id integer primary key AUTOINCREMENT"
+ ", Nombre" + " varchar(30), Imagen varchar(15), Fecha" + " varchar(15)" +
", Direccion varchar(35), sexo varchar(10), Telefono varchar(15), creado int);");
list = (ListView) findViewById(android.R.id.list);
datos = new ArrayList<Profesor>();
datos = datos();
adapter = new PAdapter(this, R.layout.row, datos);
setListAdapter(adapter);
list.setOnItemLongClickListener(new OnItemLongClickListener(){
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
if (datos.get(arg2).getCreado() == 1) {
PopupMenu popup = new PopupMenu(TestActivity.this, list);
popup.getMenuInflater().inflate(R.menu.popup3, popup.getMenu());
final int id = arg2;
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
delete(datos.get((int)adapter.getItemId(id)));
datos = datos();
adapter = new PAdapter(TestActivity.this, R.layout.row, datos);
setListAdapter(adapter);
list.invalidateViews();
return true;
}
});
popup.show();
}
return false;
}
});
}
private ArrayList<Profesor> datos(){
ArrayList<Profesor> ap = new ArrayList<Profesor>();
Cursor cursor = db.rawQuery("SELECT * FROM Profesor", null);
if(cursor.moveToFirst()){
do {
Profesor p = new Profesor(null, null);
p.setId(cursor.getInt(0));
p.setNombre(cursor.getString(1));
p.setFecha(cursor.getString(3));
p.setDireccion(cursor.getString(4));
p.setSexo(cursor.getString(5));
p.setImagen(cursor.getString(2));
p.setCreado(cursor.getInt(7));
p.setTelefono(cursor.getString(6));
ap.add(p);
} while(cursor.moveToNext());
}
return ap;
}
public static void delete(Profesor p){
db.execSQL("DELETE FROM Profesor WHERE Id = " + p.getId() + ";");
}
这是PAdapter类:
public class PAdapter extends ArrayAdapter<Profesor> {
private Context context;
private int layout;
private ArrayList<Profesor> datos;
public PAdapter(Context context, int layout, ArrayList<Profesor> datos) {
super(context, layout, datos);
this.context = context;
this.layout = layout;
this.datos = datos;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View item = inflater.inflate(layout, parent, false);
ImageView imagen = (ImageView) item.findViewById(R.id.imageView1);
datos.get(position).imagen(imagen, context, false);
TextView nombre = (TextView) item.findViewById(R.id.tNombre);
nombre.setText(datos.get(position).getNombre());
return item;
}
}
有趣的是,它执行删除后的代码,但不删除最后一个元素,也不会引发异常。而且,它只发生在最后一个元素上,并且只有在我删除它之前,如果我关闭并重新打开应用程序之后我可以正常删除它。
答案 0 :(得分:1)
试试这个:它对我来说很好。
ListView list;
adapter = new MyListAdapter(this);
list = (ListView) findViewById(android.R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
AlertDialog.Builder adb=new AlertDialog.Builder(MyActivity.this);
adb.setTitle("Delete?");
adb.setMessage("Are you sure you want to delete " + position);
final int positionToRemove = position;
adb.setNegativeButton("Cancel", null);
adb.setPositiveButton("Ok", new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
MyDataObject.remove(positionToRemove);
adapter.notifyDataSetChanged();
}});
adb.show();
}
});
答案 1 :(得分:0)
我假设您通过pos = 3作为参数值。因为列表的大小是3,所以最后一个元素的位置应该是2。
*编辑:
请记住,listview和适配器的起始索引是不同的。 ListView项pos从“1”开始作为第一个位置,适配器(如数组)从索引“0”开始作为第一个位置。
adapter.remove(adapter.getItem(pos-1));
答案 2 :(得分:0)
您的代码中存在一些错误。我的以下建议不承诺将解决您当前的问题,但让我们尝试一下:
setListAdapter(adapter);
时要注意,列表会设置其适配器用户每次时间onClick
- 这是一种浪费。使用adapter.add()
/ adapter.remove()
而不是一次又一次地执行setListAdapter
重置所有数据。list.invalidateViews()
,为了什么?在您的情况下,adapter.notifyDataSetChanged()
已足够。答案 3 :(得分:0)
我通过在单个项目的活动中添加它来解决它,所以如果我回到ListView活动,它会因为finish()而刷新;功能。[2020年2月8日]
@Override
public void onBackPressed() {
super.onBackPressed();
startActivity(new Intent(SingleItem.this, ListView.class));
finish();
}