如何将Array对象传递给另一个活动?

时间:2018-03-13 17:37:10

标签: android

我正在寻找一些我一直在努力的教程的帮助。当我使用Intent从一个活动点击一个列表项到另一个活动时,我试图传递一个对象。我已经发布了一些我在下面使用的教程代码,但似乎无法使其工作。

我的主要活动代码如下:

    StringRequest stringRequest = new StringRequest(Request.Method.GET, GET_HEROES_URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    try {
                        JSONArray array = new JSONArray(response);

                        for(int i =0; i<array.length(); i++){
                            JSONObject obj = array.getJSONObject(i);

                            Hero hero = obj.getString("name"));

                            heroList.add(hero);
                        }

                        adapter = new HeroAdapter(heroList, getApplicationContext());
                        recyclerView.setAdapter(adapter);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            });

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

}

从我的适配器这是我一直使用的代码:

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    final Hero hero = heroList.get(position);
    holder.textViewName.setText(hero.getName());

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

            Intent intent = new Intent(context, HeroDetailActivity.class);
            intent.putExtra(KEY_HERO_ID, hero.getName());

            context.startActivity(intent);
        }
    });
} 

列出了意图,但它没有将数据载入我的新活动。我只想采取

 hero.getName() 

在项目列表中单击它的位置并打开一个新活动,并在新活动中将其设置为TextView。这是我在新活动中使用的代码的一部分,但它不会在TextView中发布任何内容。

TextView textViewName

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_hero_detail);


    textViewname = (textView) findViewById(R.id.textView);

    Intent intent = getIntent();

    if(intent == null)
        return;

    int id = intent.getIntExtra(HeroAdapter.KEY_HERO_ID, -1);
    err.setText(id);

例如,我点击位于第3位的列表中的spiderman,在新活动中,textView将加载列出的Spiderman。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

试试这个:

int id = -1;
if(getIntent().hasExtra(HeroAdapter.KEY_HERO_ID)){
    id = getIntent().getExtras().getInt(HeroAdapter.KEY_HERO_ID);
}
err.setText("" + id);

如果您需要发送英雄姓名:

  

在适配器中:

Intent intent = new Intent(context, HeroDetailActivity.class);
intent.putExtra(KEY_HERO_ID, hero.getName());
context.startActivity(intent);
  

在您的活动中:

String heroName = "";
if(getIntent().hasExtra(HeroAdapter.KEY_HERO_ID)){
    heroName = getIntent().getExtras().getString(HeroAdapter.KEY_HERO_ID);
}
err.setText(heroName);

答案 1 :(得分:0)

您可以使用serializableparcelable界面传递对象,但如果您只想要英雄的名字,则应将其作为string传递给您。现在你传递id。你完全可以做到这一点。如果是int,则应将其正确设置为textview

err.setText(String.valueOf(id));

这就是为什么它现在不能正常工作。

或者只是从头开始将其作为string传递

Intent intent = new Intent(context, HeroDetailActivity.class);
intent.putExtra(KEY_HERO_NAME, hero.getName());
context.startActivity(intent);

然后检索它

Intent intent = getIntent();

    if(intent == null)
        return;

    String heroName = intent.getStingExtra(HeroAdapter.KEY_HERO_NAME);
    err.setText(heroName);
相关问题