0

I make a onClick in recyclerView onCreateViewHolder like this.

@Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_homeinfo, parent, false);
        final ViewHolder holder = new ViewHolder(itemView);

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(v.getContext(), Integer.toString(holder.getAdapterPosition()), Toast.LENGTH_SHORT).show();
            }
        });
        return holder;
    }

and I want to put this code in onClick.

final String serverURL = "http://youngh.cafe24app.com/qrock/views/qrock_pk/user_email";

                myDb = new DatabaseHelper(v.getContext());  //is it Possible?
                final String token = myDb.getToken();
                final String email = myDb.getEmail();

                StringRequest stringRequest = new StringRequest(Request.Method.GET, serverURL,
                        new Response.Listener<String>() {
                            @Override
                            public void onResponse(String response) {
                                Intent myIntent = new Intent(v.getContext(), mainActivity.class);   //v.getContent() <- ERROR
                                startActivity(myIntent);
                                finish();

                                overridePendingTransition(R.xml.madefadein, R.xml.splashfadeout);
                            }
                        }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        switch(error.networkResponse.statusCode)
                        {

                        }
                    }
                }){
                    @Override
                    protected Map<String, String> getParams() throws AuthFailureError {
                        Map<String, String> params = new HashMap<String, String>();
                        //params.put("qrock_pk", qrockPk);
                        params.put("user_email", email);
                        return params;
                    }

                    @Override
                    public Map<String, String> getHeaders() throws AuthFailureError {
                        Map<String, String> headers = new HashMap<>();
                        headers.put("x-access-token", token);
                        return headers;
                    }
                };
                MySingleton.getInstance(v.getContext()).addToRequestQue(stringRequest);

But in onCreateViewHolder, I can't cast startActivity(), finish() etc. I want to make when I click a recyclerViewItem, start these code and move to another activity. How to use these code???

I'm sorry I can not speak English well.

SunSpike
  • 127
  • 1
  • 1
  • 12
  • You need to pass context to use startActivity(), finish(). so whenever you want to use startActivity() it is call like this context.startActivity(). Yes you can use this code on onClick() – Andy Developer Sep 22 '17 at 06:12
  • Use context and cast and cast context to Activity like this:Activity activity = (Activity) context; – Vijay Chaudhary Sep 22 '17 at 06:12
  • 1
    refer this link : https://stackoverflow.com/questions/28767413/how-to-open-a-different-activity-on-recyclerview-item-onclick – zephyr Sep 22 '17 at 06:13
  • You could also make a callback to the activity that holds the adapter and launch from there. Or better yet trigger an event that the parent activity subscribes to and launch accordingly. – sam_c Sep 22 '17 at 06:37

7 Answers7

2

Try this:

In onCreateViewHolder:

context = v.getContext()

Then go on and use context.startActivity() and ((Activity)context).finish();

Suhayl SH
  • 1,213
  • 1
  • 11
  • 16
  • Can you try with ((Activity)context).finish(); – Suhayl SH Sep 22 '17 at 06:20
  • It works! but Can I declare `DatabaseHelper myDb;` in homeAdapter? and I want to use myDb like this `myDb = new DatabaseHelper(v.getContext()); ` Is it possible? – SunSpike Sep 22 '17 at 06:27
0

Try this code

Intent myIntent= new Intent(v.getContext(), mainActivity.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
v.getContext().startActivity(myIntent);
finish();
Ramesh Yogu
  • 135
  • 7
0

You can use like this;

Intent myIntent = new Intent(getApplicationContext(), mainActivity.class);
getApplicationContext().startActivity(myIntent);
Vishal Vaishnav
  • 3,346
  • 3
  • 26
  • 57
  • starting an activity from application context would require specifics flags like FLAG_NEW_TASK – Re'em Dec 19 '18 at 08:47
0

Instead of onCreateViewHolder do the click function onBindViewHolder. The reason is onBindViewHolder is called everytime add onCreateViewHolder is called only while creating views.

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //use holder.itemView.getContext() to get context
            Toast.makeText( holder.itemView.getContext().getContext(), Integer.toString(holder.getAdapterPosition()), Toast.LENGTH_SHORT).show();
        }
    });
Sharath kumar
  • 4,064
  • 1
  • 14
  • 20
0

you can use like this ((Activity)ctx).startActivity();

Vij
  • 445
  • 2
  • 9
0

You just need to change getContext() to view.getContext();

 @Override
        public void onClick(View view) {
           view.getContext().startActivity(new Intent(view.getContext(), MainActivity.class));    
        }
Ankita
  • 1,129
  • 1
  • 8
  • 15
0

1. onCreateViewHolder

Here you have to do create/initialize your views

2. onBindViewHolder

Here you have to do logics, bind the views , on click actions

Rajasekhar
  • 2,345
  • 1
  • 13
  • 20