'How to get Value of String OnClick of a ViewList in Android Studio
I am new to android studio.
I am making an app that lists names of teachers and the subjects thought and then every person has a different class. The teacher names and subjects though are stored in a TreeMap.
I am having difficulty as I want my app to feature sorts, so when outputted the positions of teachers are changed according to the sort. At the moment the items are sorted via hard code alphabetically and a switch case is used and the onclick integer position is obtained. However, I want to make the switch case make a decision according to the string of the itemClicked so whenever a sort is executed when the user clicks the correct class is displayed. Kindly see my code below:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView resultsListView = (ListView) findViewById(R.id.results_listview);
TreeMap<String, String> nameSubject = new TreeMap<>();
nameSubject.put("Richard Bonnici", "Maths"); //1
nameSubject.put("Natasha Mifsud", "English"); //2
nameSubject.put("Victor Valdes", "Computing"); //3
nameSubject.put("Jonathan Galea", "Computing"); //4
nameSubject.put("Jerome Micallef", "History"); //5
nameSubject.put("Tarah Galea", "Marketing"); //6
nameSubject.put("Micheal Gonzalez", "Maths"); //7
nameSubject.put("Jason Bartolo", "Chemistry"); //8
nameSubject.put("Jack Grech", "Chemistry"); //9
nameSubject.put("Anna Carbonaro", "Maltese"); //10
nameSubject.put("Kay Azzopardi", "Biology"); //11
nameSubject.put("George Micallef", "Physics"); //12
nameSubject.put("Gabriella Bonnici", "Maths"); //13
Map sortedMap = valueSort(nameSubject);
// Get a set of the entries on the sorted map
Set set = sortedMap.entrySet();
// Get an iterator
Iterator i = set.iterator();
List<HashMap<String, String>> listItems = new ArrayList<>();
SimpleAdapter adapter = new SimpleAdapter(this, listItems, R.layout.list_item,
new String[]{"First Line", "Second Line"},
new int[]{R.id.text1, R.id.text2});
Iterator it = nameSubject.entrySet().iterator();
while (it.hasNext()) {
HashMap<String, String> resultsMap = new HashMap<>();
Map.Entry pair = (Map.Entry) it.next();
resultsMap.put("First Line", pair.getKey().toString());
resultsMap.put("Second Line", pair.getValue().toString());
listItems.add(resultsMap);
}
resultsListView.setAdapter(adapter);
resultsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(android.widget.AdapterView<?> adapterView, View view, int i, long l) {
//
switch (i) {
case 0:
startActivity(new Intent(TeacherList.this, AnnaCarbonaro.class));
break;
case 1:
startActivity(new Intent(TeacherList.this, GabriellaBonnici.class));
break;
case 2:
startActivity(new Intent(TeacherList.this, GeorgeMicallef.class));
break;
case 3:
startActivity(new Intent(TeacherList.this, JackGrech.class));
break;
case 4:
startActivity(new Intent(TeacherList.this, JasonBartolo.class));
break;
case 5:
startActivity(new Intent(TeacherList.this, JeromeMicallef.class));
break;
case 6:
startActivity(new Intent(TeacherList.this, JonathanGalea.class));
break;
case 7:
startActivity(new Intent(TeacherList.this, KayAzzopardi.class));
break;
case 8:
startActivity(new Intent(TeacherList.this, MichealGonzalez.class));
break;
case 9:
startActivity(new Intent(TeacherList.this, NatashaMifsud.class));
break;
case 10:
startActivity(new Intent(TeacherList.this, RichardBonnici.class));
break;
case 11:
startActivity(new Intent(TeacherList.this, TarahGalea.class));
break;
case 12:
startActivity(new Intent(TeacherList.this, VictorValdes.class));
break;
}
}
});
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
