gpt4 book ai didi

java - Android - AlertDialog 中的字符串值在访问时不断重置

转载 作者:行者123 更新时间:2023-12-01 09:17:22 24 4
gpt4 key购买 nike

我遇到了一个问题,即使我在 AlertDialog fragment 中输入了数据,我输入的数据仍然重置为 null。假设我将数据输入到 EditText 对象中,将其存储在字符串变量中,将其设置为我的 Getter/Setter 类中的字符串值,然后从 fragment 中的该类中检索。

AlertDialog 图像

null

AlertDialog fragment

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 接收字符串的 fragment )

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

最佳答案

您在 ScoutFragmentAddressDialogFragment 中使用 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()

关于java - Android - AlertDialog 中的字符串值在访问时不断重置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40447383/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com