Storing and retrieving values from hashmap [closed] - android

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
i'm a beginner to Android development .... can u please tell me if this is this the correct way to declare an hashmap and add it to the arraylist?
Button createagendaButton = (Button) dialog.findViewById(R.id.button2);
createagendaButton.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
map.put("agendaTitle", edit_agendaTitle.getText().toString());
map.put("presenterName", edit_presenterName.getText().toString());
mylist.add(map);
}
});

It seems you are new to android
For starters why dont you go through samples in android sdk
sdk-path/samples/api-version/
and this link
To cheer you up try these that will solve your problems
1. get value from edittext edittext.getText();
2. store values in hashmap as hashmap.put(key,value)
3. instead of going for hashmaps, reconsider the type of data you are storing and try SparseArray if it suits your needs. Sparse arrays are well optimized and good at performance though they are very different in comparison
4. sqlite can store only some types of data. For sqlite on android try this
5. For ensuring proper functioning of your app across devices and resolutions, refer best practices and life cycles of different components used in your app. Also since you are using sqlite, prefer singleton pattern

The following code will allow you to store the values into the hashmap by using the key => value convention.
Map mMap = new HashMap();
mMap.put("FirstName", firstNameInput.getText().toString());
mMap.put("Surname", surnameInput.getText().toString());
mMap.put("Gender", genderInput.getText().toString());
Note, you will need to change the variable name before .getText() to that of your EditText variable names
http://developer.android.com/reference/java/util/HashMap.html might be of some use!

Related

How to insert multiple data in firebase using loop [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Is there any way to insert multiple data in firebase using loop in android studio.
As
for (DataSnapshot ds : dataSnapshot.getChildren())
{
String Name = ds.child("Name").getValue().toString();
list.add(Name);
}
DataSnapshot loop is used to get the multiple children. How can i insert multiple data using loop.
I am guessing you want to update various children of your database at once and for that you do not need to actually use a loop to insert multiple data at once in Firebase.
You may use maps for that, which can update multiple fields of your database in one go. The code for using maps, looks something like this:
Map<String,Object> taskMap = new HashMap<>();
taskMap.put("age", "12");
taskMap.put("gender", "male");
taskMap.put("name", "Someone");
taskMap.put("surname", "no-one");
reference.updateChildren(taskMap);
This depends a lot on your database structure, and you can edit it for your need, according to your database structure.
To know more about, how to update database using maps, go through the following links:
Link1
and
Link2.
Also, if you could make the question a bit more precise, you can get more help from here.

which code set is easier to read/maintain? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
Both of the codes below give me the same exact answers. I was just wondering which would be better programming practice for readability and maintainability. Would less lines of code be best? Would one affect the functionality of the program more than the other? any suggestions would be very much appreciated as I just want to learn the best practices for programming.
Here are the codes:
for (int i = 0; i < db.getAllDecks().size(); i++)
{
String CardCount = String.format("(%s)",db.getCardsForDeck(i+1).size());
adapter2.add(db.getAllDecks().get(i).getDeck_name());
adapter3.add(CardCount);
}
or
for (Deck deck: deckList) {
String deckName = deck.getDeck_name();
adapter2.add(deckName);
int count = db.getCardIds(deck).length;
String strCount = Integer.toString(count);
adapter3.add(strCount);
}
Overall, I think the second code is clearer, and more readable.
It contains moe variable names that is able to tell what exactly it is used for, such as deckName, count and strCount. I can clearly see that you are getting every deck's name and card count and put them in different (list?) adapters.
For the first one, I apparently needed more time to comprehend what it is doing. So IMO, the second one!
Also if you could just rename getDeck_name to getDeckName that would be better for people to read. getDeckName follows the naming convention for naming Java methods i.e. camelCase.
if you want to get data from simple list thnn foreach loop is good to use but,,, if you want to data from exact position or to store from id than for-loop is better ..
and there is NO difference by performance wise both are same as well, as i know.
as my suggestion use for loop :)
As per this book Code Complete - Steve McConnell's
for loop is good choice when you need a loop that executes a specified number of times.
foreach loop is useful for performing an operation on each member of an array or the container.
for more visit : Google books - Code Complete

Sorting a ListView by date? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
As you can see down here I have a ListView populated by a JSON but I want to sort the list by date and to have the same items down the corresponding title.
In the image down the the row third and fourth have the same date but in different row.
I do not know what code should I show if the adapter or where I populate the list.
I think U need to sort the populated data date wise in hashmap
hashmap<date,list<other Object>>like this and then use expandable listview to show the populated data..
In the spirit of having less computation processes into the Android client, I suggest you create a new method called findServicesByDate from the server, which should give you the right order based on dates.
However, if this is not possible, the only thing you have to do is order the array based on date, you should check this answer.
Is a basic. Have you ever try to Google it ?
Use a comparator.
Collections.sort(yourList, new Comparator<YourObjectInYourList>() {
public int compare(YourObjectInYourList o1, YourObjectInYourList o2) {
if (o1.getDate() == null || o2.getDate() == null)
return 0;
return o1.getDate().compareTo(o2.getDate());
}
});

(Android) Converting a database full of parking spot coordinates into pindrops on a map [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am trying to fill areas of Seattle with pindrops that will provide information when pressed, but am trying to avoid hard coding each and every pindrop. I think it would be easiest to fill a database (SQLite) with the information and have a method that automatically converts the information into the pindrops, but I'd like to confirm this.
This is a super vague question, and I am just looking for someone to point me in a good direction as I have hit a wall. How should I be going about this? Would SQLite be the simplest solution? Is there an easier way? Thanks!!
I think a SQLite database wrapped with a ContentProvider would be your best bet. If the coordinates you (will) have are lat lon, it will be cake to just convert them into markers on a map. Also depends what map you're planning of using.
For Google Maps API it would be as simple as:
// When CursorLoader finishes it calls onLoadFinished, there loop the returned data
// Table columns: float latitude, float longitude, String marker title
if (cursor.moveToFirst()) {
for (int i=0; i<cursor.getCount(); i++ ) {
mMap.addMarker(new MarkerOptions()
.position(new LatLng(cursor.getFloat(0), cursor.getFloat(1)))
.title(cursor.getString(2))
.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker)));
}
}
Under 50 rep so I cannot comment, hence this here.
You can create pin drop points in html. Any database should work. Just grab all points as an array or json and combine it with the appropriate html in a loop function.
//Edit//
I just noticed you tagged android. Scratch the html comment and check this out
https://developers.google.com/maps/documentation/android/marker
Yes using sqlite would be a totally better choice than hardcoding it into the app. You can then use PHP to read data from the sqlite database and display it on the page using google maps api or whatever you like.

What is a concrete example of Android app functionality that uses HashMap? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am a beginner in Android, and understand only very basically that HashMap class enables key/value pairs. But how does this translate into actually using this in an Android app? Could someone provide a simple, plain English example of what case you might want to use HashMap in an app? I cannot imagine a case where I might need it. Make up an Android app idea, if needed. Thanks in advance.
I am looking for a "big picture" analysis that will give some examples where you might use HashMap with certain Android functionalities you are trying to implement.
HashMap or Map interface is not new on android, This is Java Collections framework.
Java collection are meant to be used in several cases to hold data and contain 3 interfaces:
List - Basically simple list,or linked list implementations
Set - The same as list but won't hold 2 equal obejcts(You need to implement you own equals and hashcode)
Map - as you said key value pair.
Uses:
List - For anything, just to hold data
Set - For list of data that we want that all of the items will be unique.
Map - Key value and the most common example is the use for DB items, or something with ids.. for example:
bookId, Book.. I that case you can take the object by id.. This is the most common
I attached link for Java collection tutorial.. It is very important framework that you have to know if you are going to develop java/android
http://tutorials.jenkov.com/java-collections/index.html
Hope that helps
We could use HashMap to keep a list of employess together with their respective salaries.
We can do:
HashMap<String, Float> emplMap = new HashMap<String, Float>();
emplMap.put("fred", 1.000);
for(String name : emplMap.keySet()) {
System.out.print(name + "'s salary is" + emplMap.get(name));
}
Should print
"fred's salary 1.000"

Categories

Resources