共享首选项不再保存

时间:2018-11-05 21:35:55

标签: android listview arraylist sharedpreferences

我有一个将ListView对象保存到ArrayList并在OnCreate上加载它们的活动。

我昨晚对其进行了测试,并且运行良好,但是今天再次进行测试时,它正在加载昨天已经保存的内容,但是不再保存新的列表项。

这是我的代码:

<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
        xmlns:local="clr-namespace:Prototipe"
        x:Class="Prototipo.HomePage"
         NavigationPage.HasNavigationBar="False">
 <local:FinderPage Title="Finder" Icon="Finder.png" />
 <local:TransferPage Title="Transfer" Icon="transfer.png"/>
 <local:ChargePage Icon="charge.png"/>
 <local:RefillPage Title="Refill" Icon="refills.png"/>
 <local:ToolsPage Title="Tools" Icon="tools.png"/>
</TabbedPage>

///从给定的kay:值对中检索保存的首选项。

public class SubActivity extends AppCompatActivity {

Button addItem;
ImageButton back;
Button saveListBtn;
private ListView listViewer;
ArrayList<ItemObjects> items = new ArrayList<ItemObjects>();
private String key = "arg";

//这将从sharedPreferences中加载项目,并且工作正常。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment1_item_screen);
    Bundle extra = getIntent().getExtras();
    String extraString = extra.getString(key);

///使用正确的信息加载listView。

    if(loadList(extraString) != null){
        items = loadList(extraString);
    }

///只需按一下按钮,就会将一个新对象添加到 final ItemObjectsAdapter adapter = new ItemObjectsAdapter(this, items); listViewer = (ListView) findViewById(R.id.itemListView); listViewer.setAdapter(adapter); ArrayList中。我现在只是放入一个空白对象。问题来自items。它似乎不能正常工作,但是昨天它确实可以正常工作,因为每次我加载活动时,都会从sharedPreferences加载4个列表项,但是,我似乎再也无法保存了。

saveList(items, key)

//这是保存项目列表的代码。请注意,此操作昨天有效,我还没有对其进行任何更改。

    addItem = (Button) findViewById(R.id.addItemBtn);
    addItem.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ItemObjects blank = new ItemObjects("", "", "", "");
            items.add(blank);
            saveList(items, key);
            adapter.notifyDataSetChanged();
        }
    });

///这仅仅是从sharedPreferences加载项目的代码,似乎工作正常。

private void saveList(ArrayList<ItemObjects> list, String key) {
    SharedPreferences appSharedPrefs = PreferenceManager
            .getDefaultSharedPreferences(this.getApplicationContext());
    SharedPreferences.Editor prefsEditor = appSharedPrefs.edit();
    Gson gson = new Gson();
    String json = gson.toJson(list);
    prefsEditor.putString(key, json);
    prefsEditor.apply();
}

关于为什么不保存的任何想法?

1 个答案:

答案 0 :(得分:0)

好吧,看起来PS> (Get-Command -Module Microsoft.Powershell.Utility).Name ConvertFrom-SddlString Format-Hex Get-FileHash Import-PowerShellDataFile New-Guid New-TemporaryFile Add-Member Add-Type Clear-Variable Compare-Object ConvertFrom-Csv ConvertFrom-Json ConvertFrom-String ConvertFrom-StringData Convert-String ConvertTo-Csv ConvertTo-Html ConvertTo-Json ConvertTo-Xml Debug-Runspace Disable-PSBreakpoint Disable-RunspaceDebug Enable-PSBreakpoint Enable-RunspaceDebug Export-Alias Export-Clixml Export-Csv Export-FormatData Export-PSSession Format-Custom Format-List Format-Table Format-Wide Get-Alias Get-Culture Get-Date Get-Event Get-EventSubscriber Get-FormatData Get-Host Get-Member Get-PSBreakpoint Get-PSCallStack Get-Random Get-Runspace Get-RunspaceDebug Get-TraceSource Get-TypeData Get-UICulture Get-Unique Get-Variable Group-Object Import-Alias Import-Clixml Import-Csv Import-LocalizedData Import-PSSession Invoke-Expression Invoke-RestMethod Invoke-WebRequest Measure-Command Measure-Object New-Alias New-Event New-Object New-TimeSpan New-Variable Out-File Out-GridView Out-Printer Out-String Read-Host Register-EngineEvent Register-ObjectEvent Remove-Event Remove-PSBreakpoint Remove-TypeData Remove-Variable Select-Object Select-String Select-Xml Send-MailMessage Set-Alias Set-Date Set-PSBreakpoint Set-TraceSource Set-Variable Show-Command Sort-Object Start-Sleep Tee-Object Trace-Command Unblock-File Unregister-Event Update-FormatData Update-List Update-TypeData Wait-Debugger Wait-Event Write-Debug Write-Error Write-Host Write-Information Write-Output Write-Progress Write-Verbose Write-Warning 应该是saveList(items, key);

现在可以使用。