我将数据从MySQL传递到活动中的文本框,该文本成功完成,但是我需要有意地将这些文本发送到其他活动中,但对我没有警告,请帮忙
活动一:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dollars);
item_k001 = findViewById(R.id.item_k101);
item_k002 = findViewById(R.id.item_k102);
requestQueue = Volley.newRequestQueue(this);
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
final Globalv globalv = (Globalv) getApplicationContext();
try {
JSONArray jsonArray = response.getJSONArray("allegg");
String msg = "";
for (int i = 0; i < response.length(); i++) {
JSONObject respons = jsonArray.getJSONObject(i);
final String myItem_k001 = respons.getString("item_k1");
final String myItem_k002 = respons.getString("item_k2");
DollarsActivity.this.runOnUiThread(new Runnable() {
public void run() {
item_k001.setText(myItem_k001);
item_k002.setText(myItem_k002);
}
});
}
JSONObject respons2 = jsonArray.getJSONObject(0);
String id = respons2.getString("id");
globalv.setTotal_threads(Integer.parseInt(String.valueOf(id)));
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("VOLLEY", "ERROR");
}
}
);
我想向活动tow发送拖车文本:
//defines variables to the xml elements.
TextView tv1 = (TextView) findViewById(R.id.marqueeTextView1);
TextView tv2 = (TextView) findViewById(R.id.marqueeTextView2);
/// You can change your text here
tv1.setText("Text view");
tv2.setText("Text view ");
// We set the setSelected value to true for the shift of the texts
tv1.setSelected(true);
tv2.setSelected(true);
答案 0 :(得分:1)
请尝试以下代码:
活动1
intent = new Intent(OneActivity.this, TwoActivity.class);
intent.putExtra("KEY1", myItem_k001);
intent.putExtra("KEY2", myItem_k002);
startActivity(intent);
活动2
//In onCreate()
Intent intent = getIntent();
text1 = intent.getStringExtra("KEY1");
text2 = intent.getStringExtra("KEY2")
tv1.setText(text1);
tv2.setText(text2);
答案 1 :(得分:0)
如果您想在收到文字后开始活动2,则可以使用intent, 但是如果activity2是稍后打开的并且现在是onPouse, 您可以轻松使用界面:
在sampleInterface中:
public interface sampleInterface{
void setText(String text);
}
在活动1中添加:
sampleInterface callBack;
在活动1中,将其放入您获得文字的那一行:
callBack.setText("your Text");
活动2:
public class Activity2 extends AppCompatActivity implements ,sampleInterface{
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_layout_2);
textView= findViewById(R.id.textView);
}
@Override
public void setText(String yourText) {
textView.setText(yourText);
}
}
在您致电callback.setText("your text here");
的任何地方都会触发@Override
Ativity2中的方法,它将返回您的文本
希望这会有所帮助