简体   繁体   中英

Displaying data taken from a remote database in an android app? The display bit

Possibly a noob question but hopefully I can learn how to do this for the future as well.

I've connected to a mysql database on my server, connects fine, everything works. The problem is I don't know how to get that data into a table for me to use in the layout (Is a tablelayout the best way? I don't want it to fill up an entire layout, I want to be able to add text above the table and below). The code below prints the data into the logcat. You can see for yourself, it's quite basic. I've been learning to do this via various tutorials. Obviously it's not what I want though.

Does anyone have any advice on what code I should be putting here so that I can actually use my data once the app has received it. It's already in the JSON format as you can see below. The code below works, it's just not what I want it to do. The tutorials I've seen have implemented the HTTP connection and use ArrayAdaptors together but I've already done the HTTP POST bit and don't want to change it since it took me ages.

// PARSE STRING
            try {
                jArray = new JSONArray(result);
                JSONObject json_data = null;

                System.out.println("Length " + jArray.length());
                Log.d("DB", "Length " + jArray.length());

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

                    json_data = jArray.getJSONObject(i);
                    drivername = json_data.getString("Driver_full_name");
                    drivesfor = json_data.getString("Drives_for");

                    System.out.println(drivername + "&" + drivesfor);
                }
            } catch (JSONException e1) {
                Log.d("DB", "Error somewhere");
                CurrentSeasonDrivers.this.runOnUiThread(new Runnable() { 
                    public void run() {
                        Toast.makeText(CurrentSeasonDrivers, "Could not convert data to string", Toast.LENGTH_LONG).show(); } });
            }

Any advice is appreciated! My full code including the HTTP connection to show you how I've set things up: http://pastebin.com/MsKc1xPg

If you got tabular data with common schema, you may want to use ListView to display it. Each row can be literally whatever you want and using ListView automatically solves your problem is you got more rows of data to display. And HTTP POST got nothing do to with it at all. Once you got your data it is irrelevant where it came from. Put it in database, put it in array, ArrayList etc. And display. ListView may be the way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM