mercredi 1 avril 2015

Android clear listview linked to SQLite

I have the following activity populating listview from SQLite. When click on empty I delete table content correctly and refresh the CartActivity which appears empty. If I add new element in my cart then old elements appears again but only in the listview and (correctly) not in my db. If I close the application the listview update itself correctly.


Tried to give a look at similar question, tried with notify but nothing happens. Someone wrote about "you delete the data but not the entries" but I'm not able to.


How could I solve this issue?


Any help would be much appreciated. Thanks in advance.



public class CartActivity extends Activity {
ListView list;
Context context;
SessionManagement session;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cart);
Typeface font = Typeface.createFromAsset(this.getAssets(), "font/VarelaRound-Regular.ttf");
context = getApplicationContext();
session = new SessionManagement(context);
final CartHandler db = new CartHandler(this);
Log.d("Reading: ", "Reading all contacts..");
final List<CartRow> products = db.getAllProducts();

for (CartRow cn : products) {
String log = "Id: "+cn.getID()+" ,Name: " + cn.getName() + " ,Number: " + cn.getNumber() + " ,Pieces: " + cn.getPieces() + " ,Price: " + cn.getPrice() + " ,Tot: " + cn.getTotPrice();
Log.d("Nome: ", log);
}
if(products.isEmpty() ){
//Intent intent = new Intent(getApplicationContext(),MenuActivity.class);
//intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
//startActivity(intent);
Toast.makeText(this, "", Toast.LENGTH_LONG).show();
//finish();
} else {
final CartHandler mdb = new CartHandler(this);
getItemsFromDatabase(mdb);
final CustomCarterList adapter = new CustomCarterList(CartActivity.this);
list = (ListView) findViewById(R.id.cart);
list.setAdapter(adapter);
Button empty = (Button) findViewById(R.id.emptycart);
empty.setTypeface(font);
empty.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
db.deleteAll();
//mdb.deleteAll();
Intent intent = new Intent(CartActivity.this, CartActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
}
});
Button proceed = (Button) findViewById(R.id.proceed);
proceed.setTypeface(font);
proceed.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (!session.isLoggedIn()) {
Intent intent = new Intent(CartActivity.this, LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
} else {
Intent intent = new Intent(
getApplicationContext(),
CheckoutActivity.class
);
startActivity(intent);
}
}
});

}

}

public void getItemsFromDatabase(CartHandler mdb) {
Cursor cursor = null;
try{
SQLiteDatabase db =mdb.getReadableDatabase();
cursor=db.rawQuery("select * from products", null);
while (cursor.moveToNext()){
Log.e("Cart", cursor.getString(1)+":"+cursor.getString(2)+":"+cursor.getString(3)+":"+cursor.getString(4)+":"+cursor.getString(5));
CartRow.itemIdList.add(cursor.getString(0));
CartRow.itemNameList.add(cursor.getString(1));
CartRow.itemQuantityList.add(cursor.getString(2));
if (cursor.getString(3).equals("0")){
CartRow.itemPiecesList.add("Pieces: 1");}
else{
CartRow.itemPiecesList.add("Pieces: "+cursor.getString(3));}
CartRow.itemPriceList.add(cursor.getString(4) + ".00€");
CartRow.itemTotPriceList.add(cursor.getString(5)+".00€");
}
cursor.close();
}catch (SQLException e){
Log.e("DB Error", e.toString());
e.printStackTrace();
}
}

Aucun commentaire:

Enregistrer un commentaire