Reputation: 1
I am totally new to android but have worked in windows forms with comboboxes. The spinner is however bowling me out! I have a problem that I cannot find the answer to and now as last resort, I must appeal to you. I am building a simple android app that reads and writes data to a SQL server database. From the database, I need to load the name and the id numbers of teachers with a query into the spinner. The name of the selected teacher in the spinner must then be placed in a textView (textView1 named test text in the script). With the above, I have no problem.
My problem is that I want to place the id number of the selected teacher in the spinner into a second textView. To summarise: I select a teacher in the spinner and the name and id of the teacher appear in two separate text views.
I include the methods that contain my current efforts.
Any help will do.
//fill the spinner with the names of the teachers and place the name in a textView(testText) public void fillSpinner() {
Connection conn = connection();//connection to sql server database
try {
if (conn != null) {
testText.setText("Connected to server!");
String query = "SELECT id_Teacher, teacherName FROM Teachers";
PreparedStatement pst = conn.prepareStatement(query);
ResultSet rs = pst.executeQuery();
ArrayList<String> data = new ArrayList<String>();
while (rs.next()) {
String name = rs.getString("teacherName");
final String idTeacher = rs.getString("id_Teacher");
data.add(name);
data.add(idTeacher);
ArrayAdapter array = new ArrayAdapter(this, android.R.layout.simple_list_item_1, data);
spinner.setAdapter(array);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
testText.setText(spinner.getSelectedItem().toString());//place name in textview
fillIdTextView();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
} else {
testText.setText("IP Address incorrect or \n not entered!");
}
}
catch (Exception e) {
e.printStackTrace();
}
}
//Fill textView(txtId) with the id_Teacher of the nameTeacher selected in the spinner.
public void fillIdTextView(){
Connection conn = connection();
try {
if (conn != null) {
String query = "SELECT id_Teacher FROM Teachers";
PreparedStatement pst1 = conn.prepareStatement(query);
ResultSet rs = pst1.executeQuery();
ArrayList<String> dataId = new ArrayList<String>();
while (rs.next()) {
String idTeacher = rs.getString("id_Teacher");
dataId.add(idTeacher);
dataId.add(spinner.getSelectedItem().toString());
ArrayAdapter array = new ArrayAdapter(this, android.R.layout.simple_list_item_1, dataId);
txtId.setText(array.toString());//place the id of the selected teacher in textview.
// spinnerId.setAdapter(array);//place id in second spinner
}
} else {
testText.setText("IP Address incorrect or \n not entered!");
}
}
catch (Exception e) {
e.printStackTrace();
}
}
'''
Upvotes: 0
Views: 56
Reputation: 326
That's because of this part:
dataId.add(idTeacher);
dataId.add(spinner.getSelectedItem().toString());
Your adapter considers them two different items on the list, that is why they're being shown in two different text views.
If you want them to be shown in the same text view, you have to create a class Named Teacher with two fields (name, id). Then create a list of objects of type Teacher and pass it to your adapter. That's how it can understand they're actually one person/item not two.
Upvotes: 1