Dioo B
Dioo B

Reputation: 82

How to pass intent from Adapter class to other activity but my variable is in other activity

I have a problem. I want to pass my intent String "EmailHolder" from "WarehouseActivity" to "ProfilWarehouseActivity" by click on a recyclerview item in my Adapter Class which is named as "WarehouseAdapter". I want to pass the EmailHolder to fill my data when the user updates data inside ProfilWarehouseActivity.

Here is the "Intent" from my "WarehouseActivity" class :

 private String EmailHolder;

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

        Bundle extras = getIntent().getExtras();
        EmailHolder = extras.getString("emailuser");

Here is how I make intent in Adapter Class "WarehouseAdapter" to pass ID data into "ProfilWarehouseActivity" :

@SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull WarehouseAdapter.ViewHolder holder, final int position) {

        holder.namaitem.setText(listItems.get(position).get(Konfigurasi_Warehouse.TAG_WAREHOUSENAMA));
        holder.stockitem.setText("Stock : "+listItems.get(position).get(Konfigurasi_Warehouse.TAG_WAREHOUSESTOCK));
        holder.merekitem.setText("Merek : "+listItems.get(position).get(Konfigurasi_Warehouse.TAG_WAREHOUSEMEREK));
        holder.tglin.setText("Tanggal Masuk : "+listItems.get(position).get(Konfigurasi_Warehouse.TAG_WAREHOUSETGLIN));

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v ) {
                String idItem = listItems.get(position).get(Konfigurasi.TAG_ITEMID);

                passid(idItem);
            }
        });
    }

    private void passid(String idItem) {

        Intent intent = new Intent(context, ProfilWarehouseActivity.class);
        intent.putExtra(Konfigurasi_Warehouse.WAREHOUSE_ID,idItem);

        //i think this is for get my EmailHolder from WarehouseActivity to pass it with Intent from this Adapter class

        context.startActivity(intent);

    }

My question is How to pass Intent with a string "EmailHolder" that contain Intent value through my WarehouseAdapter (Adapter class), but "EmailHolder" is from my WarehouseActivity?

EDIT : Here is how i used the Adapter class from my WarehouseActivity

            final WarehouseAdapter mAdapter = new WarehouseAdapter( WarehouseActivity.this,list);
            mRecyclerView.setLayoutManager(new LinearLayoutManager(WarehouseActivity.this));
            mRecyclerView.setAdapter(mAdapter);

Upvotes: 1

Views: 2157

Answers (4)

Zain
Zain

Reputation: 40858

You can have couple of options to solve this:

Option 1:

If you want to start "ProfilWarehouseActivity" activity from the adapter, then you need to pass the "EmailHolder" from "WarehouseActivity" to your adapter by either its constructor, or a setter:

Passing String into Adapter Constructor

final WarehouseAdapter mAdapter = new WarehouseAdapter( WarehouseActivity.this, list, EmailHolder);

Passing String using a Setter

In "ProfilWarehouseActivity" activity:

mAdapter.setEmailHolder(EmailHolder);

In WarehouseAdapter adapter:

private String mEmailHolder;
public void setEmailHolder(String emailHolder) {
    this.mEmailHolder = emailHolder;
}

....

private void passid(String idItem) {

    Intent intent = new Intent(context, ProfilWarehouseActivity.class);
    intent.putExtra(Konfigurasi_Warehouse.WAREHOUSE_ID, idItem);
    intent.putExtra("EmailHolder", mEmailHolder);

    //i think this is for get my EmailHolder from WarehouseActivity to pass it with Intent from this Adapter class

    context.startActivity(intent);

}

Option 2:

The other option is to create a listener and implement it in "ProfilWarehouseActivity", And pass the itemId as a parameter to the listener callback. Then let your "ProfilWarehouseActivity" call the "WarehouseActivity" instead of the WarehouseAdapter whenever this listener is triggered.

In WarehouseAdapter adapter:

public interface ItemClickListener {
    public void onItemClick(int idItem);
}

ItemClickListener mItemClickListener;

public void setItemClickListener(ItemClickListener listener) {
    mItemClickListener = listener;
}

private void passid(String idItem) {
    if (mItemClickListener != null)
        mItemClickListener.onItemClick(idItem);
}

In "ProfilWarehouseActivity" activity:

class ProfilWarehouseActivity extends AppCompatActivity implements WarehouseAdapter.ItemClickListener { 
    private String EmailHolder;

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

        Bundle extras = getIntent().getExtras();
        EmailHolder = extras.getString("emailuser");

    }

    @Override
    onItemClick(int itemId) {
        Intent intent = new Intent(this, ProfilWarehouseActivity.class);
        intent.putExtra(Konfigurasi_Warehouse.WAREHOUSE_ID, idItem);
        intent.putExtra("EmailHolder", EmailHolder);
        startActivity(intent);        
    }   
    ... 

    final WarehouseAdapter mAdapter = new WarehouseAdapter( WarehouseActivity.this,list);
    mAdapter.setItemClickListener(this);

Upvotes: 2

Sarthak Dhami
Sarthak Dhami

Reputation: 330

For that you have use "interface " You cant directly pass value in Constructor also.

Upvotes: 0

Dioo B
Dioo B

Reputation: 82

i think i accidently found the answer (inspired from the other answer before me, thank you)... i just need to set String "EmailHolder" in "WarehouseActivity" to "public static" :

 public static String EmailHolder;

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

        mRecyclerView = findViewById(R.id.recyclerViewItemWar);

        Bundle extras = getIntent().getExtras();
        EmailHolder = extras.getString("emailuser");

And then use intent from "WarehouseAdapter" like this :

 private void passid(String idItem) {

        Intent intent = new Intent(context, ProfilWarehouseActivity.class);
        intent.putExtra(Konfigurasi_Warehouse.WAREHOUSE_ID,idItem);


        intent.putExtra("emailuser",WarehouseActivity.EmailHolder); //i add this


        context.startActivity(intent);
    }

is it fine to change String from "private" to "public static"..?

Upvotes: 0

SimplyProgrammer
SimplyProgrammer

Reputation: 1819

Pass the emailHolder value to your Adapter while creating your Adapter constructor. If I am not wrong, you might be calling the Adapater from your Activity, so just pass the value as constructor parameter.

wareHouseAdapter = new new WarehouseAdapter( WarehouseActivity.this,list,EmailHolder);

And in the WarehouseAdapter

WarehouseAdapter(otherparameter,String emailHolder) {

    this.emailHolder= emailHolder;
}

And on clicking the recyclerView item

 private void passid(String idItem) {

    Intent intent = new Intent(context, ProfilWarehouseActivity.class);
    intent.putExtra(Konfigurasi_Warehouse.WAREHOUSE_ID,idItem);

    //i think this is for get my EmailHolder from WarehouseActivity to pass it with Intent from this Adapter class

   intent.putExtra("emailHolder",emailHolder);


    context.startActivity(intent);

}

Upvotes: 2

Related Questions