Not able to add child to existing child in firebase android - java

I am creating a playlist app where every playlist has a unique key and have 2 child elements playlistId and playlist name. I want to add songs as a child under the playlist name.
Like this:
But this is what I am getting instead:
My code which brings the above-unwanted error prone result is -
databaseReference = FirebaseDatabase.getInstance().getReference().child("a44f757c1f96ac85");
public void saveSongsToPlayList(View view){
try {
String song = "Song";
databaseReference.child(playListId).child(playListName)
.child("New song").child(song).setValue(song);
}catch (Exception ex){
ex.printStackTrace();
}
}
I don't think that I need to use the data snapshot for such a simple task. Or am I wrong?

Try this
public void saveSongsToPlayList(View view){
try {
Firebase firebase = new Firebase("https://your-firebase-id.com").child("a44f757c1f96ac85").child("LZIZ......").child("playListName").child("NewSong")
Firebase song1 = firebase.child("song")
song1.setValue("link to song or whatever")
}catch (Exception ex){
ex.printStackTrace();
}
}

Related

Pagination example for spotify api java [spotify-web-api-java]

I'm trying to create a playlist viewer and I got stuck on paging.
I want to get all the names of current users playlists.
Any help would be appreciated.
public void getPlaylists() {
try {
SpotifyApi spotifyApi = new SpotifyApi.Builder().setAccessToken(auth.getToken()).build();
GetListOfCurrentUsersPlaylistsRequest getListOfCurrentUsersPlaylistsRequest = spotifyApi.getListOfCurrentUsersPlaylists().limit(5).offset(0).build();
Paging<PlaylistSimplified> playlistSimplifiedPaging = getListOfCurrentUsersPlaylistsRequest.execute();
} catch (IOException | SpotifyWebApiException e) {
System.out.println("Erroras: " + e.getMessage());
}
}
Okey so found out the answer myself don't know if its the correct way but it will work.
You can convert the paging object to a list :
List<PlaylistSimplified> playlists = Arrays.asList(playlistSimplifiedPaging.getItems());
And then get the info about your object normally.

How to uniquely identify each Task in javafx?

I have a ListView containing URLs. When a user click on one of the URL, a video is downloaded. I am calling the video download function within a Task which in turn is called in a Thread. A user can click on multiple video URL and the video would start to download. A separate Task would be created for each of the video. What i want to know is how to uniquely identify Task for each video?
Function to download video:
public void videoFileDownload(){
try {
videoDownloadUrl = lblURL.getText().toString();
IndexOfThisNode = hbox.getId();
String path = "XXXX";
downloadThisVideo = new VGet(new URL(videoDownloadUrl),new File(path));
downloadThisVideo.download();
System.out.println("Download this video: " + videoDownloadUrl + downloadThisVideo.getVideo().getState());
System.out.println("Download complete");
} catch (Exception ex) {
ex.printStackTrace();
System.out.println("Retrying...");
}
}
Function containing Task:
public void showDetailsButton(){
btnSMDetails.addEventHandler(MouseEvent.MOUSE_CLICKED, (e)->{
System.out.println("\n" + "The index is: " + getIndex() + "\n");
showLoader();
//Task created to download videos in background without blocking UI
Task downloadVideoTask = new Task<Void>() {
#Override
public Void call() {
//SIMULATE A FILE DOWNLOAD
videoFileDownload();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
};
new Thread(downloadVideoTask).start();
downloadVideoTask.setOnSucceeded(taskFinishEvent ->{showLoader(); /*isButtonClicked="0";*/});
});
}
Listcells don't exist in a one to one relationship with the underlying list. There's only enough listcells instantiated to fill the viewport of the listview plus a couple extra. Data is swapped in and out of the listcells through the updateCell method.
So you can't store data in a listcell, since the cell will get reused for another list item if you scroll the list.
What you need to do is to store a reference to the task in the underlying list item. Modify your updateCell method to bind the visibility and value of your progress bar in the listcell to the task progress property.

Inserting data, "MobileServiceTable must have a single id property defined"

I'm currently developing an android app that requires data to be inserted into an azure Mobile Service DB. An id string and a first login integer, to be exact. However the following error is being thrown up.
"IllegalArgumentException: The class representing the MobileServiceTable must have a single id property defined"
The id value that I need to insert into the database is being passed back from a fragment interface using passId(). Inside the override of this is where I am attempting to insert the values into azure as shown below.
#Override
public void passId(String id) {
userInstance user = new userInstance();
user.user_id = id;
user.first_login = 0;
mClient.getTable(userInstance.class).insert(user, new TableOperationCallback<userInstance>() {
public void onCompleted(userInstance entity, Exception exception, ServiceFilterResponse response) {
if (exception == null) {
// Insert succeeded
} else {
// Insert failed
}
}
});
The mClient var represents the MobileServicesClient as shown below
try {
mClient = new MobileServiceClient(
"https://xxxx.azure-mobile.net/",
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
this);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (Exception e){
e.printStackTrace();
}
The table name that I am trying to insert the data into is "user_table" if that helps at all.
I hope you're able to help, and thanks in advance for any help you guys give me.
SOLUTION:
Because the Azure Table that I was attempting to add data to auto created an "id" column, the user object that I was using to construct user info to insert into the database had to define an "id" String. As shown below:
public class userInstance {
#com.google.gson.annotations.SerializedName("id")
public String mId;
#com.google.gson.annotations.SerializedName("user_id")
public String mUserId;
#com.google.gson.annotations.SerializedName("first_login")
public int mLogin;
}

How to delete a specific row from azure mobile service table using Android?

I am new to Android and Window Azure.I have created a database in Azure and some Tables.now I successfully inserted data to the table but now I want to delete a row from that table.I have tried the following code but its not working and the amulater gets hing .Please help me to solve this problem
//code use for delete.
del.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final MobileServiceTable<Test> mtest;
mtest=mClient.getTable(Test.class);
try {
final MobileServiceList<Test>res=mtest.where().field("fullname").eq("hanan").execute().get();
mtest.delete("res");
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
});
In the above code I just tried to delete the row whose fullname field =="hanan"
If you are using Moblie Service with SQL database as a backend server, there are 2 ways to delete an item form table referred on official guide
A, mTable.delete(item)
B, mTable.delete(IDString)
However, in your code:
mtest.delete("res");
you used a string in delete function, Azure will delete the row with id field equl “res”.

Android Parse.com return corrupted json

I'm using Parse.com services for an Android application but when I try to get datas from Parse, an error occurs:
com.parse.ParseException: corrupted json: org.json.JSONException: No value for code
Here is the code I use to get the datas:
public void getCategories() {
listCategories = new ArrayList<>();
ParseQuery<Category> query = ParseQuery.getQuery(Category.class);
query.findInBackground(new FindCallback<Category>() {
#Override
public void done(List<Category> categories, ParseException e) {
if (e == null) {
// Clear previously loaded Categories
listCategories.clear();
for (Category category : categories) {
// Fill the list with retrieved Categories
lsistCategories.add(category);
}
}else {
Log.d("Error happened while retrieving data:", e.getMessage());
}
}
});
}
Any idea of what could be the problem?
I solved it, in fact I forgot to register my custom Category class in my Application class.
I added this to the onCreate method in my Application class and now it works:
ParseObject.registerSubclass(Category.class);

Categories

Resources