使用onBackPressed保持列表位置

时间:2015-12-01 17:59:15

标签: java android

我目前有一个应用程序显示新闻文章列表,然后在点击时将用户带到完整活动。列表很长,所以我希望它能在用户点击文章时保持其位置。

我在onBackPressed中使用了NewsItemActivity,但它似乎无法正常工作。

这是我的MainActivity代码

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

    GridView newsListView = (GridView) findViewById(R.id.newsFeedList);
    adapter = new NewsListAdapter(this, R.layout.adapter_news_list, newsListData, this);

    newsListView.setAdapter(adapter);

    newsListView.setOnItemClickListener(itemClicked);

    EDANewsApp app = EDANewsApp.getInstance();

  //  String url = "http://www.efstratiou.info/projects/newsfeed/getList.php";

  //  JsonArrayRequest jsonRequest = new JsonArrayRequest(url, listener, errorListener);

   // app.requestQueue.add(jsonRequest);

    nextStart = 0;
    updateListData(nextStart, 20);
}

public int nextStart = 0;

public void updateListData(int StartPoint, int count){
    String url = "http://www.efstratiou.info/projects/newsfeed/getList.php?start=" + StartPoint + "&count=" + count;

    EDANewsApp app = EDANewsApp.getInstance();

    JsonArrayRequest jsonRequest = new JsonArrayRequest(url, listener, errorListener);
    app.requestQueue.add(jsonRequest);

    nextStart +=count;
   // updateListData(true, nextStart, 20);

}

@Override
public boolean onCreateOptionsMenu (Menu menu){
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_menu, menu);
    return true;
}

public boolean onOptionsItemSelected(MenuItem item){
    switch (item.getItemId()) {
        case R.id.action_about:
            Intent intent = new Intent(this, AboutActivity.class);
            startActivity(intent);
            return true;
        case R.id.action_search:
            return true;
        case R.id.action_settings:
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
}

AdapterView.OnItemClickListener itemClicked = new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        Intent intent = new Intent(MainActivity.this, NewsItemActivity.class);

        intent.putExtra("newsItemId", newsListData.get(position).recordId);

        startActivity(intent);
    }
};

NewsItemActivity.java

public class NewsItemActivity extends AppCompatActivity {

    //Declare java object for the UI elements
    private TextView itemTitle;
    private TextView itemDate;
    private TextView itemContent;
    private NetworkImageView itemImage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_news_item);

        itemTitle = (TextView) findViewById(R.id.itemTitle);
        itemDate = (TextView) findViewById(R.id.itemDate);
        itemContent = (TextView) findViewById(R.id.itemContent);
        itemImage = (NetworkImageView) findViewById(R.id.itemImage);

        EDANewsApp app = EDANewsApp.getInstance();

        Intent intent = getIntent();
        int itemId = intent.getIntExtra("newsItemId", 0);

        String url = "http://www.efstratiou.info/projects/newsfeed/getItem.php?id="+itemId;

        JsonObjectRequest request = new JsonObjectRequest(url, listener, errorListener);

        app.requestQueue.add(request);
    };

    Response.Listener<JSONObject> listener = new Response.Listener<JSONObject>() {
        @Override
        public void onResponse (JSONObject response){

            //Put the json data in a java object
            NewsRecord record = new NewsRecord();
            try {
                record.recordId = response.getInt("record_id");
                record.title = response.getString("title");
                record.date = response.getString("date");
                record.shortInfo = response.getString("short_info");
                record.contents = response.getString("contents");
                record.imageUrl = response.getString("image_url");
            }
            catch (JSONException e){
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            //show the data
            itemTitle.setText(record.title);
            itemDate.setText(record.date);
            itemContent.setText(record.contents);
            itemImage.setImageUrl(record.imageUrl, EDANewsApp.getInstance().imageLoader);
        }
    };

    Response.ErrorListener errorListener = new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            //There was an error in the communication
            //We can notify the user about it
        }
    };

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }
}

1 个答案:

答案 0 :(得分:-1)

您可以使用onSaveInstanceState(Bundle bundle)来保存位置 离开列表活动。

然后你可以检查包中的postion(不是null,包含) 的onCreate。在listview上使用setSelection(int)来重置位置。

var query = from obj1 in db.Object1
            join obj2 in db.Object2 on obj1.key equals obj2.somekey
            where obj1.something = "something"
            select obj1

在onCreate

/**
 * Save state on rotation or minimization.
 *
 * @param bundle saved values
 */
public void onSaveInstanceState(Bundle bundle) {
    bundle.putInt(LAST_SELECTED, lastSelected);
    super.onSaveInstanceState(bundle);
}