I am creating a finance Android app that will open up and ask the user to add an account. (This will always be static on the page.) On the Main activity, it will have an EditText box next to a Button ("Add Account"). When the Button is pressed, I want a new Object created and it will then be stored into an ArrayList. The List of accounts (as they are added) will then be looped below (with corresponding dynamic buttons to edit the account). This is my practice/unfinished code. It is very raw at this point!
String accountName = (Whatever is in EditText Box)
ArrayList<Accounts> accountList = new ArrayList<Accounts>();
int accountListSize = accountList.size();
(Button on Click) {
Account{accountName} = new Account(); // Not sure how to dynamically name
accountList.add({accountName}) // Not sure how to dynamically name
}
// iterate through finance loop
for(int i = 0; i < accountList .size(); i++)
{
// do stuff - Create Dynamicly Edit and Clear Buttons for each account
}
One of the big issues I am trying to overcome is how to name an Object Dynamically?
Am I over-thinking this process overall and making it harder than it should be? I am going to create a class to handle the account specifics. I eventually have to keep the data stored--so maybe should I scrap the Object orientated style and use SQLite? Shared-preferences?
Any code samples would be great, but I am mostly hoping to find the recommend method I should take.
I would recommend to create an Account object that takes the name in the constructor. For example:
public class Account {
private String name;
public Account( String name ) {
this.name = name;
}
// ... other account related methods here...
public String getName() {
return name;
}
}
Then in your code above:
List<Account> accountList = new ArrayList<Account>();
(Button on Click) {
Account anAccount = new Account( accountName ); // accountName taken from text box.
accountList.add( anAccount );
}
Then to loop through the account list:
for( Account account : accountList ) {
String name = account.getName();
// .. do whatever you need to for each account...
}
Once you have this list of Account objects, you can do anything you need to do with them, such as storing in SQLite DB for later, etc...
Hope this helps...
Related
I'm working on a project in which I want a feature, message us on WhatsApp and for that, I used WhatsApp API using intent.
in 'message us on WhatsApp' feature I'm using 2-3 different numbers and storing it in the list and using random() method to retrieve that number from the list, as I want whenever a user uses that feature he/she has to connect with a different number every time.
but now I want to change those numbers which are stored inside the list.
So how can I change those numbers without changing the actual code every time?
I know it can be achieved using firebase, but I don't have too much idea about firebase.
I can only upload simple text and images on firebase and retrieve them. but how can I upload all these numbers on firebase? and how I can change them every time within a minute
the code I did is like this
public class DashBoard extends AppCompatActivity implements View.OnClickListener {
public CardView wpicon;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dash_board);
Util.blackIconStatusBar(DashBoard.this, R.color.white);
wpicon = findViewById(R.id.cardview1);
wpicon.setOnClickListener(this);
}
#Override
public void onClick(View v) {
Intent i;
String num;
String text;
num = ";
text = "I'm interseted in your service";
ArrayList<String> list = new ArrayList<>();
list.add("+91");
list.add("+91");
Random random = new Random();
String rando = list.get(random.nextInt(list.size()));
switch (v.getId()) {
case R.id.cardview1:
Intent intent = new Intent(Intent.ACTION_VIEW);
i = intent.setData(Uri.parse("http://api.whatsapp.com/send?phone=" + rando + "&text=" + text));
startActivity(i);
break;
One way of doing this with Firebase is by using Firebase Remote Config. The idea is generally simple. The remote config represents a list of keys and values. You have to list your data from the Firebase console and publish it. It will be immediately available to your users.
I have a scenario, whereby my App-A needs to read the Data from Database of App-B.
1) Modify the App-B's DB
2) Remove some rows of Data & save it.
3) While App-A is getting killed, restore App-B's data.
Now my concern is what's best way to save App-B's DB data so that it can easily be restored ?
I recommend you getting data from App B first and storing it into an Array of ItemData.class you have created. (Do this using your code in app A.)
ItemData.class :
public class ItemData{
public int itemId;
public String itemText;
public boolean itemBoolean;
}
And you can add new Item to your array as below:
ArrayList<ItemData> items= new ArrayList<>();
ItemData item = new ItemData();
item.itemId = //you should assign id from app b database
item.itemText = //you should assign text from app b database
item.itemBoolean = //you should assign boolean from app b database
items.add(item);
Then you can access all the data from app be and modify them meanwhile they are available. This will be much faster and data will not get lost from your array.
Now with a for loop you can restore all data and save it on another database:
for (int i = 0; i < items.size(); i++) {
int restoredID = items.get(i).itemId;
String restoredString = items.get(i).itemText;
boolean restoredBoolean = items.get(i).itemBoolean;
}
I hope this helps.
This is my first question in stack overflow .
I am creating one project with spinner , When dropdown list is clicked it displays 25 states , If we click any one state it takes us to new activity with LISTVIEW of hotels in that city , If we click any hotel it shows the details .
For this I am creating 2to3 activity , IF my project is small its ok , but i am adding 25 States and each states consists of 10-15 Hotels . It takes too much of time to create it ..
I use New --> Other --> Android --> Android Activity ,
My question is :- Can I create activity with contents (Image , ImageButton & details) using Java code ?
Do not create so many activities for each city and then for each hotel.
You can create only one activity to show city hotels just by passing that city name in EXTRAS in "intent" and show hotels of that city only in new activity.
To send name of city in intent use
Intent intent = new Intent(MAIN_ACTIVITY.this, HOTELS_ACTIVITY.class);
intent.putExtra("city", city_name);
startActivity(intent);
After that in new activity use
String passedCity = getIntent().getExtras().getString("city");
Now you have city name and you can retrieve hotels name of that city accordingly.
In this way you don't have to create so many activities.
Similarly you can use this to show hotel details by passing just its name and retrieving it in next activity.
Good Luck
You should be reusing activities rather than building from scratch each time.
Your data model could possibly look like this:
public class State
{
private String name;
private ArrayList<Hotel> hotels;
public State(String name, ArrayList<Hotel> hotels)
{
this.name = name;
this.hotels = hotels;
}
public ArrayList<Hotel> getHotels()
{
return this.hotels;
}
public String getName()
{
return this.name;
}
}
public class Hotel
{
private String name;
// Add in your other variables here
// Examples: Location, Id, Address
public Hotel(String name)
{
this.name = name;
}
}
Build your data however you please, but you should use objects similar to these to pass through to your 2/3 activities and build the user interface dynamically.
For this example I have an array:
String[] books = new String[x];
I would like to store the id and title in each location:
books[0]=>id:0, title:"book title1"
books[0]=>id:1, title:"book title2"
books[0]=>id:2, title:"book title3"
books[0]=>id:3, title:"book title4"
I want to store the id since it may change. I'm getting the id and title from a database. Getting the info isn't the issue. I want to store it this way so in my other functions this returns to I can use something like:
btn.setText(regions[i].title)
Any suggestion on how to handle this would be great.
Do one thing, first create a bean class like BookBean.
Under this declare two variables ID and Title. and declare getters and setters (If u are using eclipse u can easily do this by (Source -> generate getters and setters.. option)
and then declare a ArrayList to store BookBean vale as of follow.
ArrayList<BookBean> bookArrayList=new ArrayList<BookBean>();
for(int i=0;i<=urSize;i++)
{
// create a object for BookBean
BookBean book =new BookBean();
book.setID("what ever");
book.setTitle("what ever");
bookArrayList.ass(book)
}
It is better to use Arraylist with custom class.
see this
class Book
{
String id,title;
/* Cunstructor to store data */
public Book(String id,String title)
{
this.id = id;
this.title = title;
}
}
//declare arraylist
ArrayList<Book> bookList = new ArrayList<Book>();
bookList.add("1","book1");
bookList.add("2","book2");
bookList.add("3","book3");
bookList.add("4","book4");
btn.setText(bookList.get(i).title)
I think you have several options
Use a HashMap where you can use your id as key and value title
Define a class and keep id and title as attributes , define get and set methods.
Keep the objects of the class in a ArrayList
I am working in a translator kind of app and i need some help.
I have a class with getters and setters for my Array List objects. Each object has a phrase, a meaning, and usage.
so i have this to create my list:
ArrayList<PhraseCollection> IdiomsList = new ArrayList<PhraseCollection>();
now how do i add these objects to the list, each object containing the phrase, its meaning, and a use in a sentence?
For Example: The Layout would be something like this
Phrase
Kick the bucket
Meaning
When someone dies
Usage
My grandfather kicked the bucket
Thanks a lot
this is what i came up with that worked for me
private void loadIdioms() {
//creating new items in the list
Idiom i1 = new Idiom();
i1.setPhrase("Kick the bucket");
i1.setMeaning("When someone dies");
i1.setUsage("My old dog kicked the bucket");
idiomsList.add(i1);
}
ArrayList has a method call add() or add(ELEMENT,INDEX);
In order to add your objects you must first create them
PhraseCollection collection=new PhraseCollection();
then create the ArrayList by
ArrayList<PhraseCollection> list=new ArrayList<PhraseCollection>();
add them by :
list.add(collection);
Last if you want to render that in your ListView item, you must override the toString() in your PhraseCollection.
I suppose you would use the add(E) method (http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html#add(E)).
Here is an example using your example provided.
public class Phrase {
public final String phrase, meaning, usage;
//TODO: implement getters?
public Phrase(String phrase, meaning, usage) {
this.phrase = phrase;
this.meaning = meaning;
this.usage = usage;
}
}
and use it like this:
// create the list
ArrayList<Phrase> idiomsList = new ArrayList<Phrase>();
// create the phrase to add
Phrase kick = new Phrase("kick the bucket", "When someone dies", "My grandfather kicked the bucket");
// add the phrase to the list
idiomsList.add(kick);