Reputation: 305
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 .......
}
});
Upvotes: 1
Views: 295
Reputation: 2321
Just replace below code in onItemClick method.
String value = ((TextView)
view.findViewById(R.id.username)).getText().toString();
Bundle bundle = new Bundle();
bundle.putString("view", value);
Intent myIntent = new Intent(view.getContext(), MainActivity.class);
myIntent.putExtra(bundle);
startActivityForResult(myIntent, 0);
On MainActivity replace your MainFragment like this,
MainFragment fragment = new MainFragment ();
fragment.setArguments(getIntent().getExtras());
getSupportFragmentManager()
.beginTransaction()
.replace("your fragment container id here", fragment)
.commit();
Upvotes: 1
Reputation: 116
You are calling activity so how can you get the data in the fragment and if you are doing like this :
First you are launching the activity by this
Intent myIntent = new Intent(view.getContext(), MainActivity.class);
startActivityForResult(myIntent, 0);
Then in this activity you are launching the fragment
getFragmentManager().beginTransaction().replace("your fragment container id here",
fragment).commit();
Than in this case pass the data to the intent first like this
Intent myIntent = new Intent(view.getContext(), MainActivity.class);
myIntent.putString("data","Put the data which you want to pass");
startActivityForResult(myIntent, 0);
and pass this data to the fragment by using bundles Hope it will work for you .
Upvotes: 0
Reputation: 1702
you are not committing fragment
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);
// This line is important
getSupportFragmentManager().beginTransaction().add(R.id.container, fragment).commit();
Upvotes: 1
Reputation: 314
You are starting the Activity "MainActivity.class" and passing your bundle to a Fragment class "MainFragment" but never actually starting the MainFragment. So, it is obvious that your code won't work
Upvotes: 0