自动从数据库加载数据并显示到列表视图

当前,我想通过从数据库加载数据来创建列表视图。我使用了来自互联网的源代码。我从MySQL数据库中获取数据。加载数据并显示到列表视图成功。但是问题是,只有单击名为“从服务器加载JSON”的按钮,所有数据才会显示在列表视图中。现在,我希望将数据列出到列表视图中,而不需要按下“从服务器加载JSON”按钮

下面是代码:

 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);

    }
}
kissqiao 回答:自动从数据库加载数据并显示到列表视图

将onCreate函数更改为:

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);

}
本文链接:https://www.f2er.com/3169226.html

大家都在问