좋아, 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;
}
}
이재미있는 것은이 , 그것은, 삭제를 다음 코드를 실행하지만 마지막 요소를 삭제하지 않습니다하지도 않습니다 그것은 나에게 예외를 던지나요. 다시 말하지만, 마지막 요소에서만 발생합니다. 이전에 삭제 한 후에 만 닫은 후 다시 열면 정상적으로 삭제할 수 있습니다.
확인 : http://stackoverflow.com/questions/26623385/how-can-i-delete-my-list-row-by-clicking-on-button-in-each-row-of- the-listview/26623451 # 26623451 –