i have a database and 6 column in my table . i make database like this class :
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import com.example.persofessional.mybarbershops.OBJ.Obj_Customer;
import java.util.ArrayList;
public class DataBaseCustomerManager extends SQLiteOpenHelper {
public DataBaseCustomerManager(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, "MyBaberShopDB", null, 1);
}
SQLiteDatabase db;
public String Customer_TBL_Query;
@Override
public void onCreate(SQLiteDatabase db) {
Customer_TBL_Query = "CREATE TABLE tbl_Customer ( \n" +
" id INTEGER PRIMARY KEY\n" +
" NOT NULL\n" +
" UNIQUE\n" +
" DEFAULT ( 101 ),\n" +
" CustomerCode VARCHAR( 11 ),\n" +
" CustomerName VARCHAR( 30 ),\n" +
" CustomerAdress VARCHAR( 80 ),\n" +
" CustomerTell VARCHAR( 14 ),\n" +
" CustomerMoaref VARCHAR( 15 ) \n" +
");\n";
db.execSQL(Customer_TBL_Query);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS tbl_Customer");
onCreate(db);
}
public boolean add_Customer(Obj_Customer objCustomer) {
boolean result = false;
try {
String Add_Customer_Query = "INSERT INTO tbl_Customer (CustomerCode, CustomerName, CustomerAdress,CustomerTell,CustomerMoaref) \n" +
"VALUES (" + objCustomer.CustomerCode + ", '" + objCustomer.CustomerName + "', '" + objCustomer.CustomerAdress + "' ,'" + objCustomer.CustomerTell + "','" + objCustomer.CustomerMoaref + "');";
SQLiteDatabase db = this.getReadableDatabase();
db.execSQL(Add_Customer_Query);
db.close();
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public boolean Update_Customer(Obj_Customer objCustomer) {
boolean result = false;
try {
String Update_Customer_Query = "UPDATE tbl_Customer \n" +
"SET CustomerCode=" + objCustomer.CustomerCode + ",CustomerName='" + objCustomer.CustomerName + "',CustomerAdress=" +
"'" + objCustomer.CustomerAdress + "' ,CustomerTell='" + objCustomer.CustomerTell + "'," +
"CustomerMoaref='" + objCustomer.CustomerMoaref + "';" +
"WHERE CustomerTell='" + objCustomer.CustomerTell + "'";
SQLiteDatabase db = this.getReadableDatabase();
db.execSQL(Update_Customer_Query);
db.close();
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public Obj_Customer get_Code_Customer(String CustomerCode) {
Obj_Customer objCustomer = new Obj_Customer();
try {
String Search_ByCode_Customer_Query = "SELECT * FROM tbl_Customer WHERE CustomerCode = '" + CustomerCode + "';";
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(Search_ByCode_Customer_Query, null);
if (cursor.moveToFirst() == true) {
objCustomer.CustomerCode = cursor.getString(1);
objCustomer.CustomerName = cursor.getString(2);
objCustomer.CustomerAdress = cursor.getString(3);
objCustomer.CustomerTell = cursor.getString(4);
objCustomer.CustomerMoaref = cursor.getString(5);
}
} catch (Exception e) {
e.printStackTrace();
objCustomer = null;
}
return objCustomer;
}
public Obj_Customer get_Tell_Customer(String CustomerTell) {
Obj_Customer objCustomer = new Obj_Customer();
try {
String Search_ByTell_Customer_Query = "SELECT * FROM tbl_Customer WHERE CustomerTell = '" + CustomerTell + "';";
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(Search_ByTell_Customer_Query, null);
if (cursor.moveToFirst() == true) {
objCustomer.CustomerCode = cursor.getString(1);
objCustomer.CustomerName = cursor.getString(2);
objCustomer.CustomerAdress = cursor.getString(3);
objCustomer.CustomerTell = cursor.getString(4);
objCustomer.CustomerMoaref = cursor.getString(5);
}
} catch (Exception e) {
e.printStackTrace();
objCustomer = null;
}
return objCustomer;
}
public int getCustomerCount() {
int result;
String ShowAllValues = "SELECT * FROM tbl_Customer ;";
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(ShowAllValues, null);
result = cursor.getCount();
db.close();
return result;
}
public boolean Del_CustomerByCode(String CustomerCode) {
boolean result = false;
try {
String DeleteCustomerByCode = "DELETE FROM tbl_Customer WHERE CustomerCode='" + CustomerCode + "';";
SQLiteDatabase db = this.getReadableDatabase();
db.execSQL(DeleteCustomerByCode);
db.close();
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public boolean Del_CustomerByTell(String CustomerTell) {
boolean result = false;
try {
String DeleteCustomerByTell = "DELETE FROM tbl_Customer WHERE CustomerTell='" + CustomerTell + "';";
SQLiteDatabase db = this.getReadableDatabase();
db.execSQL(DeleteCustomerByTell);
db.close();
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
that's carefully add member and in my MainActivity to show all item in ListView Use this code :
public void RefreshDisplay() {
ArrayList<Obj_Customer> Array_objcustomers = new ArrayList<Obj_Customer>();
/* for (int i = 0; i <= 30; i++) {
Obj_Customer obj_customers = new Obj_Customer();
obj_customers.setCustomerName("Test Title ");
obj_customers.setCustomerTell("Test Desc");
Array_objcustomers.add(obj_customers);
} */
DataBaseCustomerManager db = new DataBaseCustomerManager(this, null, null, 1);
Array_objcustomers = db.getAllCustomerItem();
CustomerArrayeAdapter customerArrayeAdapter = new CustomerArrayeAdapter(getApplicationContext(), Array_objcustomers);
MyLastCustomerList = (ListView) findViewById(R.id.ListViewMain);
MyLastCustomerList.setAdapter(customerArrayeAdapter);
}
But Err !!! where is problem ?!!!
Aucun commentaire:
Enregistrer un commentaire