Implemented a single-choice dialog with fragments. Now you need to make the previously selected item the next time the dialog is displayed (checked == true). Something I sit and can not think of anything. I would be very grateful for the help in this matter, not necessarily with the example code (although it would be great!), But at least just an algorithm for my actions in words.

Here are my ideas. the fragment of the dialogue itself:

public class MyDialogFragment extends DialogFragment implements View.OnClickListener{ private ListView list; private DialogListAdapter adapter; private String[] data = {"liq", "bin", "nex", "rex", "hex", "stmell"}; public static MyDialogFragment newInstance() { MyDialogFragment f = new MyDialogFragment(); f.setStyle(1, 0); return f; } @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.toolbar_ts_acc_dialog_layout, null); Button btnOk = view.findViewById(R.id.toolbar_dialog_btn_ok); Button btnCancel = view.findViewById(R.id.toolbar_dialog_btn_cancel); list = view.findViewById(R.id.toolbar_dialog_accout_list); adapter = new DialogListAdapter(getActivity(), data); list.setAdapter(adapter); list.setOnItemClickListener(onItemClickListener); btnOk.setOnClickListener(this); btnCancel.setOnClickListener(this); return view; } @Override public void onClick(View v) { switch (v.getId()) { case R.id.toolbar_dialog_btn_ok: TextView accName = getActivity().findViewById(R.id.toolbar_dialog_show_tv); int checked = list.getCheckedItemPosition(); if (checked >= 0) accName.setText(data[checked]); break; case R.id.toolbar_dialog_btn_cancel: break; } dismiss(); } AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { adapter.setSelectedItem(position); adapter.notifyDataSetChanged(); } }; } 

and adapter:

 public class DialogListAdapter extends BaseAdapter { private String[] data; private final Context context; private final LayoutInflater inflater; private int selectedItem = -1; public DialogListAdapter(Context context, String[] data) { this.context = context; this.data = data; inflater = LayoutInflater.from(context); } @Override public int getCount() { return data.length; } @Override public Object getItem(int position) { return data[position]; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView == null) { convertView = inflater.inflate(R.layout.toolbar_ts_acc_dialog_list_item, parent, false); holder = new ViewHolder(); holder.radioButton = convertView.findViewById(R.id.toolbar_dialog_radio); holder.accName = convertView.findViewById(R.id.toolbar_dialog_acc_name); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.accName.setText(data[position]); if (selectedItem == position) { holder.radioButton.setChecked(true); } else holder.radioButton.setChecked(false); return convertView; } static class ViewHolder { public TextView accName; public RadioButton radioButton; } public void setSelectedItem(int num) { selectedItem = num; } } 
  • For example, save the SharedPreferences position in SharedPreferences , and when creating a fragment, get and check. There are other options - depending on the conditions under which the position should be restored or should not - woesss
  • And if I need to throw the saved value into another fragment? Can I get these SharedPreferences in another snippet? - Snuf
  • and I don't understand where to do it? in the adapter or in the fragment? - Snuf

1 answer 1

Try this. With the help of SharedPreferences, you save the selected position of the listView by calling the savePosition () method, and with the help of loadPosition () you get. About the listview.setselection method, I'm not sure what will work, if anything, see here

 public class MyDialogFragment extends DialogFragment implements View.OnClickListener{ private ListView list; private DialogListAdapter adapter; private String[] data = {"liq", "bin", "nex", "rex", "hex", "stmell"}; SharedPreferences sPref; final String SAVED_POSITION = "saved_position"; public static MyDialogFragment newInstance() { MyDialogFragment f = new MyDialogFragment(); f.setStyle(1, 0); return f; } @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.toolbar_ts_acc_dialog_layout, null); Button btnOk = view.findViewById(R.id.toolbar_dialog_btn_ok); Button btnCancel = view.findViewById(R.id.toolbar_dialog_btn_cancel); list = view.findViewById(R.id.toolbar_dialog_accout_list); adapter = new DialogListAdapter(getActivity(), data); list.setAdapter(adapter); list.setOnItemClickListener(onItemClickListener); listView.requestFocusFromTouch(); listView.setSelection(loadPosition()); btnOk.setOnClickListener(this); btnCancel.setOnClickListener(this); return view; } @Override public void onClick(View v) { switch (v.getId()) { case R.id.toolbar_dialog_btn_ok: TextView accName = getActivity().findViewById(R.id.toolbar_dialog_show_tv); int checked = list.getCheckedItemPosition(); if (checked >= 0) accName.setText(data[checked]); break; case R.id.toolbar_dialog_btn_cancel: break; } dismiss(); } AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { savePosition(position); adapter.setSelectedItem(position); adapter.notifyDataSetChanged(); } }; private void savePosition(int position) { sPref = getPreferences(MODE_PRIVATE); Editor ed = sPref.edit(); ed.putInt(SAVED_POSITION, position); ed.commit(); } private int loadPosition() { sPref = getPreferences(MODE_PRIVATE); return sPref.getInt(SAVED_POSITION, 0); } } 
  • And he did. thank! - Snuf