0

I am looking for some help with a tutorial I have been working on. I am trying to pass an object when I click on a list item from one activity to another using an Intent. I have posted some of the tutorial code I have been using below but can't seem to get it to work.

My Main Activity code is below:

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

}

And from my Adapter this is the code I have been using this:

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

The intent is listed but it is not carrying the data into my new activity. I just want to take

 hero.getName() 

at the position it was clicked on in the itemlist and open up a new activity, and in the new activity set it to a TextView. This is part of code I have used on the new activity, but it wont post anything into the 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);

For instance I click on spiderman set in the list which is at position 3, and in the new activity the textView will load with Spiderman listed. Any help would be appreciated.

nyjxrb30
  • 78
  • 1
  • 3
  • 11
  • 1
    Possible duplicate of [How to pass an object from one activity to another on Android](https://stackoverflow.com/questions/2736389/how-to-pass-an-object-from-one-activity-to-another-on-android) – Nikunj Paradva Mar 13 '18 at 17:39

2 Answers2

0

Try with this:

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

If you need send the hero name then:

In your adapter:

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

In your activity:

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

You can pass objects using serializable or parcelable interfaces but if you just want the name of your hero you should pass it as string in your intent. Now you're passing an id. You can totally do that. If it's int you should set it correctly to textview

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

That's why it's not working now.

Or just pass it as string right from the beginning

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

And then retrieve it

Intent intent = getIntent();

    if(intent == null)
        return;

    String heroName = intent.getStingExtra(HeroAdapter.KEY_HERO_NAME);
    err.setText(heroName);
Rainmaker
  • 10,294
  • 9
  • 54
  • 89