Java RecyclerView不显示任何项目,除非我按Home按钮并重新输入活动
在我第一次打开该应用程序时,它不会在“回收器”视图中显示任何内容,但在我按下“主页”按钮,然后按下“概览”按钮并从那里打开该应用程序后,它会显示项目 以下是mainActivity中的代码Java RecyclerView不显示任何项目,除非我按Home按钮并重新输入活动,java,android,android-recyclerview,Java,Android,Android Recyclerview,在我第一次打开该应用程序时,它不会在“回收器”视图中显示任何内容,但在我按下“主页”按钮,然后按下“概览”按钮并从那里打开该应用程序后,它会显示项目 以下是mainActivity中的代码 public class MainActivity extends AppCompatActivity { private RecyclerView mRecyclerView; private RecyclerView.Adapter mAdapter; private RecyclerView.Layou
public class MainActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mlayoutManager;
private ProgressDialog dialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ArrayList<String> countryNmaeList =new ArrayList<>();
final ArrayList<countryItem> countryList = new ArrayList<>();
final ProgressDialog dialog = new ProgressDialog(this);
dialog.setMessage("Loading data");
mRecyclerView = findViewById(R.id.recyclerView);
mAdapter=new countryAdapter(countryList);
mlayoutManager=new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mlayoutManager);
dialog.show();
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("https://covid-193.p.rapidapi.com/statistics")
.get()
.addHeader("x-rapidapi-host", "covid-193.p.rapidapi.com")
.addHeader("x-rapidapi-key", "xxxxxxxxxxxxx")
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(@NotNull Call call, @NotNull IOException e) {
}
@Override
public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
dialog.dismiss();
String response1=response.body().string();
try {
//geting Jason object
JSONObject jsonObject=new JSONObject(response1);
JSONArray jsonArray = jsonObject.getJSONArray("response");
for (int i=0;i<jsonArray.length();i++){
JSONObject country = jsonArray.getJSONObject(i);
JSONObject cases = country.getJSONObject("cases");
int activecaseint = cases.getInt("active");
int recoveredint =cases.getInt("recovered");
JSONObject death= country.getJSONObject("deaths");
int dtotal = death.getInt("total");
//adding items into country items
countryList.add(new countryItem(country.getString("country"),String.valueOf(activecaseint),String.valueOf(recoveredint),String.valueOf(dtotal)));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
//nottifying the dataset changed
mAdapter.notifyDataSetChanged();
mRecyclerView.setAdapter(mAdapter);
}
}
public类MainActivity扩展了AppCompatActivity{
私人回收视图mRecyclerView;
专用回收器查看适配器mAdapter;
private RecyclerView.LayoutManager MLLayoutManager;
私人对话;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
最终ArrayList CountryNameList=新ArrayList();
最终ArrayList countryList=新ArrayList();
最终进度对话框=新建进度对话框(本);
setMessage(“加载数据”);
mRecyclerView=findViewById(R.id.recyclerView);
mAdapter=新的countryAdapter(countryList);
mlayoutManager=新的LinearLayoutManager(此);
mRecyclerView.setLayoutManager(mllayoutmanager);
dialog.show();
OkHttpClient=新的OkHttpClient();
Request Request=newrequest.Builder()
.url(“https://covid-193.p.rapidapi.com/statistics")
.get()
.addHeader(“x-rapidapi-host”、“covid-193.p.rapidapi.com”)
.addHeader(“x-rapidapi-key”、“XXXXXXXXXXXX”)
.build();
client.newCall(request).enqueue(new Callback()){
@凌驾
public void onFailure(@NotNull Call Call,@NotNull ioe异常){
}
@凌驾
public void onResponse(@NotNull调用,@NotNull响应)引发IOException{
dialog.dismise();
字符串response1=response.body().String();
试一试{
//获取Jason对象
JSONObject JSONObject=新的JSONObject(响应1);
JSONArray JSONArray=jsonObject.getJSONArray(“响应”);
对于(int i=0;i,在将新数据列表添加到列表中后进行更新,如下所示:
public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
dialog.dismiss();
String response1=response.body().string();
try {
//geting Jason object
JSONObject jsonObject=new JSONObject(response1);
JSONArray jsonArray = jsonObject.getJSONArray("response");
for (int i=0;i<jsonArray.length();i++){
JSONObject country = jsonArray.getJSONObject(i);
JSONObject cases = country.getJSONObject("cases");
int activecaseint = cases.getInt("active");
int recoveredint =cases.getInt("recovered");
JSONObject death= country.getJSONObject("deaths");
int dtotal = death.getInt("total");
//adding items into country items
countryList.add(new countryItem(country.getString("country"),String.valueOf(activecaseint),String.valueOf(recoveredint),String.valueOf(dtotal)));
}
//adapter.swapData(countryList);
updateData(countryList);
} catch (JSONException e) {
e.printStackTrace();
}
}
在适配器类中添加此函数,并在需要更新列表时调用它:
public void swapData(ArrayList<countryItem> list) {
if (list != null) {
this.arrayList.clear();
this.arrayList.addAll(list);
notifyDataSetChanged();
}
}
在mainActivity中添加此方法,并在需要更新数据时调用:
public void updateData(ArrayList<countryItem> countryList) {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
mAdapter.swapData(countryList);
}
});
}
public void updateData(ArrayList countryList){
新的处理程序(Looper.getMainLooper()).post(新的Runnable()){
@凌驾
公开募捐{
mAdapter.swapData(国家列表);
}
});
}
当设置新数据时,应该在onCreate()末尾调用mAdapter.notifyDataSetChanged(),而不是在onRespone()中调用它
@Override
public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
dialog.dismiss();
String response1=response.body().string();
try {
//geting Jason object
JSONObject jsonObject=new JSONObject(response1);
JSONArray jsonArray = jsonObject.getJSONArray("response");
for (int i=0;i<jsonArray.length();i++){
JSONObject country = jsonArray.getJSONObject(i);
JSONObject cases = country.getJSONObject("cases");
int activecaseint = cases.getInt("active");
int recoveredint =cases.getInt("recovered");
JSONObject death= country.getJSONObject("deaths");
int dtotal = death.getInt("total");
//adding items into country items
countryList.add(new countryItem(country.getString("country"),String.valueOf(activecaseint),String.valueOf(recoveredint),String.valueOf(dtotal)));
}
mAdapter.notifyDataSetChanged(); // ← notify adapter here!
} catch (JSONException e) {
e.printStackTrace();
}
}
@覆盖
public void onResponse(@NotNull调用,@NotNull响应)引发IOException{
dialog.dismise();
字符串response1=response.body().String();
试一试{
//获取Jason对象
JSONObject JSONObject=新的JSONObject(响应1);
JSONArray JSONArray=jsonObject.getJSONArray(“响应”);
对于(int i=0;我是否有onPause()
或onResume
方法?如果有,请也共享它们。否我没有任何onPause或onResume方法我尝试过,但出现错误非静态方法swapData(ArrayList)无法从静态上下文countryAdapter引用。swapData(countryList);因为您是使用countryAdapter类的class name.use对象直接访问它来调用此方法:mAdapter.swapData(countryList);当我使用mAdapter.swapData(countryList)时;它显示无法解析方法swapData是的,我在问题中添加了我的适配器类
private countryAdapter mAdapter;
public void updateData(ArrayList<countryItem> countryList) {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
mAdapter.swapData(countryList);
}
});
}
@Override
public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
dialog.dismiss();
String response1=response.body().string();
try {
//geting Jason object
JSONObject jsonObject=new JSONObject(response1);
JSONArray jsonArray = jsonObject.getJSONArray("response");
for (int i=0;i<jsonArray.length();i++){
JSONObject country = jsonArray.getJSONObject(i);
JSONObject cases = country.getJSONObject("cases");
int activecaseint = cases.getInt("active");
int recoveredint =cases.getInt("recovered");
JSONObject death= country.getJSONObject("deaths");
int dtotal = death.getInt("total");
//adding items into country items
countryList.add(new countryItem(country.getString("country"),String.valueOf(activecaseint),String.valueOf(recoveredint),String.valueOf(dtotal)));
}
mAdapter.notifyDataSetChanged(); // ← notify adapter here!
} catch (JSONException e) {
e.printStackTrace();
}
}