there is a List, which is initially empty, after you click the add button, an AlertDialog opens, with input fields, data is entered, and after entering it should appear in the ListView.
2 answers
Announce your list globally and initialize
ArrayList<DataModel> maps = new ArrayList(); Well, add to the list:
builder.setPositiveButton("Добавить", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { maps.add(dataModel); adapter.notifyDataSetChanged(); dialog.dismiss(); } }); - why then loadData () if now it does not return a value? DataModel dataModel too then it is necessary to make global? - Sergey
- it still did not help, the data is not displayed. then the list item is added and that's it. and initially before data entry, an empty list item is set - Sergey
- and, yes, pass in adapter maps like this to adapter = new ListTovarAdapter (getActivity (), maps); - Android Android
- updated answer check - Android Android
- I redid it, but it turns out that now the data is not filled in maps, and it is always empty - Sergey
|
This is how it should work.
private ArrayList<DataModel> maps; ImageButton add_tovar = (ImageButton)getActivity().getParent().findViewById(R.id.add_tovar); add_tovar.setVisibility(View.VISIBLE); add_tovar.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { inputDialog(); } }); adapter = new ListTovarAdapter(getActivity(), loadData()); listTovar.setAdapter(adapter); public ArrayList<DataModel> loadData() { maps = new ArrayList<>(); DataModel dataModel = new DataModel(dataModelList.getName_tovar(),dataModelList.getMark_tovar(),dataModelList.getCost_tovar()); maps.add(dataModel); return maps; } builder.setPositiveButton("Добавить", new DialogInterface.OnClickListener(){ @Override public void onClick(DialogInterface dialog, int which) { adapter.addModel(dataModel); dialog.dismiss(); } }); AlertDialog dialog = builder.create(); dialog.show(); } in adapter
public void addModel(DataModel dataModel){ dataModelsTovar.add(dataModel); adapter.notifyDataSetChanged(); } - No, it does not work, empty elements of the list are still displayed - Sergey
- if the elements are displayed but empty, then somewhere else you have broken logic. You can try to write down this method in the adapter, now I will update the answer - artemiygreg
- I don’t understand why here maps.add (dataModel); after the click, null is added anyway - Sergey
- @ Sergey you have a DataModel null - artemiygreg
- Now I set the default fields in Datamodel equal to const, and now adding the value adds just the same value, it turns out that the value from alertDialoga was not processed .. something was completely confused - Sergey
|