Cam
Cam

Reputation: 41

Pass data between Activity and Fragment

I want to pass data from Activity to Fragment using Bundle (I used Bundle to passing data between 2 Fragment, and that's worked). In my Activity I have a ListView, in the OnItemClickListener I want to open a new Fragment, and the Item, which clicked pass through the Activity to this Fragment. But the Item is null, so basically nothing is send to the Fragment.

Here is my Activity:

public class Jegyek extends AppCompatActivity {

    View v;
    DB mydb;
    ListView listView;
    private String teszt;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_jegyek);


    listView = (ListView)findViewById(R.id.jegyekLista);
    mydb = new DB(this);

    final ArrayList<String> thelist = new ArrayList<>();
    Cursor data = mydb.getTantargynev();

    if (data.getCount() == 0) {
        Toast.makeText(this, "Nincs jegyek hozzáadva", Toast.LENGTH_SHORT).show();
    }
    else {
        while (data.moveToNext()) {
            thelist.add(data.getString(0));
            ListAdapter listadapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, thelist);
            listView.setAdapter(listadapter);
        }


        listView.setOnItemClickListener(
                new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                        teszt = thelist.get(i);
                        Bundle bundle = new Bundle();
                        bundle.putString("Tantárgy neve", teszt);
                        Toast.makeText(Jegyek.this, teszt, Toast.LENGTH_SHORT).show();

                        Fragment jegyekAllando = new jegyekAllando();
                        jegyekAllando.setArguments(bundle);
                        FragmentTransaction FragTan = getSupportFragmentManager().beginTransaction();
                        FragTan.replace(R.id.jegyekMenu, jegyekAllando);
                        ListView listaNezet = (ListView) findViewById(R.id.jegyekLista);
                        listaNezet.setVisibility(View.GONE);
                        FragTan.commit();
                    }
                }
        );
    }
}

public String getTanNev () {
    System.out.println("WARNING: "+ teszt);
    return teszt;
}

}

And my Fragment:

    public class jegyekAllando extends Fragment {

        DB mydb;

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {

            View rootView = inflater.inflate(R.layout.fragment_jegyek_allando, container, false);

           Bundle bundle = new Bundle();
            String tanNev = bundle.getStrin
g("Tantárgy neve");
        Jegyek jegyAct = new Jegyek();
        String tanNevv = jegyAct.getTanNev();
        mydb = new DB(getActivity());
        String jegyAtlag =String.valueOf(mydb.JegyekAtlaga(tanNev));
        String jegyDarab =String.valueOf(mydb.JegyekDarabszama(tanNev));

        return rootView;
    }
    }

When I realized, that the bundle is null, I try to pass that List Item through getter, so I changed my Fragment code for that:

 Jegyek jegyek = new Jegyek();
 String jegy = jegyek.getTanNev();

But that's not solved my problem, so I back to the Bundle, which also have some problem, but I can't find it. Intresting, that in the OnItemClickListener when I Toast the Item it's correct and works, but when I want to display in the console or pass to the Fragment, the value's is null.

Upvotes: 1

Views: 102

Answers (2)

Subhasmith Thapa
Subhasmith Thapa

Reputation: 894

From Activity you send data with intent as:

Bundle bundle = new Bundle();
bundle.putString("edttext", "From Activity");
// set Fragmentclass Arguments
Fragmentclass fragobj = new Fragmentclass();
fragobj.setArguments(bundle);

and in Fragment onCreateView method:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    String strtext = getArguments().getString("edttext");    
    return inflater.inflate(R.layout.fragment, container, false);
}

Hope this helps

Upvotes: 1

no_cola
no_cola

Reputation: 1160

In fragment "jegyekAllando" you must get your string from onCreate() with Bundle data = getArguments(), but you create new bundle.

Upvotes: 0

Related Questions