在我的項目中,我使用排球庫來獲取數據,並基於這些數據我將添加按鈕,按鈕顯示正確,但問題是打開另一個活動不起作用:在活動之間傳遞數據問題
RequestQueue requestQueue;
TextView txt;
final int MY_REQ_CODE = 12345;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_categories);
final GridLayout layout = (GridLayout) findViewById(R.id.layout);
requestQueue = Volley.newRequestQueue(this.getApplicationContext());
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, "url", null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONObject jsonanswer = response.getJSONObject("answer");
final String[] name=new String[9];
txt = (TextView) findViewById(R.id.name);
JSONArray result = jsonanswer.getJSONArray("result");
for (int i=0;i<jsonArrayresult.length();i++){
JSONObject objresp = result.getJSONObject(i);
String name= objresp.getString("name");
name[i] = name;
}
for (int i=0;i<titles.length;i++){
Button btn = new Button(getApplicationContext());
btn.setText(titles[i]);
layout.addView(btn);
btn.setOnClickListener(getProducts);
btn.setTag(titles[i]);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("VOLLEY", "ERROR");
}
}
);
requestQueue.add(jsonObjectRequest);
}
View.OnClickListener getProducts = new View.OnClickListener() {
@Override
public void onClick(View v) {
Object tag = v.getTag();
Intent intent = new Intent(getApplicationContext(), otherActivity.class);
intent.putExtra("name", tag.toString());
startActivity(intent);
}
};
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == MY_REQ_CODE) {
if (resultCode == RESULT_OK) {
//??
} else if (resultCode == RESULT_CANCELED){
Toast.makeText(getApplicationContext(), "Result canceled", Toast.LENGTH_SHORT).show();
}
}
}
我的第二個活動是:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_products_list);
setResult(RESULT_OK);
finish();
final Intent intent = getIntent();
final TextView txt = (TextView) findViewById(R.id.txt);
Bundle title = intent.getExtras();
if (title != null) {
str = title.getString("title");
}
txt.setText(str);
}
那麼是什麼地方出了錯,然後呢?你試圖完成的是什麼,這不會發生? 「不工作」是什麼意思? 'onClick()'沒有被調用? – NoChinDeluxe
我剛剛嘗試和onClick方法被調用,但打開另一個活動不起作用 – Guest012393
我已更新我的問題@NoChinDeluxe – Guest012393