0

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

RED_
  • 2,997
  • 4
  • 40
  • 59
  • 1
    I dont quite understand what this has to do with PHP... – Serguei Fedorov Aug 24 '12 at 17:25
  • Crap, sorry about that. I didn't even think when I added it. Using a PHP script to connect to the database but obviously it's irrelevant to this. Removed. – RED_ Aug 24 '12 at 17:29
  • If possible, could you provide the output for the JSON? It could be something as simple as your parsing is not aligning with how the JSON is formatted. – Serguei Fedorov Aug 24 '12 at 17:31
  • There's no errors here, I just don't know how to code it so that the data gets put into tables or a listview as mentioned below. At the moment it's doing something pretty basic but it's all I know. Can't find many tutorials about doing anything else with the data. – RED_ Aug 24 '12 at 17:39
  • Ah, lol. Ok, a listView if memory serves me well enough, is simply a list or an array put applied to the listview you have set for the interface. Check out this tutorial it might help http://mobile.tutsplus.com/tutorials/android/android-listview/ basically every time your for loop loops, append the result to some kind of datastructure (such as a list or an array). Then, if you look at the tutorial, you simply bind it to the listview setListAdapter(ArrayAdapter.createFromResource(getApplicationContext(), R.array.tut_titles, R.layout.list_item)); – Serguei Fedorov Aug 24 '12 at 17:47
  • Here is another question that will help on how to work with lists and listviews http://stackoverflow.com/questions/4540754/add-dynamically-elements-to-a-listview-android – Serguei Fedorov Aug 24 '12 at 17:49

1 Answers1

0

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.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • Thanks, the only reason I mentioned HTTP POST was because I looked up some tutorials and they were going about it differently which confused me. Can you guide me to any tutorials of bits of example codes on how to do this with ListViews? The code I posted above is about all I've learnt in regards to using data once I've retrieved it. – RED_ Aug 24 '12 at 17:31
  • there is hell lot of tutorials on listviews. Just google for one and follow it. I.e. this one looks basic and easy: http://www.androidhive.info/2011/10/android-listview-tutorial/ then drop resource-based array in favour of in-code ArrayList and update the code to work. Once you got that you mostly home - you load your data from remote server, put into arraylist and display (or if you want more persistent storage, look for tutorials on android database and listview with db adapters. – Marcin Orlowski Aug 24 '12 at 18:54