Java 从方法中获取字符串或Int以将其发送到另一个活动

Java 从方法中获取字符串或Int以将其发送到另一个活动,java,android,Java,Android,每次用户进入应用程序时,我都会从服务器获取一个ID,我需要将该ID发送到另一个活动,因为我根据从服务器获取的ID进行操作。但我不知道如何从函数中获取它,将其存储在变量中,然后通过意图发送它。这是我的代码,请指导我如何操作: public class SuperAdminActivity extends AppCompatActivity { int ID = -1; private String s; private ProgressDialog loading; private SharedP

每次用户进入应用程序时,我都会从服务器获取一个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));
}
}
公共类超级管理扩展了AppCompatActivity{
int ID=-1;
私有字符串;
私有对话框加载;
私人共享参考sp;
专用按钮注册新公司、注册新用户、查看所有用户列表、查看所有公司列表、新建项目;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u super\u admin);
注册新公司=(按钮)findViewById(R.id.btn\u注册新公司);
register\u new\u user=(按钮)findviewbyd(R.id.btn\u register\u new\u user);
请参阅所有用户列表=(按钮)findViewById(R.id.btn\u显示所有用户列表);
请参阅所有公司列表=(按钮)findViewById(R.id.btn所有组织列表);
新建项目=(按钮)findViewById(R.id.btn\u新建项目);
sp=getApplicationContext().getSharedReferences(“userP”,0);
s=sp.getString(“用户名”,“用户名”);
Log.e(“用户名”,s);
getData();
}
public void onRegisterButtonNewCompanyClicked(视图v){
startActivity(新意图(此,RegisterNewCompany.class));
}
已单击用户注册表上的公共无效按钮(视图v){
startActivity(新意图(this,userRegistration.class));
}
@凌驾
public void onBackPressed(){
意向意向=新意向(意向.行动\主要);
intent.addCategory(intent.CATEGORY_HOME);
星触觉(意向);
}
私有void getData(){
字符串id=s;
loading=ProgressDialog.show(这是“请稍候…”,“获取一些数据…”,false,false);
字符串url=config.DATA_url+s.toString().trim();
StringRequest StringRequest=新的StringRequest(url,new Response.Listener()){
@凌驾
公共void onResponse(字符串响应){
loading.dispose();
showJSON(响应);
Log.e(“响应:”,响应);
}
},
新的Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
}
});
RequestQueue RequestQueue=Volley.newRequestQueue(this);
添加(stringRequest);
}
私有void showJSON(字符串响应){
字符串名称=”;
试一试{
JSONObject JSONObject=新JSONObject(响应);
JSONArray result=jsonObject.getJSONArray(config.JSON_数组);
JSONObject collegeData=result.getJSONObject(0);
name=collegeData.getString(“usr_类型”);
}捕获(JSONException e){
e、 printStackTrace();
}
Log.e(“UserType=”,name);
int i=Integer.parseInt(名称);
ID=Integer.parseInt(名称);
Log.e(“我当前的ID:,String.valueOf(ID));
如果(i==1){
注册新公司。设置启用(true);
register\u new\u user.setEnabled(true);
}否则{
注册新公司。设置启用(false);
register\u new\u user.setEnabled(false);
}
}
在BtnallUsersListClick上的公共无效(视图v)
{
Intent Intent=新的Intent(getApplicationContext(),ActivityAllUsers.class);
意向。额外(“ID”,ID);
星触觉(意向);
//startActivity(新意图(本,ActivityAllUsers.class));
}
}

如您所见,我可以访问
getData()onCreate
中的code>函数,此方法记录它从服务器端获得的数组,但我想存储int,它位于showJSON方法中,您可以传递数据:直接使用如下意图:

Intent有几个名为
putExtra(字符串名,…)
的方法,允许我们在Intent中保存信息。阅读API文档,我们知道我们可以添加字符串、long、
CharSequence
、int等等。它的行为就像一个有键和值的映射。在调用者
(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)中检索所有这些数据:

其中“姓名”、“姓氏”和“电子邮件”是关键。换句话说,我们首先创建定义调用方活动(EditActivity)和目标活动(ViewActivity)的意图(第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"));
. . .
}
请在以下链接中查找更多详细信息:

首先创建一个全局变量ID,它类似于整数类型

int ID = -1;// -1 is default value
然后在showJSON方法中赋值

然后将其传递给任何类似这样的活动

          Intent intent = new Intent(SuperAdminActivity.this, UserRegisteration.class);
          intent.putExtra("ID", ID)
          startActivity(intent);
userRegistration
like中获取ID

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

在活动中创建一个全局变量,如创建字符串s时的
int number
。在您的
showJSON
中,您将获得
int i=Integer.parseInt(name)将此
i
分配给您的全局
int number
类似
number=i
并在每个按钮上单击,您都可以在intent中传递此号码。像

intent.putExtra("key", number);

发布你的json,我照你说的做了,但在其他活动中我无法获取ID。我通过登录其他活动打印了ID,但它记录了-1。这里
ID=Integer.parseInt(name)我将I分配给ID并记录,它正确显示了ID,但当我在其他活动中获得它时,它是-1@ShayeaID=Integer.parseInt(名称);你能把这个打印出来并告诉我日志是什么吗?这是它的日志:
03-15 07:32:54.101 8205-8205/com.sof.soflogisticcompany E/My Current ID::4
你能发布你的整个活动代码吗[所以我可以知道吗?@Shayea是的,你是对的,但是如果没有你做的其他事情,我怎么能让你知道,所以请粘贴,这样我可以进一步帮助你。我做了你说的,但我无法在其他活动中获得ID。我打印了
intent.putExtra("key", number);