frans ringo
frans ringo

Reputation: 1

How to pass String and Image value to my other fragment?

I want to pass my String and Image value to my another Fragment.

Here is my value (These value is called from my Fragment Home)

private GridView grid;

    String[] nama_barang_rek = {
"Batu Bata",
"Semen Holcim",
"Cat Avitex",
"Toilet Duduk TOTO"};

    int[] gambar_barang_rek = {
R.drawable.batu_bata,
R.drawable.semen_holcim,
R.drawable.cat_avitex,
R.drawable.closet_duduk_toto};   

And these a the code for my GridView, and i wanna pass those value to my CheckOutActivity

grid = (GridView) rootview.findViewById(R.id.gv_rekomendasi);
        grid.setAdapter(new CustomGrid(getActivity(), nama_barang_rek, gambar_barang_rek));
        grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(getActivity(), nama_barang_rek[position], Toast.LENGTH_SHORT).show();
            }
        });

I hope u guys could help me to fix my problem.

Upvotes: 0

Views: 957

Answers (2)

albeee
albeee

Reputation: 1472

Sending side

Fragment frag = new Fragment();
Bundle data = new Bundle();
data.putSting("key_name", value);
data.putInt("key_name", value);
frag.setArguments(data);
getSupportFragmentManager().beginTransaction()
.replace(R.id.holder, frag).Commit();

Receiving side

onCreateView() {
     Bundle data = getArguments():
     If (data != null){
         // read here
     }
}

Upvotes: 1

vimal raj
vimal raj

Reputation: 295

if you are passing value from activity to fragment use following code before Toast message inside onItemclick

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    YourFragment mFragment = new YourFragment();
    Bundle bundle = new Bundle();
    bundle.putString("strValue", nama_barang_rek[position]);
    bundle.putInt("intValue",gambar_barang_rek[position]);
    mFragment.setArguments(bundle);
    ft.replace(R.id.content_frame, mFragment);

where R.id.content_frame is the framelayout in xml file if you are passing value from activity to activity use following code before Toast message inside onItemclick

Intent i=new Intent(firstActivity.this,secondActivity.class);
i.putString("strValue", nama_barang_rek[position]);
    i.putInt("intValue",gambar_barang_rek[position]);
startActivity(i);

Upvotes: 1

Related Questions