In my activity there is a button and ListView. The ListView contains a Spinner and an EditText view. I use the button every time I want to insert a new row entry into my ActivityView list.
I followed the instructions from previous stackoverflows like this one: Android Listview with spinner and a checkbox on how to populate ListViews with custom objects like Spinners.
My problem is that every time I dynamically add a new ListView entry to the ListView, the Spinner value of the previous ListView entry is lost (the Spinner action reverts to the default setting). Say for simplicity that my Spinners are filled with the following data:
String spinner_data[] = {"apple", "banana", "pear", "watermelon", "strawberry"};
For example, if I select my first ListView Spinner value for a “pear” and then add a new ListView record with my button, the “pear” entry disappears from the first ListView counter and the default value “apple” appears).
Any help is appreciated!
This is my activity:
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); lv = (ListView) findViewById(R.id.lv); da = new DataAdapter(this, new ArrayList<RawData>()); lv.setAdapter(da); btn_new = (Button)findViewById(R.id.btn_new); btn_new.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {
The RawData class is as follows:
public class RawData { private int selected_position; private ArrayAdapter<CharSequence> adapter; public RawData(Context context) { adapter = ArrayAdapter.createFromResource(context, R.array.data, android.R.layout.simple_spinner_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); } public ArrayAdapter<CharSequence> getAdapter() { return adapter; } public String getText() { return (String) adapter.getItem(selected_position); } public int getSelected() { return selected_position; } public void setSelected(int selected) { this.selected_position = selected; } }
The DataArrayAdapter is as follows:
public class DataArrayAdapter extends ArrayAdapter<RawData> { private Activity myContext;
source share