保存列表的状态

时间:2015-09-17 20:06:56

标签: android android-bundle

如何保存我的List onSaveInstanceState方法的状态?

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css" rel="stylesheet" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
<nav role="navigation" class="navbar navbar-default navbar-static-top">
  <div class="container">
    <div class="navbar-header">
      <button type="button" data-target="#navbarCollapse" data-toggle="collapse" class="navbar-toggle"> <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>

      </button> <a href="index.html" class="navbar-brand" style="color:#000000">Paulo Pinzon-Iradian</a>

    </div>
    <div id="navbarCollapse" class="collapse navbar-collapse">
      <ul class="nav navbar-nav">
        <li><a href="work.html" style="color:#000000; margin-left:1.5em;">Work</a>

        </li>
        <li><a href="about.html" style="color:#000000; margin-left:1.5em;">About</a>

        </li>
        <li><a href="contact.html" style="color:#000000; margin-left:1.5em;">Contact</a>

        </li>
      </ul>
      <ul class="nav navbar-nav navbar-right">
        <li><a target="_blank" href="https://twitter.com/PauloPinzonArt" style="color:#00c7f4;"><i class="fa fa-twitter-square"></i></a>

        </li>
        <li><a target="_blank" href="https://instagram.com/paulopinzonart/" style="color:#125688;"><i class="fa fa-instagram"></i></a>

        </li>
        <li><a target="_blank" href="https://www.facebook.com/pages/Paulo-Pinzon-Iradian/849542288428399" style="color:#3b5197;"><i class="fa fa-facebook-square"></i></a>

        </li>
      </ul>
    </div>
  </div>
</nav>
<div class="container">
  <div class="alert alert-info">Soomething</div>
</div>

代码返回以下错误:

public class MainActivity extends AppCompatActivity {

    private ListView listView;
    private List<RssItem> items;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listView = (ListView) findViewById(R.id.listView);

        onStartCommand();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putStringArrayList("return", items);
    }

    private void onStartCommand() {
        Intent intent = new Intent(getBaseContext(), RssIntentService.class);
        intent.putExtra(RssIntentService.RECEIVER, resultReceiver);
        startService(intent);
    }

    private final ResultReceiver resultReceiver = new ResultReceiver(new Handler()) {
        @Override
        protected void onReceiveResult(int resultCode, Bundle resultData) {
            items = (List<RssItem>) resultData.getSerializable(RssIntentService.ITEMS);
            if (items != null) {
                RssAdapter adapter = new RssAdapter(getBaseContext(), items);
                listView.setAdapter(adapter);
            } else
                Toast.makeText(getBaseContext(), "Não foi possível encontrar seu feed", Toast.LENGTH_SHORT).show();
        }
    };
}

1 个答案:

答案 0 :(得分:1)

您可以将List声明为ArrayList:

private ArrayList<RssItem> items;

在使用arraylist而不是list的<{1}}方法中投射:

onReceiveResult

然后代码不应该抛出任何错误。