no suitable method for for make.Text

carelv7

Newbie
Hi there. apart from a little help with the code, I just need someone to tell me to keep going. It shouldn't be that difficult, but I just sometimes get stuck with something so small. I'm trying to make a simple toast message, and get a error about no suitable method found for makeText.

Here is the CustomAdapter:
Java:
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.MyViewHolder> {

    private LayoutInflater inflater;
    private Context ctx;

    public CustomAdapter(Context ctx) {

        inflater = LayoutInflater.from(ctx);
        this.ctx = ctx;
    }

    @Override
    public CustomAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = inflater.inflate(R.layout.rv_item, parent, false);
        MyViewHolder holder = new MyViewHolder(view);

        return holder;
    }

    @Override
    public void onBindViewHolder(final CustomAdapter.MyViewHolder holder, int position) {

        holder.tvBrand.setText((CharSequence) MainActivity.modelArrayList.get(position).getBrand());
        holder.tvCases.setText(String.valueOf(MainActivity.modelArrayList.get(position).getNumber()));
        holder.tvPallets.setText(String.valueOf(MainActivity.modelArrayList.get(position).getNumber()));

    }

    @Override
    public int getItemCount() {
        return MainActivity.modelArrayList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

        protected Button btn_cases_plus, btn_cases_minus, btn_pallets_plus, btn_pallets_minus;
        private TextView tvBrand, tvCases,tvPallets;

        public MyViewHolder(View itemView) {
            super(itemView);

            tvBrand = (TextView) itemView.findViewById(R.id.brand_name);
            tvCases = (TextView) itemView.findViewById(R.id.cases_text_view);
            tvPallets = (TextView) itemView.findViewById(R.id.pallets_text_view);
            btn_cases_plus = (Button) itemView.findViewById(R.id.casePlus1);
            btn_cases_minus = (Button) itemView.findViewById(R.id.caseMinus1);
            btn_pallets_plus = (Button) itemView.findViewById(R.id.palletsPlus1);
            btn_pallets_minus = (Button) itemView.findViewById(R.id.palletsMinus1);

            btn_cases_plus.setTag(R.integer.btn_cases_plus_view, itemView);
            btn_cases_minus.setTag(R.integer.btn_cases_minus_view, itemView);
            btn_cases_plus.setOnClickListener(this);
            btn_cases_minus.setOnClickListener(this);
            btn_pallets_plus.setTag(R.integer.btn_pallets_plus_view, itemView);
            btn_pallets_minus.setTag(R.integer.btn_pallets_minus_view, itemView);
            btn_pallets_plus.setOnClickListener(this);
            btn_pallets_minus.setOnClickListener(this);

        }

        // onClick Listener for view
        @Override
        public void onClick(View v) {

            if (v.getId() == btn_cases_plus.getId()){

                View tempview = (View) btn_cases_plus.getTag(R.integer.btn_cases_plus_view);
                TextView tv = (TextView) tempview.findViewById(R.id.cases_text_view);
                int number = Integer.parseInt(tv.getText().toString()) + 1;
                tv.setText(String.valueOf(number));
                MainActivity.modelArrayList.get(getAdapterPosition()).setNumber(number);

            } else if(v.getId() == btn_cases_minus.getId()) {

                View tempview = (View) btn_cases_minus.getTag(R.integer.btn_cases_minus_view);
                TextView tv = (TextView) tempview.findViewById(R.id.cases_text_view);
                int number = Integer.parseInt(tv.getText().toString()) - 1;
                if (number == 0 || number <0) {
                    Toast.makeText(this,"You cannot order less than one case", Toast.LENGTH_SHORT).show();
                    return;
                }
                tv.setText(String.valueOf(number));
                MainActivity.modelArrayList.get(getAdapterPosition()).setNumber(number);

            }  else if(v.getId() == btn_pallets_plus.getId()) {

                View tempview = (View) btn_pallets_plus.getTag(R.integer.btn_pallets_plus_view);
                TextView tv = (TextView) tempview.findViewById(R.id.pallets_text_view);
                int number = Integer.parseInt(tv.getText().toString()) + 1;

                tv.setText(String.valueOf(number));
                MainActivity.modelArrayList.get(getAdapterPosition()).setNumber(number);

            }   else if(v.getId() == btn_pallets_minus.getId()) {

                View tempview = (View) btn_pallets_minus.getTag(R.integer.btn_pallets_minus_view);
                TextView tv = (TextView) tempview.findViewById(R.id.pallets_text_view);
                int number = Integer.parseInt(tv.getText().toString()) - 1;
                tv.setText(String.valueOf(number));
                MainActivity.modelArrayList.get(getAdapterPosition()).setNumber(number);
            }

        }

    }
}

Looks like it is about on line 80.
Thank you so far for everybody's help on the forum.
 
D

Deleted User

Guest
With a problem like this, your first line of help is the online reference documentation. This site has details of every single class and method in the Android Java framework:

https://developer.android.com/reference/android/widget/Toast.html

Question: What do you think 'this' refers to in the following line? And more importantly, what type is it? If you don't understand the concept of the 'this' keyword, then I would strongly recommend that you review the basics of the Java language.

Code:
Toast.makeText(this,"You cannot order less than one case", Toast.LENGTH_SHORT).show();
 

scary alien

not really so scary
^^^ :thumbsup: as always, @LV426 :)

Ah, I think I've run into this before myself...

Sorry, don't have access to my development machine at the moment, but if memory serves, there are contexts in which you are not allowed to call things like the Toast method (like inside the onClick method you have it in). I'm (still) fuzzy myself on the ins and outs of the "this" context and where/when things are allowed...

I got around that by calling a custom method that called/invoked the Toast for me (passing the suitable string, of course).

Hope that's not too much bad form or hackiness--but it worked for me :).
 
D

Deleted User

Guest
Thanks @scary alien. Well the compile problem is due to that first parameter on the makeText method. You can't use 'this', because 'this' refers to the current class type - CustomAdapter. But the method requires an object of type Context. As it happens, the class contains a member variable called ctx, so that can be used.
 

carelv7

Newbie
Thread starter
Thank you very much you guys. I thought that "this" would work in the onClick Method. Did know I can't use it in an onClick method. I'm still learning. Thought I knew the basics, but stuff like this makes me end up here, but thanks to people like you two guys I'm learning everyday. Thank you again for your patience.

By the way, using the member variable ctx worked.
 
D

Deleted User

Guest
Cool. It's all about gaining experience. Takes time.
Btw I think what @scary alien was alluding to, was the fact that you are not allowed to update UI components in any thread other than the main Activity thread. So for example if you'd created another thread to handle network activity, then that thread may not update any UI component.
But that's another issue.
 
Top