vendredi 10 avril 2015

Remove item from SQLite-listview

well i want to remove Item from Sqlite which is appear in Listview I can remove item from listview and SQLite but the problem is < list view is an array > and when i delete item from item it generate new array with new indext start with 0 and this conflict with Database ID , so it removes what i don't want





public class MySQLiteHelper extends SQLiteOpenHelper {

public static final String TABLE_EMPLOYEES = "employees";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_EMPNAME = "empname";
public static final String COLUMN_AGE = "age";
private static final String DATABASE_NAME = "empdb.db";
private static final int DATABASE_VERSION = 1;

// Database creation sql statement
private static final String DATABASE_CREATE = "create table "
+ TABLE_EMPLOYEES + "(" + COLUMN_ID
+ " integer primary key autoincrement, " + COLUMN_EMPNAME
+ " text not null , " + COLUMN_AGE
+ " integer not null);";

public MySQLiteHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(DATABASE_CREATE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(MySQLiteHelper.class.getName(),
"Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + TABLE_EMPLOYEES);
onCreate(db);

}


}



public class Employee {



private long id;
private int age;
private String empName;


public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
// Will be used by the ArrayAdapter in the ListView
@Override
public String toString() {
return "Name: "+empName+" Age: "+age+" "+ "ID : "+id ;
}


} public class EmployeesDataSource {



private SQLiteDatabase database;
private MySQLiteHelper dbHelper;
private String[] allColumns = { MySQLiteHelper.COLUMN_ID,
MySQLiteHelper.COLUMN_EMPNAME, MySQLiteHelper.COLUMN_AGE , MySQLiteHelper.COLUMN_ID};

public EmployeesDataSource(Context context) {
// TODO Auto-generated constructor stub
dbHelper = new MySQLiteHelper(context);
}

public void open() throws SQLException {
database = dbHelper.getWritableDatabase();
}

public void close() {
dbHelper.close();
}

public Employee createEmployee(String empname, int age ) {
ContentValues values = new ContentValues();
values.put(MySQLiteHelper.COLUMN_EMPNAME, empname);
values.put(MySQLiteHelper.COLUMN_AGE, age);

long insertId = database.insert(MySQLiteHelper.TABLE_EMPLOYEES, null,values);

Cursor cursor = database.query(MySQLiteHelper.TABLE_EMPLOYEES,
allColumns, MySQLiteHelper.COLUMN_ID + " = " + insertId, null,
null, null, null);
cursor.moveToFirst();
Employee newComment = cursorToEmployee(cursor);
cursor.close();
return newComment;

}


public void deleteEmployee(long itemId){
open();
database.delete(MySQLiteHelper.TABLE_EMPLOYEES, "_id = ?", new String[]{Long.toString(itemId)});
close();
}

public void deleteItem(long id){
open();
database.delete(MySQLiteHelper.TABLE_EMPLOYEES, MySQLiteHelper.COLUMN_EMPNAME + " = " + id, null);
close();
}





public List<Employee> getAllEmployees() {
List<Employee> employees = new ArrayList<Employee>();

Cursor cursor = database.query(MySQLiteHelper.TABLE_EMPLOYEES,
allColumns, null, null, null, null, null);

cursor.moveToFirst();
while (!cursor.isAfterLast()) {
Employee employee = cursorToEmployee(cursor);
employees.add(employee);
cursor.moveToNext();
}
// Make sure to close the cursor
cursor.close();
return employees;
}

private Employee cursorToEmployee(Cursor cursor) {
Employee employee = new Employee();
employee.setId(cursor.getLong(0));
employee.setEmpName(cursor.getString(1));
employee.setAge(cursor.getInt(2));
return employee;
}


} public class DisplayListActivity extends ListActivity {



private EmployeesDataSource datasource;
List<Employee> values ;
ArrayAdapter<Employee> adapter;
SQLiteDatabase db ;
MySQLiteHelper helper;
Employee d ;





public void onCreate (Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.displaylist);

datasource = new EmployeesDataSource(this);
datasource.open();
ListView lv = (ListView)findViewById(android.R.id.list);
values = datasource.getAllEmployees();
adapter = new ArrayAdapter<Employee>(this,
android.R.layout.simple_list_item_1, values);
lv.setAdapter(adapter);



lv.setOnItemLongClickListener(new OnItemLongClickListener() {

@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int Pos, long ID) {
// TODO Auto-generated method stub

datasource.deleteEmployee(ID+1); // to can match with my DB wich start with 0
values.remove(Pos);
adapter.notifyDataSetChanged();




return true;
}
});


}


}


public class MeriUmarKeNaujawanoActivity extends Activity {



private EmployeesDataSource datasource;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

datasource = new EmployeesDataSource(this);
datasource.open();
Button btnDone;
btnDone = (Button) findViewById(R.id.btnDone);
btnDone.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Employee employee = null;
EditText enterName = (EditText) findViewById(R.id.edittxtEnterName);
EditText enterAge = (EditText) findViewById(R.id.edittxtEnterAge);
int age = Integer.parseInt(enterAge.getText().toString());
employee = datasource.createEmployee(enterName.getText().toString(),age);
Intent intent = new Intent(MeriUmarKeNaujawanoActivity.this, DisplayListActivity.class);
startActivity(intent);
}
});`enter code here`
}


}


Aucun commentaire:

Enregistrer un commentaire