I have a couple listview items and I need when I click on particular listview to go to Fragment_2 with a value of listview that is clicked. I try code below on Fragment_1 I get the proper value an it show in Tosat notification, but in Fragment_2 bundle is always null.
In onCreate method in Fragment_1
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String value = ((TextView) view.findViewById(R.id.username)).getText().toString();
MainFragment fragment = new MainFragment ();
Bundle bundle = new Bundle();
bundle.putString("view", value);
Toast.makeText(getApplicationContext(), value, Toast.LENGTH_SHORT).show();
fragment.setArguments(bundle);
In onCreate method in Fragment_2
Bundle bundle = this.getArguments();
if (bundle != null) {
String myString = bundle.getString("view");
Toast.makeText(getContext(), myString, Toast.LENGTH_SHORT).show();
UDPATE:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String value = ((TextView) view.findViewById(R.id.username)).getText().toString();
MainFragment fragment = new MainFragment ();
Bundle bundle = new Bundle();
bundle.putString("view", value);
Toast.makeText(getApplicationContext(), value, Toast.LENGTH_SHORT).show();
fragment.setArguments(bundle);
if (position == 0) {
Intent myIntent = new Intent(view.getContext(), MainActivity.class);
startActivityForResult(myIntent, 0);
}
if (position == 1) {
Intent myIntent = new Intent(view.getContext(), MainActivity.class);
startActivityForResult(myIntent, 0);
}
if (position == 2) {
Intent myIntent = new Intent(view.getContext(), MainActivity.class);
startActivityForResult(myIntent, 0);
}
if (position == 3) {
Intent myIntent = new Intent(view.getContext(), MainActivity.class);
startActivityForResult(myIntent, 0);
}
// ETC .......
}
});