【问题标题】:Android Studio: 2 different activities in 1 buttonAndroid Studio:1 个按钮中的 2 个不同的活动
【发布时间】:2018-05-29 06:58:45
【问题描述】:

我想让登录按钮使用数据库上的标志列分派到 2 个不同的活动中??

如果租户登录,它将重定向到租户活动

当房东登录时,它会重定向到其他活动吗?

public void onResponse(String response) {
            Log.e(TAG, "Register Response: " + response.toString());
            hideDialog();

            try {
                JSONObject jObj = new JSONObject(response);
                success = jObj.getInt(TAG_SUCCESS);

                // Check for error node in json
                if (success == 1) {


                    String username = jObj.getString(TAG_USERNAME);
                    String id = jObj.getString(TAG_ID);

                    Log.e("Successfully Login!", jObj.toString());

                    Toast.makeText(getApplicationContext(), jObj.getString(TAG_MESSAGE), Toast.LENGTH_LONG).show();


                    SharedPreferences.Editor editor = sharedpreferences.edit();
                    editor.putBoolean(session_status, true);
                    editor.putString(TAG_ID, id);
                    editor.putString(TAG_USERNAME, username);
                    editor.commit();

                    // go to main activity
                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                    intent.putExtra(TAG_ID, id);
                    intent.putExtra(TAG_USERNAME, username);
                    finish();
                    startActivity(intent);
                } else {
                    Toast.makeText(getApplicationContext(),
                            jObj.getString(TAG_MESSAGE), Toast.LENGTH_LONG).show();

                }
            } catch (JSONException e) {
                // JSON error
                e.printStackTrace();
            }

        }

【问题讨论】:

  • 您可以根据条件添加if语句并在其中使用intent
  • java? php?请不要标记垃圾邮件
  • @DheerajJoshi 我应该在哪里以及什么条件下使用它??

标签: java php mysql android-studio android-volley


【解决方案1】:

log in 按钮的onClickListener() 方法中,可以放置if() 语句。

执行此操作的步骤

1.从用户名和密码字段中获取数据,并在查询中使用该数据从数据库中检索数据

2. 将您检索到的数据存储在如下所示的字符串中,因为您只会得到一行:

String flag = cursor.getString(7); //skipping the cursor and other database part

3. 现在你可以在获取String flag 值后将if() 语句放在登录按钮的onClickListener 中,如:

if(flag.equals(landlord)){
Intent intent = new Intent(this,Landlord.class);
 startActivity(intent);
}
else{
 Intent intent = new Intent(this,Tenant.class);
 startActivity(intent);
 }

【讨论】:

    【解决方案2】:

    无论如何,您将查询以验证用户名和密码,同时您还可以获取标志信息并将其存储在本地(减少数据库调用)。

    将这些数据存储在地图中,键为用户名(假设用户名将是唯一的)并标记为值。

    Map<String,String> mapToRouting = new Map<String,String>();
    

    现在为了路由不同的活动,在按钮点击方法里面试试

    public static void buttonClickMethod(){
       //textFieldUserName is the username of user trying to login
      if(isLanlord(textFieldUserName)){
         //this means user is Landlord & put code to redirect to corresponding screen
      }
      else{
         //put code to which redirects to tenants screen!
      }
    
    }
    

    判断用户是否为房东的方法。

    public static void isLandlord(String userName){
      boolean result = false;
          if(mapToRouting.get(userName).equalsIgnorecase("Landlord"))
              result = true;
      return result;
    }
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-07-15
      • 2017-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-19
      相关资源
      最近更新 更多