Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have populated spinner values with json data and posted it into a Web Service. Now i have the option to edit all the info. When i click on edit the value that was stored previously in the spinner must to pre-populated as the first item along with other json vales.
Java
nationality_name1.add(jsonObject1.getString("NATIONALITY"));
SprStudentEnrolmentNationality.setAdapter(new ArrayAdapter(ManagementEnhancementProgramStudentEnrolment.this,android.R.layout.simple_list_item_1, nationality_name1));
SprStudentEnrolmentNationality.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                            @Override
                            public void onItemSelected(AdapterView parent, View view, int position, long id) {
                                populateNationality();
                        });


By doing like this the spinner would first be populated by the value that was stored on then displays all items and the first item is no longer in the first position.

What I have tried:

Java
nationality_name1.add(jsonObject1.getString("NATIONALITY"));
SprStudentEnrolmentNationality.setAdapter(new ArrayAdapter(ManagementEnhancementProgramStudentEnrolment.this,android.R.layout.simple_list_item_1, nationality_name1));
SprStudentEnrolmentNationality.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                            @Override
                            public void onItemSelected(AdapterView parent, View view, int position, long id) {
                                populateNationality();
                        });
Posted
Comments
David Crow 24-Jul-17 15:29pm    
Your two code snippets look the same. Is that intentional?

That aside, what does the code shown have to do with the problem? Consider something like the following with a Spinner, EditText, and Button widgets:

String[] names = new String[]{ "Name1", "Name2", "Name3", "Name4", "Name5" };
final ArrayList list = new ArrayList(Arrays.asList(names));
final ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, list);

Spinner spinner = (Spinner) findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
@Override
public void onItemSelected( AdapterView<?> parent, View view, int position, long id )
{
mPosition = position;
String item = (String) parent.getItemAtPosition(mPosition);

EditText edit = (EditText) findViewById(R.id.edit);
edit.setText(item);
}
});

spinner.setAdapter(adapter);

Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick( View v )
{
EditText edit = (EditText) findViewById(R.id.edit);
String text = edit.getText().toString();

list.remove(mPosition);
list.add(mPosition, text);

adapter.notifyDataSetChanged();
}
});
Rahul Ramakrishnan 25-Jul-17 0:11am    
Yes it was intentional. Did know what to put in the "What have I tried section" while posting the question

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900