gpt4 book ai didi

java - 将两个值作为字符串传递给同一 Activity 会返回一个值 null

转载 作者:行者123 更新时间:2023-12-01 06:06:25 24 4
gpt4 key购买 nike

这是我的代码,用于显示两个值:推荐的胰岛素剂量和注入(inject)的胰岛素剂量。这两个值被传递给另一个 Activity 。

我使用了两个 Intent 将两个值传递给另一个 Activity (NewActivity)。

问题是,当使用两个 Intent 时,仅显示一个值。输出显示为:

Recommended insulin dosage:null, Administered insulin dosage:10 (correct value of the calculation).

如果从button.setOnClickListener中删除startActivity(intent),输出将显示为:

Recommended insulin dosage:23 (correct value), Administered insulin dosage:null.

其中一个值正在变为 null。如何显示两个值?

我也尝试将字符串声明为公共(public)

MainActivity.java

public class MainActivity extends Activity implements View.OnClickListener {

public static final int DIALOG_DOWNLOAD_PROGRESS = 0;
private Button startBtn;
private ProgressDialog mProgressDialog;
private Button button;
private Button button2;
private EditText editText;
private EditText editText2;
private EditText editText4;
public static String myid;
public static String insulin;
private Spinner dynamicSpinner;
private ProgressDialog loading;





@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent=getIntent();
String carbval=intent.getStringExtra("carbval");

final EditText editText2 = (EditText) findViewById(R.id.editText2);
editText2.setText(carbval);
dynamicSpinner = (Spinner) findViewById(R.id.dynamic_spinner);

editText = (EditText) findViewById(R.id.editText);
button2 = (Button) findViewById(R.id.button2);
button = (Button) findViewById(R.id.button);

editText4=(EditText) findViewById(R.id.editText4);




button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

String carbohydrate = editText2.getText().toString().trim();
String dailydose = editText4.getText().toString().trim();
int cc = Integer.parseInt(carbohydrate);
int dd = Integer.parseInt(dailydose);
int goc = 450 / dd;
int ab = cc / goc;
myid = Integer.toString(ab);
Intent intent1=new Intent(MainActivity.this,NewActivity.class);
intent1.putExtra("ids",myid);
startActivity(intent1);


}
});




Spinner staticSpinner = (Spinner) findViewById(R.id.static_spinner);

// Create an ArrayAdapter using the string array and a default spinner
ArrayAdapter<CharSequence> staticAdapter = ArrayAdapter
.createFromResource(this, R.array.brew_array,
android.R.layout.simple_spinner_item);

// Specify the layout to use when the list of choices appears
staticAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

// Apply the adapter to the spinner
staticSpinner.setAdapter(staticAdapter);

Spinner dynamicSpinner = (Spinner) findViewById(R.id.dynamic_spinner);

String[] items = new String[] { "---SELECT---", "LOW", "MODERATE", "HIGH" };

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, items);

dynamicSpinner.setAdapter(adapter);

dynamicSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
Log.v("item", (String) parent.getItemAtPosition(position));
}

@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});


}




private void getData() {


String bloodglucose = editText.getText().toString().trim();
if (bloodglucose.equals("")) {
Toast.makeText(this, "Please enter bloodglucose", Toast.LENGTH_LONG).show();
return;
}
String dosage =dynamicSpinner.getSelectedItem().toString().trim();
if (dosage.equals("")) {
Toast.makeText(this, "Please enter dosage", Toast.LENGTH_LONG).show();
return;
}
loading = ProgressDialog.show(this,"Please wait...","Calculating insulin dosage...",false,false);

String url1 = "http://10.1.6.21/getData1.php?bloodglucose="+editText.getText().toString().trim()+"&dosage="+dynamicSpinner.getSelectedItem().toString().trim();


StringRequest stringRequest = new StringRequest(url1, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
loading.dismiss();
showJSON(response);
}
},
new Response.ErrorListener() {

@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this,error.getMessage().toString(),Toast.LENGTH_LONG).show();
}
});


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

}

private void showJSON(String response){

String insulin="";



try {
JSONObject jsonObject = new JSONObject(response);
JSONArray result = jsonObject.getJSONArray(Confignew.JSON_ARRAY);
JSONObject collegeData = result.getJSONObject(0);

insulin = collegeData.getString(Confignew.KEY_INSULIN);

Intent intent1=new Intent(MainActivity.this,NewActivity.class);
intent1.putExtra("value",insulin);

startActivity(intent1);


} catch (JSONException e) {
e.printStackTrace();
}


}

@Override
public void onClick(View v) {
getData();

}

}

NewActivity.java

public class NewActivity extends AppCompatActivity {

private Button mBtGoBack;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new);
TextView tvmessage=(TextView) findViewById(R.id.tv_message);
tvmessage.setText("Recommended Insulin Dosage: "+getIntent().getExtras().getString("value"));

TextView tvmessage1=(TextView) findViewById(R.id.tv_message1);
tvmessage1.setText("Administered Insulin Dosage: "+getIntent().getExtras().getString("ids"));
mBtGoBack = (Button) findViewById(R.id.bt_go_back);

mBtGoBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

finish();
}
});

}
}

最佳答案

查看您的代码,您似乎在开始 Activity 之前重新创建 Intent 。您创建的每个 Intent 只有一个值传递到 NewActivity.java。您需要将两个值添加到同一 Intent ,而不是重新创建它。

这是您的问题:

    Intent intent1=new Intent(MainActivity.this,NewActivity.class);
intent1.putExtra("ids",myid);

Intent intent1=new Intent(MainActivity.this,NewActivity.class);
intent1.putExtra("value",insulin);

您应该只创建 Intent 一次,然后根据需要向其添加额外内容,而不是实例化两次。

关于java - 将两个值作为字符串传递给同一 Activity 会返回一个值 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43648655/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com