can anyone help me on how to create an if else statement in android java. ? i am doing a chat app using firebase ,and i got stock because of the notification. now i would like to increase the number of messages every time i chat you. Like in example below ,let say i am john".
John: hi jacki
Jacki: hello
therefore: if i will check my firebase dashboard the numMessage is counted as 1 . because i did chat jacki once in our conversation.
Heres my code/
package com.example.mypc.thisissample;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import com.firebase.client.Firebase;
public class MainActivity extends AppCompatActivity {
private EditText editText;
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Firebase.setAndroidContext(this);
final Firebase firebase = new Firebase("https://example.firebaseio.com/");
editText = (EditText) findViewById(R.id.editText);
listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(new MessageAdapter(this, Message.class, R.layout.fragment_message, firebase.child("chat")));
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Message message = new Message();
message.setMessage(editText.getText().toString());
message.setAuthor("Name");
firebase.child("chat").push().setValue(message);
editText.setText("");
}
});
}
}
I didn't get what you really need, please explain a little better. But if you are asking for if/else statement in java try to read some java cookbook first!
Anyway
if(statement) {
} else {
}
First read de numCount from the firebase and store it on a localVariable
https://www.firebase.com/docs/android/guide/retrieving-data.html
Then every time someone press the send button push the number to the firebase.
https://www.firebase.com/docs/android/guide/saving-data.html
Here you have an example of an increment
final Firebase upvoteref = new Firebase("https://shareurday.firebaseio.com/Message/"+msg_id+"/upvotes");
upvoteref.runTransaction(new Transaction.Handler() {
@Override
public Transaction.Result doTransaction(final MutableData currentData) {
if (currentData.getValue() == null) {
currentData.setValue(1);
} else {
currentData.setValue((Long) currentData.getValue() + 1);
}
return Transaction.success(currentData);
}
public void onComplete(FirebaseError firebaseError, boolean committed, DataSnapshot currentData) {
if (firebaseError != null) {
System.out.println("Firebase counter increment failed.");
} else {
System.out.println("Firebase counter increment succeeded.");
}
}
});
Hope it helps
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.