gpt4 book ai didi

java - 使用 onBackPressed 保持列表位置

转载 作者:行者123 更新时间:2023-12-01 06:11:25 25 4
gpt4 key购买 nike

我目前有一个应用程序,它显示新闻文章列表,然后在用户单击时将其带到完整的 Activity 。该列表很长,因此我希望它在用户点击文章或从文章中点击时保持其位置。

我在 NewsItemActivity 中使用了 onBackPressed,但它似乎不起作用。

这是我的 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();
}
}

最佳答案

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

然后您可以检查 bundle (不为空,包含)中的位置onCreate。在 ListView 上使用 setSelection(int) 重置位置。

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

在onCreate中

    // Check that savedInstanceState is not NULL before referencing
if (savedInstanceState.containsKey(LAST_SELECTED)) {
lastSelected = savedInstanceState.getInt(LAST_SELECTED);
}

关于java - 使用 onBackPressed 保持列表位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34026985/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com