保存和检索数组并在ListView中显示

时间:2016-02-09 01:43:37

标签: java android listview sharedpreferences

我正在创建一个日志,我试图存储由用户创建的几个字符串,然后检索并显示在我的列表视图(lwItems)中。但是,我当前的问题是每当用户写一个字符串时,整个表都会被覆盖。如何更改我的代码,以便将数组保存到共享首选项,并将字符串附加到它而不会破坏以前的条目?这是我的java类 - (“customValues”由我的类传递给用户输入其字符串)

public class Training1 extends Fragment implements View.OnClickListener, AdapterView.OnItemClickListener {

    private ListView lwItems;
    private Button btnChangeItems;    
    private String DEFAULT_ITEMS_VALUES_STRING = "Item 1; Item 2";
    private String customItemString;
    private static SharedPreferences preferenceItems;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {


        View rootView = inflater.inflate(R.layout.content_main, container, false);

        lwItems = (ListView) rootView.findViewById(R.id.lwItems);
        preferenceItems = getActivity().getSharedPreferences("items", 0);

        setHasOptionsMenu(true);

        return rootView;
    }

    @Override
    public void onCreateOptionsMenu(
            Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.traning_menu_itemdetail, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // handle item selection
        switch (item.getItemId()) {
            case R.id.action_add:
                Intent trainingCreateIntent = new Intent(getContext(), Training2.class);
                startActivity(trainingCreateIntent);
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        Bundle setItemsIntent = getActivity().getIntent().getExtras();
        if (setItemsIntent != null) {
            customItemString = getActivity().getIntent().getExtras().getString("customValues");
        }

        String[] items;
        String preferenceItemsString = preferenceItems.getString("customValues", null);
        if (preferenceItemsString != null) {
            items = getSplitAndTrim(preferenceItemsString);
        } else {
            items = getSplitAndTrim(DEFAULT_ITEMS_VALUES_STRING);
        }

        ArrayAdapter<String> arrayAdapter =
                new ArrayAdapter<>(getActivity(), android.R.layout.simple_expandable_list_item_1, items);
        lwItems.setAdapter(arrayAdapter);
        lwItems.setOnItemClickListener(this);

    }

    @Override
    public void onPause() {
        super.onPause();
        if (customItemString != null) {
            putStringInPreferences("customValues", customItemString);
        }
    }

    @Override
    public void onClick(View v) {

        Intent intent = new Intent(getActivity(), Training2.class);
        startActivity(intent);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        String textOfItem = String.valueOf(((TextView) view).getText());
        Toast.makeText(getActivity(), textOfItem, Toast.LENGTH_SHORT).show();
    }

    public static void putStringInPreferences(String key, String customItemString) {
        SharedPreferences.Editor editor = preferenceItems.edit();
        editor.putString(key, customItemString);
        editor.commit();
    }

    @NonNull
    private String[] getSplitAndTrim(String preferenceItemsString) {
        return preferenceItemsString.trim().split("\\s*;\\s*");
    }
}

1 个答案:

答案 0 :(得分:1)

我猜这个方法:

public static void putStringInPreferences(String key, String customItemString) {
    SharedPreferences.Editor editor = preferenceItems.edit();
    editor.putString(key, customItemString);
    editor.commit();
}

您首先要读出原始值,添加新项目,然后写出新字符串。这段代码只是在每次调用时用新字符串覆盖旧字符串。