猿问

Activity 重新加载先前选定的数据错误

所以我开发了一个android新闻应用程序。工具栏上有一个微调器项目,数据会根据微调器选择重新加载。但是,当数据重新加载时,对于某些项目,它会直接加载相关内容,而对于其他一些项目,它会加载先前选择的微调器数据。当前项目的当前数据仅在使用我实现的滑动刷新功能重新加载相同的微调器数据后显示。我希望与该选择相关的正确数据第一次加载本身而不必一直刷新。

下面是我的 MainActivity,其中调用了所有这些函数。

package com.example.app.activities;


import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.DialogInterface;

import android.content.Intent;

import android.graphics.Color;

import android.net.Uri;

import android.os.Build;

import android.os.Bundle;

import android.support.annotation.NonNull;

import android.support.design.widget.AppBarLayout;

import android.support.design.widget.BottomNavigationView;

import android.support.design.widget.CoordinatorLayout;

import android.support.v4.app.Fragment;

import android.support.v4.app.FragmentManager;

import android.support.v4.app.FragmentPagerAdapter;

import android.support.v4.view.ViewPager;

import android.support.v7.app.AlertDialog;

import android.support.v7.app.AppCompatActivity;

import android.support.v7.widget.Toolbar;

import android.view.LayoutInflater;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.view.ViewGroup;

import android.widget.AdapterView;

import android.widget.ArrayAdapter;

import android.widget.ImageView;

import android.widget.Spinner;

import android.widget.TextView;

import android.widget.Toast;


import com.android.volley.DefaultRetryPolicy;

import com.android.volley.Request;

import com.android.volley.RequestQueue;

import com.android.volley.Response;

import com.android.volley.RetryPolicy;

import com.android.volley.VolleyError;

import com.android.volley.toolbox.StringRequest;

import com.android.volley.toolbox.Volley;

import com.google.firebase.messaging.FirebaseMessaging;

import com.squareup.picasso.Picasso;


import org.json.JSONArray;

import org.json.JSONException;

import org.json.JSONObject;


import java.util.ArrayList;


犯罪嫌疑人X
浏览 155回答 1
1回答

天涯尽头无女友

我发现从 API 级别 3 开始,可以在具有布尔值的 Activity 上使用 onUserInteraction() 从而确定用户的交互。在下面的链接中找到文档。http://developer.android.com/reference/android/app/Activity.html#onUserInteraction()@Overridepublic void onUserInteraction() {&nbsp; &nbsp; &nbsp;super.onUserInteraction();&nbsp; &nbsp; &nbsp;userInteracts = true;}我的调整在 MainActivity.java 中如此mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {&nbsp; &nbsp; &nbsp; &nbsp; int check = 0;&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (IsInteracting) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Intent intent = getIntent(); //MainActivity&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String itemSelected = mySpinner.getItemAtPosition(mySpinner.getSelectedItemPosition()).toString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //TODO: DONE&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; prefManager.spinWriteString(itemSelected);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(getApplicationContext(), "Constituency News Now Showing", Toast.LENGTH_SHORT).show();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; startActivity(intent);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; finish();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }
随时随地看视频慕课网APP

相关分类

Java
我要回答