0

currently I want to create list view by load the data from the database. I used the source code from the internet. I fetch the data from the MySQL Database. load data and display to the list view is successful. But the issue is all the data will be display in the listview only if I click a button named "Load JSON from server". Now, I want the data listed to the list view without pushing the button "Load JSON from server"

Below is the code:

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Assign ID's to ListView.
    listView = (ListView) findViewById(R.id.listView1);

    button = (Button)findViewById(R.id.button);

    progressBar = (ProgressBar)findViewById(R.id.ProgressBar1);

    // Adding click listener to button.
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            //Showing progress bar just after button click.
            progressBar.setVisibility(View.VISIBLE);

            // Creating StringRequest and set the JSON server URL in here.
            StringRequest stringRequest = new StringRequest(HTTP_URL,
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {

                            // After done Loading store JSON response in FinalJSonObject string variable.
                            FinalJSonObject = response ;

                            // Calling method to parse JSON object.
                            new ParseJSonDataClass(MainActivity.this).execute();

                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {

                            // Showing error message if something goes wrong.
                            Toast.makeText(MainActivity.this,error.getMessage(),Toast.LENGTH_LONG).show();

                        }
                    });

            // Creating String Request Object.
            RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);

            // Passing String request into RequestQueue.
            requestQueue.add(stringRequest);

        }
    });
}

// Creating method to parse JSON object.
private class ParseJSonDataClass extends AsyncTask<Void, Void, Void> {

    public Context context;

    // Creating List of Subject class.
    List<Subject> CustomSubjectNamesList;

    public ParseJSonDataClass(Context context) {

        this.context = context;
    }

    @Override
    protected void onPreExecute() {

        super.onPreExecute();
    }

    @Override
    protected Void doInBackground(Void... arg0) {

        try {

            // Checking whether FinalJSonObject is not equals to null.
                if (FinalJSonObject != null) {

                    // Creating and setting up JSON array as null.
                    JSONArray jsonArray = null;
                    try {

                        // Adding JSON response object into JSON array.
                        jsonArray = new JSONArray(FinalJSonObject);

                        // Creating JSON Object.
                        JSONObject jsonObject;

                        // Creating Subject class object.
                        Subject subject;

                        // Defining CustomSubjectNamesList AS Array List.
                        CustomSubjectNamesList = new ArrayList<Subject>();

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

                            subject = new Subject();

                            jsonObject = jsonArray.getJSONObject(i);

                            //Storing ID into subject list.
                            subject.Subject_ID = jsonObject.getString("id");

                            //Storing Subject name in subject list.
                            subject.Subject_Name = jsonObject.getString("subject_Name");

                            // Adding subject list object into CustomSubjectNamesList.
                            CustomSubjectNamesList.add(subject);
                        }
                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result)

    {
        // After all done loading set complete CustomSubjectNamesList with application context to ListView adapter.
            ListViewAdapter adapter = new ListViewAdapter(CustomSubjectNamesList, context);

        // Setting up all data into ListView.
            listView.setAdapter(adapter);

        // Hiding progress bar after all JSON loading done.
        progressBar.setVisibility(View.GONE);

    }
}
2
  • you need to call api in background and get the data. Commented Nov 4, 2019 at 4:00
  • the how about the button? Commented Nov 4, 2019 at 4:03

1 Answer 1

2

Change onCreate function to:

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Assign ID's to ListView.
listView = (ListView) findViewById(R.id.listView1);

button = (Button)findViewById(R.id.button);

progressBar = (ProgressBar)findViewById(R.id.ProgressBar1);


        //Showing progress bar just after button click.
        progressBar.setVisibility(View.VISIBLE);

        // Creating StringRequest and set the JSON server URL in here.
        StringRequest stringRequest = new StringRequest(HTTP_URL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {

                        // After done Loading store JSON response in FinalJSonObject string variable.
                        FinalJSonObject = response ;

                        // Calling method to parse JSON object.
                        new ParseJSonDataClass(MainActivity.this).execute();

                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                        // Showing error message if something goes wrong.
                        Toast.makeText(MainActivity.this,error.getMessage(),Toast.LENGTH_LONG).show();

                    }
                });

        // Creating String Request Object.
        RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);

        // Passing String request into RequestQueue.
        requestQueue.add(stringRequest);

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.