簡體   English   中英

java中的if else語句(Android)

[英]If else statement in java (Android)

任何人都可以幫助我如何在 android java 中創建 if else 語句。 ? 我正在使用 firebase 做一個聊天應用程序,由於收到通知,我得到了庫存。 現在我想增加每次和你聊天的消息數量。 就像下面的例子,假設我是約翰”。

John: hi jacki
Jacki: hello

因此:如果我將檢查我的 firebase 儀表板,則 numMessage 被計為 1 。 因為我在我們的談話中確實和 jacki 聊過一次。

這是我的代碼/

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("");



            }
        });
    }


}

我沒有得到你真正需要的東西,請解釋好一點。 但是,如果您在 Java 中要求 if/else 語句,請先嘗試閱讀一些 Java 食譜!

反正

if(statement) {
} else {
}

編輯一:

首先從 firebase 讀取 de numCount 並將其存儲在 localVariable 上

https://www.firebase.com/docs/android/guide/retrieving-data.html

然后每次有人按下發送按鈕時,都會將數字推送到火力基地。

https://www.firebase.com/docs/android/guide/saving-data.html

這里有一個增量示例

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.");
                }
            }
        });

希望能幫助到你

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM