ken:
I organize all the Api Calling method of my app inside a separate Class.So when I get the response from the server,I insert it into a Model class.But the problem is,I cant notifyDataSetChanged()
(from the class I parsing the JSON) after I parsing all the JSON. Therefore,my adapter's getItemCount()
value is always be 0.Ended up the recyclerView
never have data.
Here is my Fragment.class
RecyclerView recyclerView;
LatestAdapter latestAdapter;
ListlatestItems;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_latest, container, false);
// Initilize the RecyclerView and Adapter
recyclerView =(RecyclerView)view.findViewById(R.id.recycler_view);
latestAdapter = new LatestAdapter(getContext(),latestItems);
final LinearLayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setAdapter(latestAdapter);
fetchIntialPost();
}
private void fetchIntialPost() {
//here I make API call in another class,which is all API calling method in that class
ApiHelper apiHelper = new ApiHelper();
apiHelper.fetchIntialPost();
}
Here my ApiHelper (This class contains all the API calling method using Volley )
public class ApiHelper {
public void fetchIntialPost(){
JsonObjectRequest jsonReq = new JsonObjectRequest(Request.Method.GET, MY_URL ,null, new Response.Listener() {
@Override
public void onResponse(JSONObject response) {
Log.d("Volley response 123",response.toString());
//here I parse the response in another class
//this class only doing JSON parsing operation
ItemHelper itemHelper = new ItemHelper();
itemHelper.parseItemJsonFeed(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// VolleyLog.d(AppController.TAG, "Error: " + error.getMessage());
}
}) {
@Override
public MapgetHeaders() throws AuthFailureError {
Mapheaders = new HashMap();
Context applicationContext = MainActivity.getContextOfApplication();
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(applicationContext);
String apiKey = preferences.getString("apiKey","");
headers.put("Content-Type", "application/json");
headers.put("authorization",apiKey);
return headers;
}
};
// Adding request to volley request queue
AppController.getInstance().addToRequestQueue(jsonReq);
}
}
Here is my ItemHelper class (The class only using to JSON parsing)
public class ItemHelper {
private List- items = new ArrayList();
public ListgetList() {
return items;
}
public void parseItemJsonFeed (JSONObject response){
try {
JSONObject jObj = response.getJSONObject("return_data");
//here parsing all the data from the response
******
//After all the parsing,I save it to the model
//here set all the data to the model
setItemToFeedArrayList(id);
//****** here is the problem,I can `notifyDataSetChanged()`
//*****For the adapter in Fragment Class
//I cant access from this class here
Fragment fragment = new Fragment();
fragment.getLatestAdapter().notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
private void setItemToFeedArrayList(int id ) {
Item item = new Item();
item.setId(id);
//add item to the list
items.add(item);
}
Problems:
My problems now is,I cant access the LatestAdapter
object in my Fragment.class .Therefore I cant call notifyDataSetChanged()
after I parsing response Json.
What I tried
I tried to make a method like below in ItemHelper Class :
public List
getList() {
return items;
}
In LatestAdapter,I also create a method like this:
public void setItem(List
- item) {
this.latestItems = item;
notifyDataSetChanged();
}
And then in Fragment.class I access it like this
ItemHelper itemHelper = new ItemHelper();
latestItems = itemHelper.getList();
latestAdapter.setPosts(latestItems);
With what I tried above,the getItemCount()
of LatestAdapter
still 0,after the API call.
So somebody please give me a hint to solve this problem
,or a better solution for organize all API calling method in 1 class,all Json Parsing Operation in 1 class.At the same time can notifyDataSetChanged()
for the adapter as well.
Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots