Bloc counter for shopping cart - dart

I am trying to build a shopping cart using the bloc pattern as this my first app in flutter as well as using bloc. my problem is that I am trying to get the stream of an int each time the user add the product to the cart. but it seems that I am using the sink and stream wrong but I don't know exactly where
ItemCounterBloc
final _itemCounterSubject = BehaviorSubject<int>(seedValue: 0);
final _cartItemsController = StreamController<List<CartItem>>();
int count = 0;
ItemCounterBloc(Item item){
_cartItemsController.stream
.map((list) => list.any((cartItem)=> cartItem.item == item))
.listen((increment){
count += 1;
_itemCounterSubject.add(count);
});
}
Sink<List<CartItem>> get cartItems => _cartItemsController.sink;
ValueObservable<int> get isInCart => _itemCounterSubject.stream.distinct().shareValue(seedValue: 0);
void dispose(){
_cartItemsController.close();
_itemCounterSubject.close();
}
}
Counter
StreamBuilder<int>(
stream: _bloc.isInCart,
initialData:0,
builder: (context, snapshot) => Text('${snapshot.data}')
Also I have another bloc for adding items to the cart.

There is a full example on how to build a shopping cart system.
Including the following parts :
Adding / Removing items from the cart
AppBar counter with the amount of items in the cart
Shopping Cart BLOC
https://github.com/Ephenodrom/FlutterAdvancedExamples/tree/master/lib/examples/shoppingCart
This is how your BLOC could look :
class ShoppingCartBloc implements BlocBase {
static const String TAG = "ShoppingCartBloc";
ShoppingCart cart = ShoppingCart();
/// Sinks
Sink<Product> get addition => itemAdditionController.sink;
final itemAdditionController = StreamController<Product>();
Sink<Product> get substraction => itemSubtractionController.sink;
final itemSubtractionController = StreamController<Product>();
/// Streams
Stream<ShoppingCart> get cartStream => _cart.stream;
final _cart = BehaviorSubject<ShoppingCart>();
ShoppingCartBloc() {
itemAdditionController.stream.listen(handleItemAdd);
itemSubtractionController.stream.listen(handleItemRem);
}
///
/// Logic for product added to shopping cart.
///
void handleItemAdd(Product item) {
Logger(TAG).info("Add product to the shopping cart");
cart.addProduct(item);
cart.calculate();
_cart.add(cart);
return;
}
///
/// Logic for product removed from shopping cart.
///
void handleItemRem(Product item) {
Logger(TAG).info("Remove product from the shopping cart");
cart.remProduct(item);
cart.calculate();
_cart.add(cart);
return;
}
///
/// Clears the shopping cart
///
void clearCart() {
cart.clear();
}
#override
void dispose() {
itemAdditionController.close();
itemSubtractionController.close();
}
}
class ShoppingCart {
List<Product> products = [];
double priceNet;
double priceGross;
double vatAmount;
void addProduct(Product p) {
products.add(p);
}
void remProduct(Product p) {
products.remove(p);
}
void calculate() {
priceNet = 0;
priceGross = 0;
vatAmount = 0;
products.forEach((p) {
priceNet += p.priceNet;
priceGross += p.priceGross;
vatAmount += p.vatAmount;
});
}
void clear() {
products = [];
priceNet = 0;
priceGross = 0;
vatAmount = 0;
}
}
class Product {
final String name;
final double priceNet;
final double priceGross;
final double vatAmount;
final double tax;
Product(
{this.name, this.priceNet, this.priceGross, this.vatAmount, this.tax});
}

Related

Update custom listviewAdapter

I wanne add new data to my listviewAdapter, i tried it with clearing all the data before it and notifying that there is now data but it doesn't seem to work. I followed the Solution (stackoverflow) but it doesn't work.
Does anyone have an idea why?
public class ReportsListViewAdapter : BaseAdapter<IMobileReport>, IFilterable
{
internal List<IMobileReport> originalData;
internal List<IMobileReport> reports;
private Context context;
public override IMobileReport this[int position] => reports[position];
public ReportsListViewAdapter(Context context, IEnumerable<IMobileReport> reports)
{
this.reports = reports.OrderBy(report => report.StudyDate).ToList();
this.context = context;
Filter = new ReportsFilter(this);
}
public override int Count => this.reports.Count;
public Filter Filter { get; private set; }
public override long GetItemId(int position)
{
return position;
}
public void updateReportsList(List<MobileReport> newlist)
{
reports.AddRange(newlist);
this.NotifyDataSetChanged();
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
View row = convertView;
if(row == null)
{
row = LayoutInflater.From(context).Inflate(Resource.Layout.listView_reports_row, null, false);
}
var txtName = row.FindViewById<TextView>(Resource.Id.txtName);
txtName.Text = reports[position].Student.Name;
var txtFirstName = row.FindViewById<TextView>(Resource.Id.txtFirstName);
txtFirstName.Text = reports[position].Student.FirstName;
var txtSource = row.FindViewById<TextView>(Resource.Id.txtSource);
txtSource.Text = reports[position].Source;
var txtCritical = row.FindViewById<TextView>(Resource.Id.txtCritical);
txtSource.Text = reports[position].Critical.ToString();
return row;
}
}
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
reports = new List<IMobileReport>();
//Init();
_reportsHubConnector = new ReportsHubConnector();
#pragma warning disable 4014 // We cannot await this task here because the signature of the inherited method is void
Task.Factory.StartNew(async () =>
{
await _reportsHubConnector.StartConnection();
await _reportsHubConnector.SendT();
}, TaskCreationOptions.PreferFairness);
#pragma warning restore 4014
Console.WriteLine("HomeActivity: OnCreate");
SetContentView(Resource.Layout.activity_reports);
SupportActionBar.SetDisplayShowTitleEnabled(false);
SupportActionBar.SetDisplayHomeAsUpEnabled(false);
SupportActionBar.SetDisplayShowHomeEnabled(true);
WireUpElements();
listView = FindViewById<ListView>(Resource.Id.reports);
ReportsListViewAdapter adapter = new ReportsListViewAdapter(this, reports);
listView.Adapter = adapter;
searchView = FindViewById<SearchView>(Resource.Id.searchView1);
searchView.QueryTextChange += this.Filter;
listView.ItemClick += ItemClicked;
criticalButton = FindViewById<LinearLayout>(Resource.Id.AuthenticatorButton);
criticalButton.Click += criticalClicked;
_reportsHubConnector.ReportsRecieved += (reports) =>
{
adapter.updateReportsList(reports);
};
}
When i debug slowly the GetView does get triggered, maybe this is a clue to why its not being called when i don't debug or go over the code quick.
This.RunOnUiThread is never called but update is.
_reportsHubConnector.ReportsRecieved += (tmpReports) =>
{
adapter.updateReportsList(tmpReports);
this.RunOnUiThread(() =>
{
criticalButton.SetBackgroundColor(Android.Graphics.Color.Red);
});
};
i tried it with clearing all the data before it and notifying that there is now data but it doesn't seem to work.
From shared code , not seeing clear method , you can add reports.Clear() to check whether it works .
public void updateReportsList(List<MobileReport> newlist)
{
reports.Clear();
reports.AddRange(newlist);
this.NotifyDataSetChanged();
}
If not works , need to check whehter added newlist is the correct data format.
========================Update========================
In OnCreate method , where _reportsHubConnector.ReportsRecieved call update method modify as follow :
_reportsHubConnector.ReportsRecieved += (tmpReports) =>
{
adapter.updateReportsList(tmpReports);
};
Change reports argument to tmpReports to avoid mixing with the original data reports .
Therefore , there is another common way to update data of adapter as follow :
_reportsHubConnector.ReportsRecieved += (reports) =>
{
List<IMobileReport> tmp = new List<IMobileReport>();
tmp = reports ; // use a tmp list data for updating , not using original list data
adapter.updateReportsList(tmp);
};
============================Update===============================
From my sample project , I find a interesting phenomenon and that maybe the problem.
Here I will share my custom adapter HomeScreenAdapter :
public class HomeScreenAdapter : BaseAdapter<TableItem> {
List<TableItem> items;
Activity context;
public HomeScreenAdapter(Activity context, List<TableItem> items)
: base()
{
this.context = context;
this.items = new List<TableItem>();
this.items.AddRange(items);
//this.items = items;
}
public override long GetItemId(int position)
{
return position;
}
public override TableItem this[int position]
{
get { return items[position]; }
}
public override int Count
{
get { return items.Count; }
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
var item = items[position];
View view = convertView;
if (view == null) // no view to re-use, create new
view = context.LayoutInflater.Inflate(Resource.Layout.CustomView, null);
view.FindViewById<TextView>(Resource.Id.Text1).Text = item.Heading;
view.FindViewById<TextView>(Resource.Id.Text2).Text = item.SubHeading+" items";
view.FindViewById<ImageView>(Resource.Id.Image).SetImageResource(item.ImageResourceId);
return view;
}
public void UpdateListView(List<TableItem> newTableItem)
{
items.Clear();
items.AddRange(newTableItem);
NotifyDataSetChanged();
}
}
You will see in the Constructor of HomeScreenAdapter , I commented this line of code this.items = items; . The reault that it will work:
However , if I use this.items = items; to replace this.items.AddRange(items);, it will update nothing even can not show anything in ListView .
public HomeScreenAdapter(Activity context, List<TableItem> items)
: base()
{
this.context = context;
this.items = new List<TableItem>();
//this.items.AddRange(items);
this.items = items;
}
The effect :
The possible reason :
If the equal sign is used here, the pointer address of the items will change. When the data is updated, it cannot point to the original data source, so the update cannot be successful.
Therefore , here Constructor of your code can modify as follow to check whehter it works :
public ReportsListViewAdapter(Context context, IEnumerable<IMobileReport> reports)
{
//this.reports = reports.OrderBy(report => report.StudyDate).ToList();
this.reports = new List<IMobileReport>();
this.reports.AddRange(reports.OrderBy(report => report.StudyDate).ToList());
this.context = context;
Filter = new ReportsFilter(this);
}
Related : Here is the sample project link .

