Question
How can I compare the string in the array and perform an action to the list item if the string matches? @Override protected void onCreate(Bundle
How can I compare the string in the array and perform an action to the list item if the string matches?
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_list);
listView = findViewById(R.id.listView);
final ArrayList
array.add("MainActivity"); array.add("FirstActivity"); array.add("SecondActivity");
ArrayAdapter
listView.setAdapter(arrayAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView> parent, View view, int position, long id) { if(array.toString().equals("MainActivity")) { Intent mainAct = new Intent(ListActivity.this, MainActivity.class); startActivity(mainAct); }else if(array.toString().equalsIgnoreCase("FirstActivity")) { Intent firstAct = new Intent(ListActivity.this, MainActivity.class); startActivity(firstAct); }else if (array.toString().equalsIgnoreCase("SecondActivity")) { Intent secondAct = new Intent(ListActivity.this, MainActivity.class); startActivity(secondAct); } } });
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started