I have a ListView
, in the Item
ListView
there is a drop-down list ( Spinner
), there is also a "+" button with which the user can add new Items
to the ListView.
Suppose the user created two Items
and selected certain values in them in Spinner
, when adding a new Item
, the values selected in the first two Items
in Spinner
get confused, I can’t understand how to save values correctly and then display them when creating new Items
.
There was an idea to save selected values in ArrayList
when onItemSelected
into an array, but then it is not clear what to do if the user first selected one value and then changed it to another. Or maybe you need to use Expandable ListView
?
Code in Activity:
public class ClaimWorkActivity extends Activity { ArrayList <String> workCount = new ArrayList<>(); WorksAdapter worksAdapter; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.claim_work_activity); workCount.add("1"); lvListWork = (ListView) findViewById(R.id.lvListWork); btnAdd =(Button) findViewById(R.id.btnAdd); worksAdapter = new WorksAdapter(this); lvListWork.setAdapter(worksAdapter); btnAdd.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { workCount.add("1"); worksAdapter.addall(workCount); } }); worksAdapter.addall(workCount); } } }
Adapter Code:
public class WorksAdapter extends BaseAdapter { Context ctx; LayoutInflater lInflater; ArrayList<String> objects; private String [] workType = {"Work type 1", "Work type 2", "Work type 3", "Work type 4", "Work type 5"}; public WorksAdapter(Context context) { ctx = context; objects=new ArrayList<>(); lInflater = (LayoutInflater) ctx .getSystemService(Context.LAYOUT_INFLATER_SERVICE); } @Override public int getCount() { return objects.size(); } @Override public Object getItem(int position) { return objects.get(position); } @Override public long getItemId(int position) { return position; } public void addall(ArrayList<String> object){ objects.clear(); objects.addAll(object); notifyDataSetChanged(); } @Override public View getView(int position, View convertView, ViewGroup parent) { View view = convertView; if (view == null) { view = lInflater.inflate(R.layout.claim_work_item, parent, false); } ((TextView) view.findViewById(R.id.tvWorkNumber)).setText(String.valueOf(position+1)); //objects.get(position) Spinner spinnerClaimWorkName= (Spinner) view.findViewById(R.id.tvClaimWorkName); ArrayAdapter<String> adapterWorkType = new ArrayAdapter<String>(ctx, android.R.layout.simple_spinner_item, workType); adapterWorkType.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinnerClaimWorkName.setAdapter(adapterWorkType); spinnerClaimWorkName.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { Log.d("Work Type", workType[position]); } @Override public void onNothingSelected(AdapterView<?> arg0) { } }); return view; } }
getView()
force the states to the element from this array, in the case ofSpinner
- thesetSelection(position)
method. Then this array can be picked up via the getter adapter in the activation or any other recipient that needs information on the elements selected in the list. - pavlofff