I'm looking to create a list of over 100 entries/activities. Upon clicking any entry of the list, an activity is supposed to open, which I managed to do like the following:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(position==0){
Intent intent0 = new Intent(view.getContext(), first.class);
startActivityForResult(intent0,0);
}
else if(position==1){
Intent intent1 = new Intent(view.getContext(), second.class);
startActivityForResult(intent1,0);
}
But to make navigation through the list easier, I sorted the list alpabetically via:
Collections.sort(ListOfObjects, new Comparator<String[]>() {
@Override
public int compare(String[] o1, String[] o2) {
return o1[0].compareToIgnoreCase(o2[0]);
}
});
This results in the problem, that upon clicking an entry, regardless of what entry, the activity linked to the position is opened, but not the activity linked to the entry. So my question is how to open a specific activity, based on the clicked entry/name of the entry, and not based on the location of the entry? I'd be happy for any help.
REST OF CODE:
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ListView listView = findViewById(R.id.Objektliste);
searchView = findViewById(R.id.Searchview);
final ArrayList<String[]> ListOfObjects = new ArrayList<>();
arrayAdapter = new ArrayAdapter<String[]>(this, android.R.layout.simple_list_item_2, android.R.id.text1, ListOfObjects){
// Code for subitem
@Override
public View getView(int position, View convertView, ViewGroup parent){
View view = super.getView(position, convertView, parent);
String[] entry = ListOfObjects.get(position);
TextView text1 = view.findViewById(android.R.id.text1);
TextView text2 = view.findViewById(android.R.id.text2);
text1.setText(entry[0]);
text2.setText(entry[1]);
return view;
}
};
listView.setAdapter(arrayAdapter);
//a searchbar/filter which doesnt work either but thats for another day
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
arrayAdapter.getFilter().filter(newText);
return false;
}
});
//Items and subitems put into my list
ListOfObjects.add(new String[] {"first", "first1"});
ListOfObjects.add(new String[] {"second","second1"});
ListOfObjects.add(new String[] {"third","third1"});
//and so on
//sort
Collections.sort(ListOfObjects, new Comparator<String[]>() {
@Override
public int compare(String[] o1, String[] o2) {
return o1[0].compareToIgnoreCase(o2[0]);
}
});
//onClick
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(position==0){
Intent intent0 = new Intent(view.getContext(), first.class);
startActivityForResult(intent0,0);
}
else if(position==1){
Intent intent1 = new Intent(view.getContext(), second.class);
startActivityForResult(intent1,0);
}
else if(position==2){
Intent intent2 = new Intent(view.getContext(), third.class);
startActivityForResult(intent2,0);
}
else if(position==3){
Intent intent3 = new Intent(view.getContext(), fourth.class);
startActivityForResult(intent3,0);
}
}
});
}
}
ListOfObjects is the ArrayList the adapter uses to populate your views. So in setOnItemClickListener you can simply use:
if(ListOfObjects.get(position)[0].equals("first")){
// start fist activity
}
if(ListOfObjects.get(position)[0].equals("second")){
// start second activity
}