Android - 来自 AlertDialog 的字符串值在访问时不断重置
Posted
技术标签:
【中文标题】Android - 来自 AlertDialog 的字符串值在访问时不断重置【英文标题】:Android - String value from AlertDialog keeps resetting on access 【发布时间】:2017-03-19 17:54:21 【问题描述】:我遇到了一个问题,即使我在 AlertDialog Fragment 中为它输入了数据,我也将数据输入到我的字符串一直重置为 null 的位置。假设我将数据输入到 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
【问题讨论】:
【参考方案1】:您在ScoutFragment
和AddressDialogFragment
中都使用AddressProvider addressProvider = new AddressProvider();
。
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()
【讨论】:
以上是关于Android - 来自 AlertDialog 的字符串值在访问时不断重置的主要内容,如果未能解决你的问题,请参考以下文章
Android:如何设置AlertDialog的宽度和高度,以及AlertDialog风格的按钮?