我如何使用 Button 上的 If 方法从实时数据库获取 ActionView link,如果没有 link 给出 toast 消息,则没有 link

How can i use If method on Button getting ActionView link from realtime database, and if there's no link give toast message there's no link

我正在使用 Firebase 实时数据库,并使用检索数据,我想问是否要在 OnClick() 上为 button 并从实时数据库中获取 ActionView link,如果数据库中没有 link,则给出 "There's no link available ATM"

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.res_screen);

    String data0= getIntent().getStringExtra("Name");
    String data1= getIntent().getStringExtra("Address");
    String data2= getIntent().getStringExtra("Phone1");
    String data3= getIntent().getStringExtra("Phone2");
    String data4= getIntent().getStringExtra("Phone3");
    String data5= getIntent().getStringExtra("Offer");
    final String data6= getIntent().getStringExtra("Facebook");
    final String data7= getIntent().getStringExtra("Menu");
    final String data8 = getIntent().getStringExtra("Menu");

    TextView nm = findViewById(R.id.name);
    TextView ad = findViewById(R.id.address);
    TextView ph1 = findViewById(R.id.phone1);
    TextView ph2 = findViewById(R.id.phone2);
    TextView ph3 = findViewById(R.id.phone3);
    TextView off = findViewById(R.id.closeoffer);
    ImageView fb = findViewById(R.id.facebookbut);
    Button menu = findViewById(R.id.menubut);

    nm.setText(data0);
    ad.setText(data1);
    ph1.setText(data2);
    ph2.setText(data3);
    ph3.setText(data4);
    off.setText(data5);
    fb.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(data6));
            startActivity(intent);
        }
    });
    menu.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(getIntent().getStringExtra("Menu")));
            startActivity(i);
        }
    });
}

可能是我的语言不好,所以我有点困惑。 我想将 if else 添加到 OnClick() 中。 意思是..如果这个按钮有 link 来访问 ACTION_VIEW,那么就去吧。但是,如果还没有 link 来自实时数据库,则给出一个 Toast 消息 "Sorry there's no link available at this moment"

也许您需要这样做:

fb.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
         //data6 may be having link to fb ,here we check if it is not null 
           if(data6!=null )
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(data6));
            startActivity(intent);
        }else{
         //toast a message
          Toast.makeText(Yourclass.this, "Sorry there's no link available at this moment", Toast.LENGTH_SHORT).show();
         }
    });