how can i retrieve different child firebase database in single activity

I am trying to retrieve data from Firebase Database and set them to text view. My database has different multiple child data. I want to retrieve all child in a single activity like globally. I have a different card view click in the main activity. when I click on any item it does not show the same child data with related that item. It shows single child data on every item click. How can I get them in a signal Activity on card view click? I am new to Android and I have tried multiple answers here, but failed. Can anyone help me with this?
public class GlobalActivity extends AppCompatActivity
RecyclerView mRecyclerView;
List<User> myUserList;
User mUser;
ProgressDialog progressDialog;
private DatabaseReference db;
private ValueEventListener eventListener;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_global);
progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Please wait its loding..");
mRecyclerView = (RecyclerView) findViewById(R.id.farazList);
GridLayoutManager gridLayoutManager = new GridLayoutManager(AhmedFarazActivity.this, 1);
mRecyclerView.setLayoutManager(gridLayoutManager);
myUserList = new ArrayList<>();
final ReAdapter reAdapter = new ReAdapter(GlobalActivity.this, myUserList);
mRecyclerView.setAdapter(reAdapter);
db = FirebaseDatabase.getInstance().getReference().child("Spinner").child("US");
progressDialog.show();
eventListener = db.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
myUserList.clear();
for (DataSnapshot ds : dataSnapshot.getChildren()) {
User userData = ds.getValue(User.class);
myUserList.add(userData);
}
reAdapter.notifyDataSetChanged();
progressDialog.dismiss();
}
#Override
public void onCancelled(DatabaseError databaseError) {
progressDialog.dismiss();
}
});
}
}
Handle to click
This is handler in the adapter when I click on any item it does not show the same child data with related that item. It shows single child data on every item click.
#Override
public void onBindViewHolder(#NonNull MyHolder holder, int position) {
// Hide data
holder.nameText.setText(models.get(position).getName());
holder.img.setImageResource(models.get(position).getImg());
holder.setItemClickListener(new ItemClickListener() {
#Override
public void onItemClick(View view, int pos) {
//Go to UK Item to show UK data
if (models.get(pos).getName().equals("US")){
//Start Display Activity on click
Toast.makeText(c, "US", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(c, GlobalActivity.class);
c.startActivity(intent);
}
//Go to UK Item to show UK data
if (models.get(pos).getName().equals("UK")){
//Start System Info Activity on click
Toast.makeText(c, "UK", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(c, GlobalActivity.class);
c.startActivity(intent);
}
}
});
}
Now i figure out my solution
I add just Intent.putExtra("data","1"); in every handler click
#Override
public void onBindViewHolder(#NonNull MyHolder holder, int position) {
// Hide data
holder.nameText.setText(models.get(position).getName());
holder.img.setImageResource(models.get(position).getImg());
holder.setItemClickListener(new ItemClickListener() {
#Override
public void onItemClick(View view, int pos) {
//Go to UK Item to show UK data
if (models.get(pos).getName().equals("US")){
//Start Display Activity on click
Toast.makeText(c, "US", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(c, GlobalActivity.class);
intent.putExtra("data","1");
c.startActivity(intent);
}
//Go to UK Item to show UK data
if (models.get(pos).getName().equals("UK")){
//Start System Info Activity on click
Toast.makeText(c, "UK", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(c, GlobalActivity.class);
intent.putExtra("data","2");
c.startActivity(intent);
}
}
});
}
In GlobalActivity i create a method for data
initialize data(); inside onCreate
public class GlobalActivity extends AppCompatActivity
RecyclerView mRecyclerView;
List<User> myUserList;
User mUser;
ProgressDialog progressDialog;
private DatabaseReference db;
private ValueEventListener eventListener;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_global);
progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Please wait its loding..");
mRecyclerView = (RecyclerView) findViewById(R.id.farazList);
GridLayoutManager gridLayoutManager = new GridLayoutManager(AhmedFarazActivity.this, 1);
mRecyclerView.setLayoutManager(gridLayoutManager);
myUserList = new ArrayList<>();
final ReAdapter reAdapter = new ReAdapter(GlobalActivity.this, myUserList);
mRecyclerView.setAdapter(reAdapter);
data();
progressDialog.show();
eventListener = db.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
myUserList.clear();
for (DataSnapshot ds : dataSnapshot.getChildren()) {
User userData = ds.getValue(User.class);
myUserList.add(userData);
}
reAdapter.notifyDataSetChanged();
progressDialog.dismiss();
}
#Override
public void onCancelled(DatabaseError databaseError) {
progressDialog.dismiss();
}
});
}
method for getting
public void data() {
String data = getIntent().getStringExtra("data");
if (data.equals("1")) {
db = FirebaseDatabase.getInstance().getReference().child("Spinner").child("India");
db.keepSynced(true);
} else if (data.equals("2")) {
db = FirebaseDatabase.getInstance().getReference().child("Spinner").child("Pak");
db.keepSynced(true);
} else if (data.equals("3")) {
db = FirebaseDatabase.getInstance().getReference().child("Spinner").child("UK");
db.keepSynced(true);
}
}

Raven paging queries in a specific way

I'm developing an ASP.NET MVC application using RavenDB 3. I don't have a lot of experience with raven.
In general, when executing queries to display data, the first 128 items are returned on the page. More records are added in an "infinite scroll"-manner by using paged queries.
Now, however, I have the requirement that items are loaded in 'groups'.
Assume the following class:
public class Item {
public string Id { get; set; }
public int Group { get; set; }
public string text { get; set; }
}
Assume the database contains 40 items having group='1', 40 items having group='2' and 50 items having group='3'.
This is a total of 130 items. This would mean that the last 'group' fetched would not be complete. It would be missing 2 items.
I would like a mechanism that is aware of this, so that it would fetch at least 128 AND would fetch 'extra' if the last group is not completely included.
Afterwards, when I fetch the next page, I would like it to start with the next group.
Is there any way I can make this work without 'fabricating' a single page myself by doing more than one call?
EDIT: I cannot assume the groups are perfectly equal in size, but I can assume the sizes will be 'simular'
Also, I cannot change the design to store all items in a single 'group'-object for instance.
Okay so basically what you will need to do is calculate the number of results that were in the previous pages and the number of results that are in the current page. Below is a quick example app to give you an idea of how to do it. One caveat, if the number of results for the current group range exceeds the MaxNumberOfRequestsPerSession than an error will be thrown, so you might want to put some handling in there for that.
Note for running this example:
Make sure your platform is set to x64 in visual studio if you are using the most recent versions of RavenDB. Otherwise, this example will thrown an error about Voron not being stable in 32 bit mode.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Raven.Client;
using Raven.Client.Embedded;
using Raven.Client.Listeners;
namespace ConsoleApplication
{
internal class Program
{
private static void Main(string[] args)
{
using (var gdm = new GroupDataManager())
{
Console.WriteLine("Started Seeding");
gdm.Seed().Wait();
Console.WriteLine("Finished Seeding");
Console.WriteLine("===============================================================");
Console.WriteLine("Get First Page");
Console.WriteLine("===============================================================");
var firstPage = gdm.GetPagedGroupResults(1, 3).Result;
foreach (var item in firstPage)
{
Console.WriteLine(item.Text);
}
Console.WriteLine("===============================================================");
Console.WriteLine("Get Second Page");
Console.WriteLine("===============================================================");
var secondPage = gdm.GetPagedGroupResults(2, 3).Result;
foreach (var item in secondPage)
{
Console.WriteLine(item.Text);
}
}
Console.ReadLine();
}
}
public class GroupDataManager : IDisposable
{
private readonly IDocumentStore _documentStore = new EmbeddedRavenDatabase().Store;
public void Dispose()
{
_documentStore.Dispose();
}
public async Task Seed()
{
var rnd = new Random();
using (var session = _documentStore.OpenAsyncSession())
{
for (var groupNumber = 1; groupNumber < 15; groupNumber++)
{
for (var i = 0; i < rnd.Next(5, 25); i++)
{
var item = new Item
{
Group = groupNumber,
Text = string.Format("Group: {0} Item:{1}", groupNumber, i)
};
await session.StoreAsync(item);
}
}
await session.SaveChangesAsync();
}
}
public async Task<IList<Item>> GetPagedGroupResults(int page, int numberOfGroupsPerPage)
{
var startingGroup = ((page - 1) * numberOfGroupsPerPage) + 1;
using (var session = _documentStore.OpenAsyncSession())
{
// Calculate the number of items that were contained in the previous groups
var numberToSkip = await session.Query<Item>().CountAsync(item => item.Group < startingGroup);
var endGroup = startingGroup + numberOfGroupsPerPage;
// Calculate the number of items that are in the current range of groups
var numberToTake = await session.Query<Item>().CountAsync(item => item.Group >= startingGroup && item.Group < endGroup);
var results = await session.Query<Item>().Skip(numberToSkip).Take(numberToTake).ToListAsync();
return results;
}
}
}
public class Item
{
public string Id { get; set; }
public int Group { get; set; }
public string Text { get; set; }
}
/// <summary>
/// For Testing Only. Prevents stale queries
/// </summary>
public class NoStaleQueriesAllowed : IDocumentQueryListener
{
public void BeforeQueryExecuted(IDocumentQueryCustomization queryCustomization)
{
queryCustomization.WaitForNonStaleResults();
}
}
public class EmbeddedRavenDatabase
{
private static bool _configured = false;
private static readonly Lazy<IDocumentStore> _lazyDocStore = new Lazy<IDocumentStore>(() =>
{
var docStore = new EmbeddableDocumentStore
{
RunInMemory = true
};
docStore.RegisterListener(new NoStaleQueriesAllowed());
docStore.Initialize();
return docStore;
});
public IDocumentStore Store
{
get { return _lazyDocStore.Value; }
}
}
}

Blackberry: saving ListField content and dirty state management

I have prepared a simple test case to demonstrate my problem.
It is just 1 file which will run instantly when added to a new project.
I would like to have a MainScreen displaying an editable list of items:
and when leaving this screen, the user should be asked - if she wants to save the modified list to persistent storage, by presenting the standard Save/Discard/Cancel-dialog:
I have added setDirty(true) to my menu items and the standard dialog does come up okay.
My problem is: I don't know how to clear the dirty flag after saving - in my current code the Save/Discard/Cancel-dialog comes again and again, even if I just view the ListField, without editing it.
src\mypackage\MyList.java:
package mypackage;
import java.util.*;
import net.rim.device.api.collection.*;
import net.rim.device.api.collection.util.*;
import net.rim.device.api.system.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.ui.decor.*;
import net.rim.device.api.util.*;
public class MyList extends UiApplication implements FieldChangeListener {
MyScreen myScreen = new MyScreen();
public static void main(String args[]) {
MyList app = new MyList();
app.enterEventDispatcher();
}
public MyList() {
MainScreen titleScreen = new MainScreen();
titleScreen.setTitle("Click the button:");
ButtonField myButton = new ButtonField("Show the list", ButtonField.CONSUME_CLICK) ;
myButton.setChangeListener(this);
titleScreen.add(myButton);
pushScreen(titleScreen);
}
public void fieldChanged(Field field, int context) {
pushScreen(myScreen);
}
}
class MyScreen extends MainScreen {
ObjectListField myList = new ObjectListField();
static PersistentObject myStore;
static Vector myData;
static {
myStore = PersistentStore.getPersistentObject(0xb77f8e453754f37aL);
myData = (Vector) myStore.getContents();
if (myData == null) {
myData = new Vector();
myData.addElement("String 1");
myData.addElement("String 2");
myData.addElement("String 3");
myStore.setContents(myData);
}
}
public MyScreen() {
setTitle("Edit the list below:");
add(myList);
addMenuItem(addItem);
addMenuItem(editItem);
addMenuItem(removeItem);
}
// load data from persistent store into the ListField
private void loadData() {
// clear the ListField
myList.setSize(0);
// copy data from the Vector to the ListField
for (int i = myData.size() - 1; i >= 0; i--)
myList.insert(0, myData.elementAt(i));
}
// save data from the ListField into the persistent store
private void saveData() {
// clear the Vector
myData.removeAllElements();
// copy data from the ListField to the Vector
for (int i = myList.getSize() - 1; i >=0; i--)
myData.addElement(myList.get(myList, i));
synchronized(PersistentStore.getSynchObject()) {
myStore.commit();
}
}
protected void onUiEngineAttached(boolean attached) {
if (attached) {
loadData();
}
}
public void save() {
saveData();
// UPDATE: when I call setDirty(false); here, then
// the app starts displaying Save/Discard/Cancel dialog
// on its exit - so there must be a better way...
}
private final MenuItem addItem = new MenuItem("Add Item", 0, 0) {
public void run() {
String[] buttons = {"Add", "Cancel"};
Dialog myDialog = new Dialog("Add Item", buttons, null, 0, null);
EditField myEdit = new EditField("Item: ", "");
myDialog.add(myEdit);
if (myDialog.doModal() == 0) {
myList.insert(0, myEdit.getText());
setDirty(true);
}
}
};
private final MenuItem editItem = new MenuItem("Edit Item", 0, 0) {
public void run() {
String[] buttons = {"Save", "Cancel"};
Dialog myDialog = new Dialog("Edit Item", buttons, null, 0, null);
int index = myList.getSelectedIndex();
if (index == -1) {
return;
}
String selectedItem = (String) myList.get(myList, index);
EditField myEdit = new EditField("Item: ", selectedItem);
myDialog.add(myEdit);
if (myDialog.doModal() == 0) {
myList.set(index, myEdit.getText());
setDirty(true);
}
}
};
private final MenuItem removeItem = new MenuItem("Remove Item", 0, 0) {
public void run() {
String[] buttons = {"Delete", "Cancel"};
Dialog myDialog = new Dialog("Remove Item", buttons, null, 0, null);
int index = myList.getSelectedIndex();
if (index == -1) {
return;
}
String selectedItem = (String) myList.get(myList, index);
LabelField myLabel = new LabelField("Really delete " + selectedItem + "?");
myDialog.add(myLabel);
if (myDialog.doModal() == 0) {
myList.delete(index);
setDirty(true);
}
}
};
}
Please share your Blackberry 6 experience, advices in regard to persistent storage are also welcome.
In my real program I'm using KeywordFilterField for viewing a SortedReadableList, so from reading Blackberry docs I suppose, that I must always copy data between SortedReadableList and Vector - because the latter is persistable and the former is not?
setDirty(false) will clear the dirty flag if that is what you are after.

BlackBerry Persistent store

I am developing an application in which I want to store many data entries using persistent store. the problem is whenever new entry is made it replaces the existing entry.
here is my code please help me.
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.system.*;
import net.rim.device.api.util.*;
import java.util.*;
/*An application in which user enters the data. this data is displayed when user press the save button*/
public class Display extends UiApplication {
/*declaring Strings to store the data of the user*/
String getFirstName;
String getLastName;
String getEmail;
String getGender;
String getStatus;
/*declaring text fields for user input*/
private AutoTextEditField firstName;
private AutoTextEditField lastName;
private EmailAddressEditField email;
/*declaring choice field for user input*/
private ObjectChoiceField gender;
/*declaring check box field for user input*/
private CheckboxField status;
//Declaring button fields
private ButtonField save;
private ButtonField close;
/*declaring vector*/
private static Vector _data;
/*declaring persistent object*/
private static PersistentObject store;
/*creating an entry point*/
public static void main(String[] args)
{
/*creating instance of the class */
Display app = new Display();
app.enterEventDispatcher();
}
/*creating default constructor*/
public Display()
{
/*Creating an object of the main screen class to use its functionalities*/
MainScreen mainScreen = new MainScreen();
//setting title of the main screen
mainScreen.setTitle(new LabelField("Enter Your Data"));
//creating text fields for user input
firstName = new AutoTextEditField("First Name: ", "");
lastName= new AutoTextEditField("Last Name: ", "");
email= new EmailAddressEditField("Email:: ", "");
//creating choice field for user input
String [] items = {"Male","Female"};
gender= new ObjectChoiceField("Gender",items);
//creating Check box field
status = new CheckboxField("Active",true);
//creating Button fields and adding functionality using listeners
save = new ButtonField("Save",ButtonField.CONSUME_CLICK);
save.setChangeListener(new FieldChangeListener()
{
public void fieldChanged(Field field, int context)
{
save();
}
});
close = new ButtonField("Close",ButtonField.CONSUME_CLICK);
close.setChangeListener(new FieldChangeListener()
{
public void fieldChanged(Field field, int context)
{
onClose();
}
});
//adding the input fields to the main screen
mainScreen.add(firstName);
mainScreen.add(lastName);
mainScreen.add(email);
mainScreen.add(gender);
mainScreen.add(status);
//adding buttons to the main screen
mainScreen.add(close);
mainScreen.add(save);
//adding menu items
mainScreen.addMenuItem(saveItem);
mainScreen.addMenuItem(getItem);
//pushing the main screen
pushScreen(mainScreen);
}
//adding functionality to menu item "saveItem"
private MenuItem saveItem = new MenuItem("Save", 110, 10)
{
public void run()
{
//Calling save method
save();
}
};
//adding functionality to menu item "saveItem"
private MenuItem getItem = new MenuItem("Get", 110, 11)
{
//running thread for this menu item
public void run()
{
//synchronizing thread
synchronized (store)
{
//getting contents of the persistent object
_data = (Vector) store.getContents();
//checking for empty object
if (!_data.isEmpty())
{
//if not empty
//create a new object of Store Info class
StoreInfo info = (StoreInfo)
//returning last component of the vector
_data.lastElement();
//storing information retrieved in strings
getFirstName = (info.getElement(StoreInfo.NAME));
getLastName = (info.getElement(StoreInfo.LastNAME));
getEmail = (info.getElement(StoreInfo.EMail));
getGender = (info.getElement(StoreInfo.GenDer));
getStatus = (info.getElement(StoreInfo.setStatus));
//calling the show method
show();
}
}
}
};
//coding for persistent store
static {
store =
PersistentStore.getPersistentObject(0xdec6a67096f833cL);
synchronized (store) {
if (store.getContents() == null) {
store.setContents(new Vector());
store.commit();
}
}
_data = new Vector();
_data = (Vector) store.getContents();
}
//new class store info implementing persistable
private static final class StoreInfo implements Persistable
{
//declaring variables
private Vector _elements;
public static final int NAME = 0;
public static final int LastNAME = 1;
public static final int EMail= 2;
public static final int GenDer = 3;
public static final int setStatus = 4;
public StoreInfo()
{
_elements = new Vector(5);
for (int i = 0; i < _elements.capacity(); ++i)
{
_elements.addElement(new String(""));
}
}
public String getElement(int id)
{
return (String) _elements.elementAt(id);
}
public void setElement(int id, String value)
{
_elements.setElementAt(value, id);
}
}
//details for show method
public void show()
{
Dialog.alert("Name is "+getFirstName+" "+getLastName+"\nGender is "+getGender+"\nE-mail: "+getEmail+"\nStatus is "+getStatus);
}
//creating save method
public void save()
{
//creating an object of inner class StoreInfo
StoreInfo info = new StoreInfo();
//getting the test entered in the input fields
info.setElement(StoreInfo.NAME, firstName.getText());
info.setElement(StoreInfo.LastNAME,lastName.getText());
info.setElement(StoreInfo.EMail, email.getText());
info.setElement(StoreInfo.GenDer,gender.toString());
if(status.getChecked())
info.setElement(StoreInfo.setStatus, "Active");
else
info.setElement(StoreInfo.setStatus, "In Active");
//adding the object to the end of the vector
_data.addElement(info);
//synchronizing the thread
synchronized (store)
{
store.setContents(_data);
store.commit();
}
//resetting the input fields
Dialog.inform("Success!");
firstName.setText(null);
lastName.setText(null);
email.setText("");
gender.setSelectedIndex("Male");
status.setChecked(true);
}
//overriding onClose method
public boolean onClose()
{
System.exit(0);
return true;
}
}
Actually it's saves everything perfectly, but retrieving only the last one record:
StoreInfo info = (StoreInfo)_data.lastElement();
Try this to display every record:
for (int i = 0; i < _data.size(); i++) {
StoreInfo info = (StoreInfo)_data.elementAt(i);
...
show();
}

Resources