Search code examples
androidarraylistdatamodel

Dynamic created data not inserted global scope Arraylist data variable


I am trying to get data from internet and that will be populated in listview. I have fully functional Custom adapter named NoticeAdapter. Arraylist is not append with dynamically created object.

What is my mistake ? It already took my 8 hours to find problem.But not find. No error shown but not list view is created. If I make a simple one or two object in onCreate method then it added to list . it works fine .

Please Anyone help me. Thanks in Advance

ListView noticeList;
NoticeAdapter na;
public String title, file_link, dept, pubDate;

Here I Decleare data global variable

public ArrayList<NoticeModel> data = new ArrayList<>();
String serverUrl = "http://192.168.0.104/virtualclassroomapi/notice.php";


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_notice_board);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);



    noticeList = (ListView) findViewById(R.id.noticelist);

Here I call to get data from Internet and save it as object in data

    getDataFromServer();

If here I call "getData();" then it will worked

    na = new NoticeAdapter(data, getApplicationContext());
    noticeList.setAdapter(na);

}



public void getDataFromServer(){



    StringRequest request = new StringRequest(Request.Method.POST, serverUrl, new Response.Listener<String>() {


        @Override
        public void onResponse(String response) {

            try {
                JSONArray jsonArray = new JSONArray(response);

                for (int i =0; i<jsonArray.length();i++)
                {
                    try {

                        JSONObject jsonObject = jsonArray.getJSONObject(i);
                        title = jsonObject.getString("title");
                        file_link = jsonObject.getString("file_link");
                        dept = jsonObject.getString("dept");
                        pubDate = jsonObject.getString("publish_date");

I test Response from server fine using Toast. but not append to data(Arraylist) global variable as new object

                        data.add(new NoticeModel(title,file_link,dept,pubDate));





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

                }


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


        }


    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(NoticeBoard.this,"Response Error",Toast.LENGTH_LONG).show();
        }
    });

    MySingleton.getInstance(NoticeBoard.this).addToRequestQueue(request);


}

Data from here inserted but not inserted from there, If I call it instead of getDataFromServer()

public void getData(){



    String title = "title from varable";
    data.add(new NoticeModel(title,"something", "get","sthe"));

}

Solution

  • after adding data to your adapter you should to tell adapter has been changed, so call na.notifyDataSetChanged();