show()
我正在尝试创建一个片段,当显示时,它会根据我传递给该方法的额外项目来更改其内容。对话框片段实例是为了可重用性而保留的,换句话说,我只需将片段实例化一次,并show()
在我希望显示它时使用它显示的新对象进行调用。它在第一次显示时工作正常,但对于随后的显示调用,我无法更改编辑文本的文本。你会在下面的课程中看到,我实际上已经明确地在show()
代码块之后将文本更改为“我被改变了”,但这从未反映在 ui 中。
public class RegisterFragment extends DialogFragment implements View.OnClickListener, DialogInterface.OnClickListener
{
private static final String KEY_SOURCE = "source";
private static String source;
private static JSONObject listEntry;
private RegisterItem item;
private boolean spinnerMode;
private boolean viewsCreated;
private TextView label1, label2;
private EditText field1 = null, field2 = null;
private Spinner spinner;
private ArrayAdapter<String> adapter;
private Button send, cancel;
private OnClickSendCancelButtonListener sendListener;
public static RegisterFragment newInstance(String source)
{
RegisterFragment fragment = new RegisterFragment();
Bundle b = new Bundle();
b.putString(KEY_SOURCE, source);
fragment.setArguments(b);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Bundle b = this.getArguments();
if (b != null)
{
source = b.getString(KEY_SOURCE);
load(source);
}
adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
viewsCreated = false;
}
public RegisterItem getRegisterItem()
{
return this.item;
}
@Override
public void onAttach(Activity activity)
{
super.onAttach(activity);
try
{
this.sendListener = (OnClickSendCancelButtonListener) activity;
}
catch (ClassCastException e)
{
throw new ClassCastException("Activity must implement RegisterFragment#OnClickSendCancelButtonListener");
}
}
private void load(String source)
{
if (item == null)
item = new RegisterItem();
item.loadData(source);
spinnerMode = item.hasChoices();
if (!viewsCreated)
return;
label1.setText(item.field1Label);
label2.setText(item.field2Label);
send.setText(item.sendButtonText);
cancel.setText(item.cancelButtonText);
Log.v("RegisterFragment", "FIELD1: " + item.defaultField1);
Log.v("RegisterFragment", "FIELD2: " + item.defaultField2);
if (spinnerMode)
{
field1.setVisibility(View.GONE);
spinner.setVisibility(View.VISIBLE);
adapter.clear();
for(String choice: item.choices)
adapter.add(choice);
adapter.notifyDataSetChanged();
}
else
{
field1.setVisibility(View.VISIBLE);
spinner.setVisibility(View.GONE);
}
if (item.hasDefaults())
{
Log.v("RegisterFragment", "Setting text for field 2");
field2.setText(item.defaultField2);
Log.v("RegisterFragment", "FIELD2 TEXT: " + field2.getText().toString());
if (spinnerMode)
spinner.setSelection(adapter.getPosition(item.defaultField2));
else
field1.setText(item.defaultField1);
}
}
public void show(FragmentManager manager, String tag, String source, String listEntry)
{
Log.v("Register Fragment", "Showing fragment\nSOURCE STRING: " + source);
load(source);
RegisterFragment.source = source;
try {
RegisterFragment.listEntry = new JSONObject(listEntry);
} catch (JSONException e) {
e.printStackTrace();
}
super.show(manager, tag);
if (field2 != null)
field2.setText("I Have Been Changed");
}
@Override
public void dismiss()
{
if (field1 != null)
field1.setText("");
if (field2 != null)
field2.setText("");
super.dismiss();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View v = inflater.inflate(R.layout.register_screen, null);
spinner = (Spinner) v.findViewById(R.id.register_spinner);
spinner.setAdapter(adapter);
label1 = (TextView) v.findViewById(R.id.register_label1);
label2 = (TextView) v.findViewById(R.id.register_label2);
field1 = (EditText) v.findViewById(R.id.register_field1);
field2 = (EditText) v.findViewById(R.id.register_field2);
send = (Button) v.findViewById(R.id.register_send);
cancel = (Button) v.findViewById(R.id.register_cancel);
send.setOnClickListener(this);
cancel.setOnClickListener(this);
viewsCreated = true;
load(source);
return v;
}
@Override
public void onDestroyView() {
super.onDestroyView();
viewsCreated = false;
}
}