android - Passing selected value to another activity (using recycleview/textView) -


i'm making pokedex app , i'm stuck @ passing data of selected pokemon activity(to show more details). problem keeps sending same value other activity(the first value in list, in case bulbasaur) if select different pokemon.

the list consists of recycleview, textview , imageview.and use value of textview data send other activity.

so want pass correct name(textview) selected pokemon other activity click image. , im not sure i'm missing.

thanks in advance!


adapter code

public class pokemonlistadapter extends recyclerview.adapter<pokemonlistadapter.viewholder>{  private arraylist<pokemon> dataset; private context context;  public pokemonlistadapter(context context){     this.context = context;     dataset = new arraylist<>(); }  @override public viewholder oncreateviewholder(viewgroup parent, int viewtype){     view view =   layoutinflater.from(parent.getcontext()).inflate(r.layout.image_pokemon,                                              parent,false);     return new viewholder(view); }  @override public void onbindviewholder(viewholder holder, int position){     pokemon p = dataset.get(position);     holder.pokemontextview.settext(p.getname());      glide.with(context).load("http://pokeapi.co/media/sprites/pokemon/" + p.getnumber() + ".png")             .centercrop()             .crossfade()             .into(holder.pictureimageview);  }  @override public int getitemcount(){     return dataset.size(); }  public void toegevoegdepokemonlist(arraylist<pokemon> pokemonlist) {     dataset.addall(pokemonlist);     notifydatasetchanged(); }  public class viewholder extends recyclerview.viewholder{     private imageview pictureimageview;     private textview pokemontextview;       public viewholder(view itemview) {         super(itemview);         pictureimageview = (imageview) itemview.findviewbyid(r.id.pictureimageview);         pokemontextview = (textview) itemview.findviewbyid(r.id.pokemontextview);     } } 

first activity (list)

 public class pokedex extends appcompatactivity {  private retrofit retrofit; private static final string tag = "pokedex"; private recyclerview recyclerview; private pokemonlistadapter pokemonlistadapter; private int offset; private boolean loaded;  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_pokedex);      //recycleviewer     recyclerview = (recyclerview) findviewbyid(r.id.recyclerview);     pokemonlistadapter = new pokemonlistadapter(this);     recyclerview.setadapter(pokemonlistadapter);     recyclerview.sethasfixedsize(true);      //grid     final gridlayoutmanager layoutmanager = new gridlayoutmanager(this, 3);     recyclerview.setlayoutmanager(layoutmanager);     recyclerview.addonscrolllistener(new recyclerview.onscrolllistener() {         @override         public void onscrolled(recyclerview recyclerview, int dx, int dy) {             super.onscrolled(recyclerview, dx, dy);              if(dy > 0){                 int visibleitemcount = layoutmanager.getchildcount();                 int totalitemcount = layoutmanager.getitemcount();                 int partvisible = layoutmanager.findfirstvisibleitemposition();                  if(loaded){                     if((visibleitemcount + partvisible) >= totalitemcount){                         log.i(tag, "final");                         loaded = false;                         offset += 20;                         getdatapokemon(offset);                     }                 }             }         }     });      //retrofit     retrofit = new retrofit.builder()             .baseurl("http://pokeapi.co/api/v2/")             .addconverterfactory(gsonconverterfactory.create())             .build();      loaded = true;     offset = 0;      getdatapokemon(offset); }   //method private void getdatapokemon(int offset) {     apiservice service = retrofit.create(apiservice.class);     call<pokemonrequest> pokemonrequestcall = service.getpokemonlist(20, offset);      pokemonrequestcall.enqueue(new callback<pokemonrequest>() {         @override         public void onresponse(call<pokemonrequest> call, response<pokemonrequest> response) {             loaded = true;             if (response.issuccessful()) {                  pokemonrequest pokemonrequest = response.body();                 arraylist<pokemon> pokemonlist = pokemonrequest.getresults();                  pokemonlistadapter.toegevoegdepokemonlist(pokemonlist);              } else {                 log.e(tag, "onresponse: " + response.errorbody());             }         }          @override         public void onfailure(call<pokemonrequest> call, throwable t) {             loaded = true;             log.e(tag, "onfailure: " + t.getmessage());         }      }); }   public void pokemon_onclick(view v) {     textview textview = (textview)findviewbyid(r.id.pokemontextview);     intent intent = new intent();     intent.putextra("pokemonname", textview.gettext().tostring());     intent.setclass(this, pokemondetail.class);     startactivity(intent); } 

xml layout file (first activity)

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="be.thomasmore.project_idexv2.pokedex">  <android.support.v7.widget.recyclerview     android:id="@+id/recyclerview"     android:layout_height="match_parent"     android:layout_width="match_parent"/> 


xml layout file(which shows image , text)

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content">  <imageview     android:id="@+id/pictureimageview"     android:layout_width="96dp"     android:layout_height="96dp"     android:layout_gravity="center_horizontal"     android:onclick="pokemon_onclick"/>  <textview     android:id="@+id/pokemontextview"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:text="@string/app_name"     android:gravity="center_horizontal"/>      <!--android:textallcaps="true"--> 


2nd activity

@override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_pokemon_detail);      string pokemon = getintent().getextras().getstring("pokemonname");     textview textview = (textview)findviewbyid(r.id.pokemonname);     textview.settext(pokemon); } 

it won't work way. when pokemon_onclick() called, findviewbyid return first occurance of view id pokemontextview. that's why recieving same value in second activity.

the simplest solution implement onclicklistener in viewholder , start 2nd activity directly there.

public class viewholder extends recyclerview.viewholder implements view.onclicklistener {         private imageview pictureimageview;         private textview pokemontextview;      public viewholder(view itemview) {         super(itemview);          itemview.setclickable(true);         itemview.setonclicklistener(this);          pictureimageview = (imageview) itemview.findviewbyid(r.id.pictureimageview);         pokemontextview = (textview) itemview.findviewbyid(r.id.pokemontextview);     }      @override     public void onclick(view v) {         intent intent = new intent(context, pokemondetail.class);         intent.putextra("pokemonname", dataset.get(getadapterposition()).getname());         context.startactivity(intent);     } } 

little bit more complex better solution create interface , handle click callbacks inside activity:

create interface inside adapter , provide setter:

private pokemonclicklistener listener;  public void setlistener(pokemonclicklistener listener) {     this.listener = listener; }  public interface pokemonclicklistener{     void onpokemonclicked(pokemon pokemon); } 

implement onclicklistener in viewholder above, call listener method instead of starting 2nd activity:

@override public void onclick(view v) {     if(listener != null){         listener.onpokemonclicked(dataset.get(getadapterposition()));     } } 

in acitivty, implement pokemonclicklistener , set listener adapter.

adapter.setlistener(new pokemonclicklistener() {     @override     public void onpokemonclicked(pokemon pokemon) {         intent intent = new intent(this, pokemondetail.class);         intent.putextra("pokemonname", pokemon.getname());         startactivity(intent);     } }); 

Comments

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -