Reputation: 88
:)
I am trying to create an application that shows a ListFragment. When one of the items in the ListFragment is clicked by the user it changes the text in the TextView of an another activity.
I have tried this approach so far:
(This is in the Fragment activity, item is a static string)
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
item = ((TextView) v).getText().toString();
Intent intent = new Intent(AboutFragment.this.getActivity(),Shower.class);
startActivity(intent);
}
}
(This is in the empty activity that takes the item's string and uses it to determine what to show on the TextView)
TextView texter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shower);
texter = (TextView) findViewById(R.id.text);
Setter();
}
public void Setter(){
switch (AboutFragment.item.toString()) {
case "moon": texter.setText("Mortal");
case "blue":texter.setText("Job");
}
}
Currently it only shows the word "Job", it doesnt change when I click the other word. I tried using the onResume, but to no avail.
Thanks for any help provided. :)
Upvotes: 0
Views: 114
Reputation: 20646
Why you don't use putExtras()
? Maybe it will do what you want easily.
1- First of all the onListItemClick()
should be like :
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
String text = l.getItemAtPosition(position).toString();
Intent intent = new Intent(getActivity(),Shower.class);
intent.putExtra("KeyString", text);
getActivity().startActivity(intent);
}
2- In yourempty Activity
you should do this :
TextView texter;
String myText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shower);
texter = (TextView) findViewById(R.id.text);
Bundle extras = getIntent().getExtras();
if (extras != null) {
myText = extras.getString("KeyString");
}
Setter();
}
public void Setter(){
switch (myText) {
case "moon": texter.setText("Mortal");
break;
case "blue":texter.setText("Job");
break;
}
}
Hope it helps... I tried to understan your problem so let me know if it's what you want.
Upvotes: 2
Reputation: 12524
You need a break statement to terminate the execution of the switch. Without it, the "moon" case executes, immediately followed by execution of the "job" case which results in the behavior you describe.
switch (AboutFragment.item.toString()) {
case "moon": texter.setText("Mortal");
break;// add this or else the next case will execute
case "blue":texter.setText("Job");
break;// you should add this too
}
Upvotes: 2