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 = new ArrayList<>();

array.add("MainActivity"); array.add("FirstActivity"); array.add("SecondActivity");

ArrayAdapter arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, array);

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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!