Issue
I'm new to Android and I'm trying to create simple app in Android Studio. Basically listview of some bigger towns, after clicking on any I would like to create an activity and based on which item was clicked, I'd like to set text of textview of new activity on some basic text like "One", or town description e.g. The problem is setting text, it simply doesn't work and I can't really figure out why. The id passed to t(Textview) is the id from new Acticity (its textview)
public class MainActivity extends AppCompatActivity {
private ListView list;
private ArrayAdapter<String> adapter;
private AdapterView.OnItemClickListener myClick = new AdapterView.OnItemClickListener() {
@SuppressLint("SetTextI18n")
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(view.getContext(), newActivity.class);
startActivity(intent);
TextView t = (TextView) findViewById(R.id.myTV);
if (i == 0) {
t.setText("Zero");
} else if (i == 1) {
t.setText("One");
} else if (i == 2) {
t.setText("Two");
} else if (i == 3) {
t.setText("Three");
} else {
t.setText("...");
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView) findViewById(R.id.listView);
String[] m = {"Rome", "Florence", "Paris", "Edinburgh",
"London", "Prague", "Venice", "Brisbane", "Kyoto", "Rio de Janeiro"};
ArrayList<String> towns = new ArrayList<>(Arrays.asList(m));
adapter = new ArrayAdapter<>(this, R.layout.element, towns);
list.setAdapter(adapter);
list.setOnItemClickListener(myClick);
}
}
Solution
The reason the TextView is not showing any text in the target Activity (newActivity.class
) is because the text data to be shown in the TextView has to be passed from the exiting Activity (MainActivity
) using a Bundle object to the Intent that starts the NewActivity
.
I would assume that variable 't' is supposed to be the TextView of newActivity.class
and not MainActivity
.
That would mean you would have to move it's initialization code to the onCreate()
of NewActivity
.
It should not be in the onItemClick()
of the MainActivity
.
TextView t = (TextView) findViewById(R.id.myTV);
The next step you would want to take is to create a Bundle
object in the onItemClick()
and pass the relevant text information to the target Activity. This is one way to do so:
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(view.getContext(), newActivity.class);
Bundle txtBundle = new Bundle();
if (i == 0) {
txtBundle.putString("text", "Zero");
} else if (i == 1) {
txtBundle.putString("text", "One");
} else if (i == 2) {
txtBundle.putString("text", "Two");
} else if (i == 3) {
txtBundle.putString("text", "Three");
} else {
txtBundle.putString("text", "...");
}
intent.putExtras(txtBundle);
startActivity(intent);
}
Then proceed to the onCreate()
of the NewActivity
and retrieve the Bundle that was added to the Intent that started it like so:
public class NewActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_activity_layout);
TextView t = (TextView) findViewById(R.id.myTV);
Bundle txtBundle = getIntent().getExtras();
String text = txtBundle.getString("text");
t.setText(text);
}
Hopefully this should solve the problem.
Answered By - IODevBlue
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.