我遇到了一个问题,即使在AlertDialog Fragment中为它输入数据后,我也要在其中将数据输入字符串的位置保持为零。应该是将数据输入EditText对象,将其存储在字符串变量中,将其设置为Getter / Setter类中的字符串值,然后从片段中的该类中检索。
AlertDialog的图像
AlertDialog片段
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import org.ramferno.scoutapplication.ramfernoscout.R;
import org.ramferno.scoutapplication.ramfernoscout.providers.AddressProvider;
public class AddressDialogFragment extends DialogFragment {
AddressProvider addressProvider = new AddressProvider();
EditText enterIP;
String urlAddress;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
//Declare and initialize objects
LayoutInflater i = getActivity().getLayoutInflater();
View v = i.inflate(R.layout.fragment_dialog, null);
enterIP = (EditText) v.findViewById(R.id.enterIP);
//Create AlertDialog
AlertDialog.Builder b = new AlertDialog.Builder(getActivity())
.setTitle("Enter IP Address")
.setPositiveButton("ADD",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
urlAddress = enterIP.getText().toString();
addressProvider.setAddress(urlAddress);
} //End of onClick
}) //End of DialogInterface
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.cancel();
} //End of onClick
} //End of DialogInterface
); //End of AlertDialog
b.setView(v);
return b.create();
} //End of onCreateDialog
} //End of class
Getter / Setter类
public class AddressProvider {
private String urlAddress;
public String getAddress() {
return urlAddress;
} //End of getAddress
public void setAddress(String urlAddress) {
this.urlAddress = urlAddress;
} //End of setAddress
} //End of class
ScoutFragment(从Getter / Setter接收字符串的片段)
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import org.ramferno.scoutapplication.ramfernoscout.downloaders.Downloader;
import org.ramferno.scoutapplication.ramfernoscout.R;
import org.ramferno.scoutapplication.ramfernoscout.providers.AddressProvider;
//Start of ScoutFragment
public class ScoutFragment extends Fragment {
//Declares Android UI objects
AddressProvider addressProvider = new AddressProvider();
FloatingActionButton addDataScout;
ListView eListScoutInfo;
String IP = addressProvider.getAddress();
//Declare and initialize variable
String urlAddress = "http://" + IP + "/ramfernoscout/matchdb/matchretrieve.php";
public ScoutFragment() {
// Required empty public constructor
} //End of ScoutFragment
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//Inflates layout for this fragment
View view = inflater.inflate(R.layout.fragment_scout, null, false);
//Instantiate ListView object with the xml ListView object
eListScoutInfo = (ListView) view.findViewById(R.id.listScoutInfo);
//Add instructions to the Refresh FAB that will download the data from the database server
FloatingActionButton fab = (FloatingActionButton) view.findViewById(R.id.fab2);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Downloader d = new Downloader(getActivity(),urlAddress,eListScoutInfo);
d.execute();
} //End of onClick
}); //End of setOnClickListener
//Change fragment to AddScoutDataFragment with animations
addDataScout = (FloatingActionButton) view.findViewById(R.id.fab);
addDataScout.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
AddScoutDataFragment fragment = new AddScoutDataFragment();
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.enter_from_right,
R.anim.exit_to_left, R.anim.enter_from_left, R.anim.exit_to_right);
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
} //End of onClick
}); //End of setOnClickListener
//Returns view
return view;
} //End of onCreateView
} //End of class
最佳答案
您在AddressProvider addressProvider = new AddressProvider();
和ScoutFragment
中都使用AddressDialogFragment
。new
运算符将创建AddressProvider
类的新实例。如果要保留数据,则应仅创建AddressProvider
的单个实例。因此,应将AddressProvider
设置为SingleTon
类。
public class AddressProvider {
private static AddressProvider ourInstance = new AddressProvider();
private String urlAddress;
private AddressProvider() {
}
public static AddressProvider getInstance() {
return ourInstance;
}
public String getAddress() {
return urlAddress;
}
public void setAddress(String urlAddress) {
this.urlAddress = urlAddress;
}
}
用法,
要存储IP,
AddressProvider.getInstance().setAddress("xxx.xxx.xx.xx");
取回,
AddressProvider.getInstance().getAddress()