Android 在活动和问题之间传递数据

Android 在活动和问题之间传递数据,android,android-studio,Android,Android Studio,在我的项目中,我使用volley库获取数据,并根据这些数据添加按钮,按钮显示正确,但问题是打开另一个活动不起作用: RequestQueue requestQueue; TextView txt; final int MY_REQ_CODE = 12345; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVi

在我的项目中,我使用volley库获取数据,并根据这些数据添加按钮,按钮显示正确,但问题是打开另一个活动不起作用:

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();
        }
    }
}

在内部活动之间移动时,应使用方法,而不是startActivityForResult

更改:

startActivityForResult(intent, MY_REQ_CODE);
致:

更新: 据我所见,您还创建了一个名为btn的新按钮,但您将侦听器分配给其他按钮。我看不到在哪里创建btncategorie按钮,它应该是:

Button btn = new Button(getApplicationContext());
btn.setText(titles[i]);
btn.setOnClickListener(getProducts);
btn.setTag(titles[i]);
layout.addView(btn);

那么,出了什么问题?什么是你想要实现的,而不是发生的?“不工作”是什么意思?是
onClick()
没有被调用?我刚刚尝试过,onClick方法被调用,但打开另一个活动不起作用。我更新了我的问题@nochindeluxe。你能更新你的主要问题并包含整个活动类吗?很抱歉,我更改了一些数据以显示我的类,但忘了更改那些btnCategorie。
startActivity(intent);
Button btn = new Button(getApplicationContext());
btn.setText(titles[i]);
btn.setOnClickListener(getProducts);
btn.setTag(titles[i]);
layout.addView(btn);