Android 如果在第二个活动中使用,则RecyclerView在屏幕上不显示任何数据,但如果在启动器活动中使用,则显示列表
在第一个活动(RollDiceActivity.java)中单击按钮后,我将进入第二个活动,我的recyclerview将获取JSON对象(使用截取)。我的代码很简单:Android 如果在第二个活动中使用,则RecyclerView在屏幕上不显示任何数据,但如果在启动器活动中使用,则显示列表,android,android-recyclerview,Android,Android Recyclerview,在第一个活动(RollDiceActivity.java)中单击按钮后,我将进入第二个活动,我的recyclerview将获取JSON对象(使用截取)。我的代码很简单: viewQuotesBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // created explicit intent Intent inten
viewQuotesBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// created explicit intent
Intent intent = new Intent(RollDiceActivity.this,MainActivity.class);
intent.putExtra("addedRandomNo", addedRandomNo);
startActivity(intent);
finish();
}
});
第二个活动-MainActivity.java,其中recyclerView为,但recyclerView未在屏幕上显示任何数据(但Log.d(TAG,response.toString()+“Size:”+response.length());提供了正在获取的所有JSON对象)
package com.subbasishlive.recycleServiceExplanation;
导入android.os.Bundle;
导入android.support.v7.app.AppActivity;
导入android.support.v7.widget.LinearLayoutManager;
导入android.support.v7.widget.RecyclerView;
导入android.util.Log;
导入com.android.volley.Request;
导入com.android.volley.RequestQueue;
导入com.android.volley.Response;
导入com.android.volley.VolleyError;
导入com.android.volley.toolbox.JsonArrayRequest;
导入com.android.volley.toolbox.JsonObjectRequest;
导入com.android.volley.toolbox.volley;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
导入java.util.ArrayList;
导入java.util.List;
导入Adapter.MyAdapter;
导入Model.ListItem;
导入Util.Constants;
导入Util.Prefs;
/**
*由Subbasishnath于2018年11月6日创建。
*/
公共类MainActivity扩展了AppCompatActivity{
私有静态最终字符串标记=“获取长度”;
私人回收站;
专用MyAdapter适配器;
私有列表listItems;//listItems是电影。。。
private RequestQueue;//使用截击
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//我们正在添加队列并传递当前上下文。。。。
queue=Volley.newRequestQueue(MainActivity.this);
//TODO:将添加一个FAB按钮以调用警报对话框进行搜索。。。
//现在,我们将设置我们的recyclerview并设置适配器。
recyclerView=(recyclerView)findViewById(R.id.RecyclerViewW1);
recyclerView.setHasFixedSize(false);
setLayoutManager(新的LinearLayoutManager(MainActivity.this));
//创建Prefs类的实例,调用参数化构造函数
//正在将mainActivity作为参数传递。。。
//Prefs Prefs=新的Prefs(MainActivity.this);
//调用getSearch()方法
//String search=prefs.getSearch();
//ArrayList能够保存任何类型的列表
listItems=new ArrayList();
//listItems设置为getMovies()的返回值,它是一个列表
//因此listItems保存搜索参数返回的所有电影。
listItems=getMovies();
adapter=new MyAdapter(MainActivity.this,listItems);//为我们的listItems创建一个新适配器。
setAdapter(适配器);//为我们的recyclerView设置适配器。
adapter.notifyDataSetChanged();//通知所有注册的观察者数据集已更改。
}
//正在创建获取电影的方法。。。
公共列表getMovies(){
listItems.clear();//首先我们要清除电影列表。。。。
JsonArrayRequest getRequest=newjsonarrayrequest(Request.Method.GET,Constants.base_url+“wp/v2/AllQuotes”,new Response.Listener()){
@凌驾
公共void onResponse(JSONArray响应){
//TODO:response保存URL中的整个JSON对象
试一试{
Log.d(标记,response.toString()+“大小:”+response.length());
//实例化了一个新的JSON数组。
//将JSON数组的名称作为参数传递。
//Search是JSON文件中JSON数组的名称
//JSONArray moviesray=response.getJSONArray(“搜索”);
//现在我通过for循环遍历数组
//该数组中的每个索引元素都包含一个JSON对象。
对于(int i=0;i请尝试此代码.)
listItems.add(movie); // after this line add below code..
adapter.notifyDataSetChanged();
在列表项
列表中添加数据后,您需要通知适配器
试试这个
使用适配器。notifyDataSetChanged();
在getMovies()
方法中的onResponse()
方法中添加数据后
示例代码
public List<ListItem> getMovies(){
listItems.clear(); // first we're clearing the movie list....
JsonArrayRequest getRequest = new JsonArrayRequest(Request.Method.GET, Constants.base_url + "wp/v2/AllQuotes",new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
// TODO:response holds the whole JSON object from the URL
try{
Log.d(TAG, response.toString() + "Size: "+response.length());
// instantiated a new JSON array.
// Pass the name of the JSON array as parameter.
// Search is name of the JSON array from the JSON file
//JSONArray moviesArray = response.getJSONArray("Search");
// Now I'm iterating through the array by a for loop
// every index-element in that array contains a JSON object.
for(int i=0;i<response.length();i++){
// instanciating JSONObject variable to pick a Index item(which is a JSON object) from the JSONArray.
//JSONObject movieObj = moviesArray.getJSONObject(i);
ListItem movie = new ListItem();// creating ListItem object.
Log.d(TAG,"Object at " + i+ response.get(i));
// getting the JSONObject at i-th position of the JSONArray
JSONObject obj = response.getJSONObject(i);
// fetching id of each JSONObject from JSONArray.
movie.setId(obj.getInt("id"));
// title itself is an object so first I'm retrieving that
JSONObject titleObj=obj.getJSONObject("title");
// using title object I'm retrieving the string in it.
movie.setTitle(titleObj.getString("rendered"));
// content itself is an object so first I'm retriving that.
JSONObject contentObj = obj.getJSONObject("content");
// setting up the content
movie.setContent(contentObj.getString("rendered"));
// getting image object and setting up image.
JSONObject imgObj = obj.getJSONObject("better_featured_image");
movie.setPostImg(imgObj.getString("source_url"));
// getting excerpt object and setting up excerpt.
JSONObject excerptObj = obj.getJSONObject("excerpt");
movie.setPostExcerpt(excerptObj.getString("rendered"));
// adding the newly created movie to listItems list...
listItems.add(movie);
}
adapter.notifyDataSetChanged();
}catch(JSONException e){
e.printStackTrace(); // throwing an exception
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// TODO: Handle error
}
});
queue.add(getRequest);
return listItems;
}
public List getMovies(){
listItems.clear();//首先我们要清除电影列表。。。。
JsonArrayRequest getRequest=newjsonarrayrequest(Request.Method.GET,Constants.base_url+“wp/v2/AllQuotes”,new Response.Listener()){
@凌驾
公共void onResponse(JSONArray响应){
//TODO:response保存URL中的整个JSON对象
试一试{
Log.d(标记,response.toString()+“大小:”+response.length());
//实例化了一个新的JSON数组。
//将JSON数组的名称作为参数传递。
//Search是JSON文件中JSON数组的名称
//JSONArray moviesray=response.getJSONArray(“搜索”);
//现在我通过for循环遍历数组
//该数组中的每个索引元素都包含一个JSON对象。
对于(int i=0;我不应该在每次循环迭代中调用它。他应该在添加所有电影后调用它…在循环定义之后。你刚刚救了我的命,这是我的第一个应用程序,我在完成它之前就卡住了。谢谢兄弟:))
listItems.add(movie); // after this line add below code..
adapter.notifyDataSetChanged();
public List<ListItem> getMovies(){
listItems.clear(); // first we're clearing the movie list....
JsonArrayRequest getRequest = new JsonArrayRequest(Request.Method.GET, Constants.base_url + "wp/v2/AllQuotes",new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
// TODO:response holds the whole JSON object from the URL
try{
Log.d(TAG, response.toString() + "Size: "+response.length());
// instantiated a new JSON array.
// Pass the name of the JSON array as parameter.
// Search is name of the JSON array from the JSON file
//JSONArray moviesArray = response.getJSONArray("Search");
// Now I'm iterating through the array by a for loop
// every index-element in that array contains a JSON object.
for(int i=0;i<response.length();i++){
// instanciating JSONObject variable to pick a Index item(which is a JSON object) from the JSONArray.
//JSONObject movieObj = moviesArray.getJSONObject(i);
ListItem movie = new ListItem();// creating ListItem object.
Log.d(TAG,"Object at " + i+ response.get(i));
// getting the JSONObject at i-th position of the JSONArray
JSONObject obj = response.getJSONObject(i);
// fetching id of each JSONObject from JSONArray.
movie.setId(obj.getInt("id"));
// title itself is an object so first I'm retrieving that
JSONObject titleObj=obj.getJSONObject("title");
// using title object I'm retrieving the string in it.
movie.setTitle(titleObj.getString("rendered"));
// content itself is an object so first I'm retriving that.
JSONObject contentObj = obj.getJSONObject("content");
// setting up the content
movie.setContent(contentObj.getString("rendered"));
// getting image object and setting up image.
JSONObject imgObj = obj.getJSONObject("better_featured_image");
movie.setPostImg(imgObj.getString("source_url"));
// getting excerpt object and setting up excerpt.
JSONObject excerptObj = obj.getJSONObject("excerpt");
movie.setPostExcerpt(excerptObj.getString("rendered"));
// adding the newly created movie to listItems list...
listItems.add(movie);
}
adapter.notifyDataSetChanged();
}catch(JSONException e){
e.printStackTrace(); // throwing an exception
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// TODO: Handle error
}
});
queue.add(getRequest);
return listItems;
}