Home > Software engineering >  Int passed into Intent but Android studio is saying that I passed a String instead
Int passed into Intent but Android studio is saying that I passed a String instead

Time:10-23

I pasased an int array into my intent but Android studio is saying :

error: method getIntExtra in class Intent cannot be applied to given types; img = getIntent().getIntExtra("myImg"); ^ required: String,int found: String reason: actual and formal argument lists differ in length

String int found: String reason: actual and formal argument lists differ in length

The Adapter Code:

public class myAdapter extends RecyclerView.Adapter<myAdapter.MyViewHolder> {


String names[],descriptions[];
Context context;
int images[];


public myAdapter(Context ct, String nm[], String des[], int img[] ){
    names=nm;
    descriptions=des;
    images=img;
    context=ct;
}


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



      LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.recycle_row, parent, false);
        return new MyViewHolder(view);
    }

    //
    @Override
    public void onBindViewHolder(@NonNull myAdapter.MyViewHolder holder,int position) {
//Setting the text to the textView from the names array
        holder.resTitle.setText(names[position]);
        holder.myImg.setImageResource(images[position]);
        //onclick listener for the rows
        holder.mylayout.setOnClickListener(new View.OnClickListener() {
            @Override
            //might have to use getContext Here
            public void onClick(View v) {
                Intent intent = new Intent(context, cuisineInformationActivity.class);
                intent.putExtra("Name", names[position]);
                intent.putExtra("Description", descriptions[position]);
                intent.putExtra("myImg", images[position]);
                context.startActivity(intent);

            }

The Activity class:

    //this activity will react to a click on the recycleview in cuisineFragment and will show fruther information about that particular restaurant
public class cuisineInformationActivity extends AppCompatActivity {


    ImageView Imageview;
    TextView name,description;
    String nm, ds;
    int img;


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

        Imageview = findViewById(R.id.restActImgview);
        name = findViewById(R.id.restActName);
        description = findViewById(R.id.restActDesc);

        getData();
        setData();


    }

//getting the data from intent
    private void getData() {
        if (getIntent().hasExtra("Name") && getIntent().hasExtra("Description") && getIntent().hasExtra("myImg") ){
            nm = getIntent().getStringExtra("Name");
            ds = getIntent().getStringExtra("Description");
            img = getIntent().getIntExtra("myImg");

        } else{
            Toast.makeText(this,"No data was found",Toast.LENGTH_SHORT).show();
        }

CodePudding user response:

getIntExtra() takes two parameters:

  • The name of the extra
  • A default value to return if the extra is missing

You are only providing one parameter: the name. So, add a second parameter, which is your default value, such as img = getIntent().getIntExtra("myImg", -1);.

  • Related