1. Are you ready for the Galaxy S20? Here is everything we know so far!

no suitable method for for make.Text

Discussion in 'Android Development' started by carelv7, Aug 8, 2017.

  1. carelv7

    carelv7 Newbie
    Thread Starter

    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:
    Code (Java):
    1. public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.MyViewHolder> {
    2.  
    3.     private LayoutInflater inflater;
    4.     private Context ctx;
    5.  
    6.     public CustomAdapter(Context ctx) {
    7.  
    8.         inflater = LayoutInflater.from(ctx);
    9.         this.ctx = ctx;
    10.     }
    11.  
    12.     @Override
    13.     public CustomAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    14.  
    15.         View view = inflater.inflate(R.layout.rv_item, parent, false);
    16.         MyViewHolder holder = new MyViewHolder(view);
    17.  
    18.         return holder;
    19.     }
    20.  
    21.     @Override
    22.     public void onBindViewHolder(final CustomAdapter.MyViewHolder holder, int position) {
    23.  
    24.         holder.tvBrand.setText((CharSequence) MainActivity.modelArrayList.get(position).getBrand());
    25.         holder.tvCases.setText(String.valueOf(MainActivity.modelArrayList.get(position).getNumber()));
    26.         holder.tvPallets.setText(String.valueOf(MainActivity.modelArrayList.get(position).getNumber()));
    27.  
    28.     }
    29.  
    30.     @Override
    31.     public int getItemCount() {
    32.         return MainActivity.modelArrayList.size();
    33.     }
    34.  
    35.     class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
    36.  
    37.         protected Button btn_cases_plus, btn_cases_minus, btn_pallets_plus, btn_pallets_minus;
    38.         private TextView tvBrand, tvCases,tvPallets;
    39.  
    40.         public MyViewHolder(View itemView) {
    41.             super(itemView);
    42.  
    43.             tvBrand = (TextView) itemView.findViewById(R.id.brand_name);
    44.             tvCases = (TextView) itemView.findViewById(R.id.cases_text_view);
    45.             tvPallets = (TextView) itemView.findViewById(R.id.pallets_text_view);
    46.             btn_cases_plus = (Button) itemView.findViewById(R.id.casePlus1);
    47.             btn_cases_minus = (Button) itemView.findViewById(R.id.caseMinus1);
    48.             btn_pallets_plus = (Button) itemView.findViewById(R.id.palletsPlus1);
    49.             btn_pallets_minus = (Button) itemView.findViewById(R.id.palletsMinus1);
    50.  
    51.             btn_cases_plus.setTag(R.integer.btn_cases_plus_view, itemView);
    52.             btn_cases_minus.setTag(R.integer.btn_cases_minus_view, itemView);
    53.             btn_cases_plus.setOnClickListener(this);
    54.             btn_cases_minus.setOnClickListener(this);
    55.             btn_pallets_plus.setTag(R.integer.btn_pallets_plus_view, itemView);
    56.             btn_pallets_minus.setTag(R.integer.btn_pallets_minus_view, itemView);
    57.             btn_pallets_plus.setOnClickListener(this);
    58.             btn_pallets_minus.setOnClickListener(this);
    59.  
    60.         }
    61.  
    62.         // onClick Listener for view
    63.         @Override
    64.         public void onClick(View v) {
    65.  
    66.             if (v.getId() == btn_cases_plus.getId()){
    67.  
    68.                 View tempview = (View) btn_cases_plus.getTag(R.integer.btn_cases_plus_view);
    69.                 TextView tv = (TextView) tempview.findViewById(R.id.cases_text_view);
    70.                 int number = Integer.parseInt(tv.getText().toString()) + 1;
    71.                 tv.setText(String.valueOf(number));
    72.                 MainActivity.modelArrayList.get(getAdapterPosition()).setNumber(number);
    73.  
    74.             } else if(v.getId() == btn_cases_minus.getId()) {
    75.  
    76.                 View tempview = (View) btn_cases_minus.getTag(R.integer.btn_cases_minus_view);
    77.                 TextView tv = (TextView) tempview.findViewById(R.id.cases_text_view);
    78.                 int number = Integer.parseInt(tv.getText().toString()) - 1;
    79.                 if (number == 0 || number <0) {
    80.                     Toast.makeText(this,"You cannot order less than one case", Toast.LENGTH_SHORT).show();
    81.                     return;
    82.                 }
    83.                 tv.setText(String.valueOf(number));
    84.                 MainActivity.modelArrayList.get(getAdapterPosition()).setNumber(number);
    85.  
    86.             }  else if(v.getId() == btn_pallets_plus.getId()) {
    87.  
    88.                 View tempview = (View) btn_pallets_plus.getTag(R.integer.btn_pallets_plus_view);
    89.                 TextView tv = (TextView) tempview.findViewById(R.id.pallets_text_view);
    90.                 int number = Integer.parseInt(tv.getText().toString()) + 1;
    91.  
    92.                 tv.setText(String.valueOf(number));
    93.                 MainActivity.modelArrayList.get(getAdapterPosition()).setNumber(number);
    94.  
    95.             }   else if(v.getId() == btn_pallets_minus.getId()) {
    96.  
    97.                 View tempview = (View) btn_pallets_minus.getTag(R.integer.btn_pallets_minus_view);
    98.                 TextView tv = (TextView) tempview.findViewById(R.id.pallets_text_view);
    99.                 int number = Integer.parseInt(tv.getText().toString()) - 1;
    100.                 tv.setText(String.valueOf(number));
    101.                 MainActivity.modelArrayList.get(getAdapterPosition()).setNumber(number);
    102.             }
    103.  
    104.         }
    105.  
    106.     }
    107. }
    Looks like it is about on line 80.
    Thank you so far for everybody's help on the forum.

     


  2. Best Answer:
    Post #4 by Deleted User, Aug 8, 2017 (1 points)

    1. Download the Forums for Android™ app!


      Download

       
  3. Deleted User

    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 (Text):
    1.  
    2. Toast.makeText(this,"You cannot order less than one case", Toast.LENGTH_SHORT).show();
    3.  
     
    scary alien likes this.
  4. scary alien

    scary alien not really so scary
    VIP Member

    ^^^ :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 :).
     
  5. Deleted User

    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.
     
    scary alien likes this.
  6. carelv7

    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.
     
    scary alien likes this.
  7. Deleted User

    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.
     
    sweetndreemy73 and scary alien like this.
Loading...
Similar Threads - suitable method Text
  1. Mtn Drew
    Replies:
    5
    Views:
    455
  2. kamieniarzk
    Replies:
    0
    Views:
    486
  3. katesfb
    Replies:
    0
    Views:
    635
  4. tapio
    Replies:
    5
    Views:
    640
  5. Anand Suthar
    Replies:
    1
    Views:
    1,142
  6. alisyed
    Replies:
    1
    Views:
    346
  7. AlinTecsan
    Replies:
    1
    Views:
    1,428
  8. JohnJSal
    Replies:
    9
    Views:
    1,816
  9. consultant
    Replies:
    2
    Views:
    616

Share This Page

Loading...