1. Download our Official Android App: Forums for Android!

Cannot resolve method...startActivity (Intent) little problem

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

  1. carelv7

    carelv7 Newbie
    Thread Starter
    Rank:
    None
    Points:
    16
    Posts:
    18
    Joined:
    Jul 4, 2017

    Jul 4, 2017
    18
    1
    16
    Hi, hope you're doing good. I'm having a slight problem starting an intent. With the startActivity it tells me: "Cannot resolve method 'startActivity(android.....). How should I start that activity.


    String orderMessage = createOrderSummary(naam, hoeveel);
    }
    private String createOrderSummary(String naam, String hoeveel) {
    String orderMessage = "Brand" + naam;
    orderMessage += "Quantity" + hoeveel;
    return orderMessage;

    Intent intent4 = new Intent(ctx.getApplicationContext(), BlackLabel.class);
    intent4.putExtra("key", orderMessage);
    startActivity(intent4);
    }

    Here is the code of the whole adapter.

    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) modelArrayList.get(position).getBrand());
    holder.tvImage.setImageResource(Integer.parseInt(String.valueOf(modelArrayList.get(position).getImage())));
    holder.tvCases.setText(String.valueOf(modelArrayList.get(position).getNumber()));
    holder.tvPallet_size.setText(String.valueOf(modelArrayList.get(position).getPallet_size()));
    holder.tvCases.setText(String.valueOf(modelArrayList.get(position).getNumber2()));
    holder.tvPallets.setText(String.valueOf(modelArrayList.get(position).getNumber()));
    holder.tvCases.setText(String.valueOf(modelArrayList.get(position).getNumber2()));
    }

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

    class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

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

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

    tvBrand = (TextView) itemView.findViewById(R.id.brand_name);
    tvImage = (ImageView) itemView.findViewById(R.id.beer_logo);
    tvCases = (TextView) itemView.findViewById(R.id.cases_text_view);
    tvPallet_size = (TextView) itemView.findViewById(R.id.pallets_size);
    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);

    }

    public void submitOrder(View view) {
    TextView textName = (TextView) view.findViewById(R.id.brand_name);
    String naam = textName.getText().toString();
    TextView textQuant = (TextView) view.findViewById(R.id.cases_text_view);
    String hoeveel = textQuant.getText().toString();

    String orderMessage = createOrderSummary(naam, hoeveel);
    }
    private String createOrderSummary(String naam, String hoeveel) {
    String orderMessage = "Brand" + naam;
    orderMessage += "Quantity" + hoeveel;
    return orderMessage;

    Intent intent4 = new Intent(ctx.getApplicationContext(), BlackLabel.class);
    intent4.putExtra("key", orderMessage);
    startActivity(intent4);
    }

    // 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 tvCases = (TextView) tempview.findViewById(R.id.cases_text_view);
    int number = Integer.parseInt(tvCases.getText().toString()) + 1;
    tvCases.setText(String.valueOf(number));
    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 tvCases = (TextView) tempview.findViewById(R.id.cases_text_view);
    int number = Integer.parseInt(tvCases.getText().toString()) - 1;
    if (number == 0 || number <0) {
    Toast.makeText(ctx,"You cannot order less than one case", Toast.LENGTH_SHORT).show();
    tvCases.setText(String.valueOf(0));
    return;
    }
    tvCases.setText(String.valueOf(number));
    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 tvPallets = (TextView) tempview.findViewById(R.id.pallets_text_view);
    TextView tvCases = (TextView) tempview.findViewById(R.id.cases_text_view);
    TextView tvPallet_size = (TextView) tempview.findViewById(R.id.pallets_size);
    int number = Integer.parseInt(tvPallets.getText().toString()) + 1;
    tvPallets.setText(String.valueOf(number));
    int number2 = Integer.parseInt(tvPallets.getText().toString()) * Integer.parseInt(tvPallet_size.getText().toString());
    tvCases.setText(String.valueOf(number2));
    modelArrayList.get(getAdapterPosition()).setNumber(number);
    modelArrayList.get(getAdapterPosition()).setNumber2(number2);

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

    View tempview = (View) btn_pallets_minus.getTag(R.integer.btn_pallets_minus_view);
    TextView tvPallets = (TextView) tempview.findViewById(R.id.pallets_text_view);
    TextView tvCases = (TextView) tempview.findViewById(R.id.cases_text_view);
    TextView tvPallet_size = (TextView) tempview.findViewById(R.id.pallets_size);
    int number = Integer.parseInt(tvPallets.getText().toString()) - 1;
    tvPallets.setText(String.valueOf(number));
    int number2 = Integer.parseInt(tvPallets.getText().toString()) * Integer.parseInt(tvPallet_size.getText().toString());
    tvCases.setText(String.valueOf(number2));
    if (number == 0 || number < 0) {
    Toast.makeText(ctx, "You cannot order less than one pallet", Toast.LENGTH_SHORT).show();
    tvCases.setText(String.valueOf(0));
    tvPallets.setText(String.valueOf(0));
    }
    modelArrayList.get(getAdapterPosition()).setNumber(number);
    modelArrayList.get(getAdapterPosition()).setNumber2(number2);

    }
    }}}}

    It's about in the middle where I where I create an intent to use the data of two field in another class. This is the adapter.

     

    Advertisement

  2. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #11
    Points:
    1,988
    Posts:
    7,842
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    7,842
    11,439
    1,988
    Male
    Software developer
    South West of England
    You can't place this code in your Adaptor

    Code (Text):
    1.  
    2. Intent intent4 = new Intent(ctx.getApplicationContext(), BlackLabel.class);
    3. intent4.putExtra("key", orderMessage);
    4. startActivity(intent4);
    5.  
    The reason you get the error is because startActivity() is a method in the Activity class. It can only be invoked from a class which extends Activity.
     
  3. carelv7

    carelv7 Newbie
    Thread Starter
    Rank:
    None
    Points:
    16
    Posts:
    18
    Joined:
    Jul 4, 2017

    Jul 4, 2017
    18
    1
    16
    thank you, but please help. How am I going to get the two values (
    R.id.brand_name &
    R.id.cases_text_view) from my adapter into an email intent which I have on my MainActivity? The bottom navigation with the email intent on one of the buttons is on the main activity.
     
  4. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #11
    Points:
    1,988
    Posts:
    7,842
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    7,842
    11,439
    1,988
    Male
    Software developer
    South West of England
  5. carelv7

    carelv7 Newbie
    Thread Starter
    Rank:
    None
    Points:
    16
    Posts:
    18
    Joined:
    Jul 4, 2017

    Jul 4, 2017
    18
    1
    16
    Thank you very much.
     

Share This Page

Loading...