从方法获取String或Int以将其发送到另一个Activity

时间:2017-03-15 07:28:50

标签: java android

每次用户进入应用程序时,我都会从服务器获取一个ID,我需要将该ID发送到另一个活动,因为我根据从服务器获取的ID做了一些事情。但我不知道如何从函数中获取它以将其存储在变量中,然后通过意图发送它。这是我的代码请指导我如何做到:

public class SuperAdminActivity extends AppCompatActivity {
int ID = -1;
private String s;
private ProgressDialog loading;
private SharedPreferences sp;
private Button register_new_company, register_new_user, see_all_userslist, see_all_companies_list, new_item;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_super_admin);
    register_new_company = (Button) findViewById(R.id.btn_register_new_company);
    register_new_user = (Button) findViewById(R.id.btn_register_new_user);
    see_all_userslist = (Button) findViewById(R.id.btn_show_all_users_list);
    see_all_companies_list = (Button) findViewById(R.id.btn_all_organizations_list);
    new_item = (Button) findViewById(R.id.btn_new_item);
    sp = getApplicationContext().getSharedPreferences("userP", 0);
    s = sp.getString("Username", "");
    Log.e("Username ", s);
    getData();
}
public void onRegisterButtonNewCompanyClicked(View v) {
    startActivity(new Intent(this, RegisterNewCompany.class));
}

public void onUserRegisterButtonClicked(View v) {
    startActivity(new Intent(this, UserRegisteration.class));

}



@Override
public void onBackPressed() {
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_HOME);
    startActivity(intent);
}


private void getData() {
    String id = s;
    loading = ProgressDialog.show(this, "Please wait...", "Getting Some data...", false, false);

    String url = config.DATA_URL + s.toString().trim();
    StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            loading.dismiss();
            showJSON(response);

            Log.e("RESPONE : ", response);
        }
    },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                }
            });

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
}

private void showJSON(String response) {
    String name = "";
    try {
        JSONObject jsonObject = new JSONObject(response);
        JSONArray result = jsonObject.getJSONArray(config.JSON_ARRAY);
        JSONObject collegeData = result.getJSONObject(0);
        name = collegeData.getString("usr_type");
    } catch (JSONException e) {
        e.printStackTrace();
    }
    Log.e("UserType = ", name);
    int i = Integer.parseInt(name);
    ID = Integer.parseInt(name);
    Log.e("My Current ID :" , String.valueOf(ID));
    if (i == 1) {
        register_new_company.setEnabled(true);
        register_new_user.setEnabled(true);
    } else {
        register_new_company.setEnabled(false);
        register_new_user.setEnabled(false);
    }

}

public void onBtnAllUsersListCLick(View v)
{
    Intent intent = new Intent(getApplicationContext(),ActivityAllUsers.class);
    intent.putExtra("ID",ID);
    startActivity(intent);
    //startActivity(new Intent(this, ActivityAllUsers.class));
}
}

如您所见,我可以访问getData();内的onCreate功能,此方法记录从服务器端获取的数组,但我想存储 int ,它位于 showJSON 方法

3 个答案:

答案 0 :(得分:1)

您可以传递数据:直接使用Intent,如下所示:

Intent有几个名为putExtra(String name, …..)的方法,它允许我们在Intent中保存我们的信息。阅读API Doc我们知道我们可以添加字符串,long,CharSequence,int等等。它的行为类似于Map,其中包含键和值。在调用者(EditActivity)中,我们可以通过这种方式传递数据:

Intent intent = new Intent(EditActivity.this, ViewActivity.class);
intent .putExtra("name", edtName.getText().toString();
intent .putExtra("surname", edtSurname.getText().toString();
intent .putExtra("email", edtEmail.getText().toString();
startActivity(i);

您可以在bwlow:

等下一个活动中检索所有这些数据

“name”,“surname”和“email”是关键。换句话说,我们首先创建定义调用者活动(EditActivity)和目标活动(ViewActivity)的intent(第1行),然后使用putExtra添加我们的数据。在目标活动中检索发送的数据我们有:

protected void onCreate(Bundle savedInstanceState) {
Intent intent  = getIntent();

String name = intent .getStringExtra("name"));
String surname = intent .getStringExtra("surname"));
String email = intent .getStringExtra("email"));
. . .
}

请在以下链接中找到更多详情: Passing data between activities

答案 1 :(得分:1)

首先创建一个整数类型的全局变量ID,如

int ID = -1;// -1 is default value

然后在showJSON方法

中指定值

ID = //get value from json which is received from server

然后将其传递给任何此类活动

          Intent intent = new Intent(SuperAdminActivity.this, UserRegisteration.class);
          intent.putExtra("ID", ID)
          startActivity(intent);

UserRegisteration中获取ID

int id = getIntent().getIntExtra("ID", -1);// -1 is default value 

答案 2 :(得分:1)

在您的活动中创建一个全局变量,例如int number String s。在您showJSON的{​​{1}} int i = Integer.parseInt(name);i将此int number投放到您的全局number = i; intent.putExtra("key", number); ,点击每次按钮,您都可以在意图内传递此号码。像

{{1}}