我正在尝试在SharedPreferences上保存4个变量的数据并将其加载到列表中。我的问题是它显示了列表第一行中的所有数据。我想分开:每次我点击保存按钮我想保存4个变量,当我再次点击它时,我想保存新变量并显示一个新行。
TubeDataFragment(保存):
public class TubeDataFragment extends Fragment {
List<String> tubeData = new ArrayList<>();
public TubeDataFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_tube_data, container, false);
final Spinner spinnerMaterial = (Spinner) view.findViewById(R.id.snipperMaterial);
ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(getActivity(), R.layout.spinner_item, getResources().getStringArray(R.array.material));
myAdapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
spinnerMaterial.setAdapter(myAdapter);
//controls
Button btn_saveTube = (Button) view.findViewById(R.id.btn_save_tube);
final EditText et_diameter = (EditText) view.findViewById(R.id.et_diameter);
final EditText et_clr = (EditText) view.findViewById(R.id.et_clr);
final EditText et_thickness = (EditText) view.findViewById(R.id.et_thickness);
//guarda dados do tubo
btn_saveTube.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tubeData.add(" Diameter: " + et_diameter.getText().toString());
tubeData.add(" Thickness: " + et_thickness.getText().toString());
tubeData.add(" CLR: " + et_clr.getText().toString());
tubeData.add(" Material: " + spinnerMaterial.getSelectedItem().toString());
StringBuilder stringBuilder = new StringBuilder();
int i = 1;
for(String data : tubeData)
{
stringBuilder.append(data);
stringBuilder.append(";");
if(i++ == tubeData.size())
{
stringBuilder.append("\n");
}
}
SharedPreferences settings = getActivity().getSharedPreferences("PREFS",0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("tubeData", stringBuilder.toString());
editor.commit();
}
});
return view;
}
}
ArchiveFragment(加载)
public class ArchiveFragment extends Fragment {
public ArchiveFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_archive, container, false);
ListView tubeDataList = (ListView) view.findViewById(R.id.tubeData_list);
//load tube data
SharedPreferences settings = getActivity().getSharedPreferences("PREFS",0);
String tubeDataString = settings.getString("tubeData", "");
String[] tubeDataSplit = tubeDataString.split("\n");
List<String> tubeDataItems = new ArrayList<>();
for(int i=0; i<tubeDataSplit.length;i++)
{
tubeDataItems.add(tubeDataSplit[i]);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, android.R.id.text1, tubeDataItems);
// Assign adapter to ListView
tubeDataList.setAdapter(adapter);
return view;
}
}
答案 0 :(得分:0)
if 条件错误。
在TubeDataFragment
中,使用onClick()
方法中的以下代码:
.............
..................
StringBuilder stringBuilder = new StringBuilder();
int i = 1;
for(String data : tubeData)
{
stringBuilder.append(data);
stringBuilder.append(";");
if(i == 4)
{
stringBuilder.append("\n");
i = 0;
}
i++;
}
...............
.......................