Android RecyclerView Does Not Update

I am trying to add some values to my recyclerView adapter which I took from JSON. I am taking values correctly but it is giving this log:

java.lang.NullPointerException:Attempt to invoke virtual method
‘int java.util.ArrayList.size()’ on a null object reference at: com.example.asus.drawer_app.MyAdapter.getItemCount(MyAdapter.java:55).

My adapter is defined follow:

public class MyAdapter extends RecyclerView.Adapter {

private ArrayList itemsData;

public MyAdapter(ArrayList<ItemData> itemsData) {
    this.itemsData = itemsData;
    notifyDataSetChanged();

}

// Create new views (invoked by the layout manager)
@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                               int viewType) {
    // create a new view
    View itemLayoutView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.fragment_news, null);

    // create ViewHolder

    ViewHolder viewHolder = new ViewHolder(itemLayoutView);
    return viewHolder;
}

// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {

    // - get data from your itemsData at this position
    // - replace the contents of the view with that itemsData

    viewHolder.txtViewDate.setText(itemsData.get(position).getDate());
    viewHolder.txtViewTitle.setText(itemsData.get(position).getTitle());
    viewHolder.txtViewDescription.setText(itemsData.get(position).getDescription());
    viewHolder.imgViewIcon.setText(itemsData.get(position).getImageUrl());



}

@Override
public int getItemCount() {
    return itemsData.size();
}

// inner class to hold a reference to each item of RecyclerView
public static class ViewHolder extends RecyclerView.ViewHolder {

    public TextView txtViewDate;
    public TextView txtViewTitle;
    public TextView txtViewDescription;
    public TextView imgViewIcon;
    public ViewHolder(View itemLayoutView) {
        super(itemLayoutView);
        txtViewTitle = (TextView) itemLayoutView.findViewById(R.id.item_title);
        txtViewDate = (TextView) itemLayoutView.findViewById(R.id.item_date);
        txtViewDescription = (TextView) itemLayoutView.findViewById(R.id.item_description);
        imgViewIcon = (TextView) itemLayoutView.findViewById(R.id.item_icon);
    }
}



// Return the size of your itemsData (invoked by the layout manager)

}

And my fragment code is :

public class NewsFragment extends Fragment{

String getItemsUrl="http://phptest.basarimobile.com/BoxFeed/ws.php?method=getItems&token=test&rssID=1&lastID=190";
ArrayList<ItemData> itemsData = null;


public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);

    new JSONGetNews().execute();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_news, container, false);


    RecyclerView recyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerView);

    // 2. set layoutManger
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    // 3. create an adapter

    MyAdapter mAdapter = new MyAdapter(itemsData);

    // 4. set adapter
    recyclerView.setAdapter(mAdapter);

    // 5. set item animator to DefaultAnimator
    recyclerView.setItemAnimator(new DefaultItemAnimator());

    return rootView;
}


private class JSONGetNews extends AsyncTask<String, String, JSONObject> {

    private ProgressDialog pDialog;

    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(getActivity());
        pDialog.setMessage("Getting Data ...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();

    }

    @Override
    protected JSONObject doInBackground(String... args) {
        JSONParser jParser = new JSONParser();


        JSONObject json = jParser.getJSONFromUrl(getItemsUrl);
        return json;
    }

    protected void onPostExecute(JSONObject json) {
        pDialog.dismiss();
        try {
            // Getting JSON Array
            JSONArray titleArr = json.getJSONObject("data").getJSONArray("results");

            for (int i = 0; i < titleArr.length(); i++) {
                final JSONObject titleObj = titleArr.getJSONObject(i);
                String title = titleObj.getString("title");
                String description = titleObj.getString("description");
                String imgUrl = titleObj.getString("img");
                String date = titleObj.getString("date");


           itemsData.add(new ItemData(title, date, imgUrl, description));

            }
            }catch(JSONException e){
                e.printStackTrace();
            }
        }


}

I think itemsData is NULL. If it is null, how can I send it to adapter? And how can I update adapter.?

Thanks For Help.


Source: java

Leave a Reply