0

Users albums will be opened when the user is clicked. User's website http://jsonplaceholder.typicode.com/users and album's website http://jsonplaceholder.typicode.com/albums. I want to use intent MainActivity from Main2Activity but I have error and i uploaded the picture below.

SimpleRecyclerAdapter.java

public class SimpleRecyclerAdapter extends 
    RecyclerView.Adapter<SimpleRecyclerAdapter.ViewHolder> {

    public static class ViewHolder extends RecyclerView.ViewHolder{
    public TextView isim ;
    public TextView soyIsim ;
    public CardView cardView ;

    public ViewHolder(View itemView) {
        super(itemView);
        isim = itemView.findViewById(R.id.textIsım);
        soyIsim=itemView.findViewById(R.id.textSoyIsim);
        cardView = itemView.findViewById(R.id.cardView);

    }
}


List<PlaceHolderModel> placeHolderModel;
Context context ;

public SimpleRecyclerAdapter(List<PlaceHolderModel> placeHolderModel, Context context) {
    this.placeHolderModel = placeHolderModel;
    this.context=context;
}

@Override
public SimpleRecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_custom,parent,false);
    ViewHolder viewHolder = new ViewHolder(v);
    return  viewHolder ;
}

@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
    holder.isim.setText(placeHolderModel.get(position).name);
    holder.soyIsim.setText(placeHolderModel.get(position).username);
    holder.cardView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //what shall i write
            Intent intent=new Intent(context,Main2Activity.class);
            intent.putExtra("mesaj",placeHolderModel.get(position).id);
            context.startActivity(intent);




        }
    });
}

@Override
public int getItemCount() {
    return placeHolderModel.size();
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
    super.onAttachedToRecyclerView(recyclerView);
}

}

Main2Activity.java

public class Main2Activity extends AppCompatActivity {

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

    Intent intent =getIntent();
    Toast.makeText(this, String.valueOf(intent.getIntExtra("mesaj", 0)), Toast.LENGTH_SHORT).show();
    Factory.getInstance().placeAlbums(intent.getIntExtra("mesaj", 0)).enqueue(new Callback<List<PlaceHolderModel>>() {
        @Override
        public void onResponse(Call<List<PlaceHolderModel>> call, Response<List<PlaceHolderModel>> response) {

        }

        @Override
        public void onFailure(Call<List<PlaceHolderModel>> call, Throwable t) {

        }
    });

}
 }

ServiceGenerator.java

public interface ServiceGenerator {
@GET("/users")
Call<List<PlaceHolderModel>> placeHolderModel();

@GET("/albums")
Call<List<PlaceHolderModel>> placeAlbums(@Query("userId") int userId);


}

enter image description here

Your advice important for me

Thank you very much !

David Wasser
  • 93,459
  • 16
  • 209
  • 274
Yavuz
  • 129
  • 1
  • 3
  • 17
  • Use `MainActivity.this` for the `Context` when instantiating your `SimpleRecyclerAdapter`. Do not use `getApplicationContext()`, `getBaseContext()`, etc. Also, please don't post screenshots of your logcat. Please post all text as text. – Mike M. Aug 17 '17 at 06:08
  • @Yavuz provide your mainActivity as well – Ashwani Aug 17 '17 at 06:14
  • I dont one more picture sharing. I understand what you mean and i wrote Context but i am getting an error. My MainActiviity.java is below – Yavuz Aug 17 '17 at 06:15
  • Unfortunately it answer not working – Yavuz Aug 17 '17 at 06:33
  • My code is working. I wrote MainActivity.this , thanks – Yavuz Aug 17 '17 at 06:38

0 Answers0