ich wurde durch das Tutorial von L3322 inspiriert und habe das ganze für TabelLayout implementiert.
Diese Tabelle ist dynamisch und stellt alle MySQL Ergebnisse in zwei Spalten dar.
Für Feedback wäre ich sehr dankbar, da dieser Code mein erstes Posting im Entwickler Forum ist.
Die Vorarbeit ist dem original Tutorial von L3322 zu entnehmen.
Die PHP-Datei dem Beispiel entsprechend angepasst:
1<?php
2 mysql_connect("deinhost","deinusername","deinpasswort");
3 mysql_select_db("deinedatenbank");
4
5 $q=mysql_query("SELECT column1, column2 FROM table");
6 while($e=mysql_fetch_assoc($q))
7 $output[]=$e;
8
9 print(json_encode($output));
10
11 mysql_close();
12?>
2 mysql_connect("deinhost","deinusername","deinpasswort");
3 mysql_select_db("deinedatenbank");
4
5 $q=mysql_query("SELECT column1, column2 FROM table");
6 while($e=mysql_fetch_assoc($q))
7 $output[]=$e;
8
9 print(json_encode($output));
10
11 mysql_close();
12?>
Das XML-Tabellen-Layout (layout.xml):
1<?xml version="1.0" encoding="utf-8"?>
2<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:orientation="vertical"
4 android:layout_width="fill_parent"
5 android:layout_height="fill_parent"
6 >
7 <ScrollView android:id="@+id/ScrollView01" android:layout_width="wrap_content" android:layout_height="wrap_content">
8 <TableLayout android:id="@+id/TableLayout01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:stretchColumns="0">
9
10 <TableRow android:id="@+id/TableRow01" android:layout_width="wrap_content" android:layout_height="wrap_content">
11 <TextView android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="column1"</TextView>
12
13 <TextView android:id="@+id/TextView02" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="column2"></TextView>
14 </TableRow>
15
16 </TableLayout>
17 </ScrollView>
18</LinearLayout>
2<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:orientation="vertical"
4 android:layout_width="fill_parent"
5 android:layout_height="fill_parent"
6 >
7 <ScrollView android:id="@+id/ScrollView01" android:layout_width="wrap_content" android:layout_height="wrap_content">
8 <TableLayout android:id="@+id/TableLayout01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:stretchColumns="0">
9
10 <TableRow android:id="@+id/TableRow01" android:layout_width="wrap_content" android:layout_height="wrap_content">
11 <TextView android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="column1"</TextView>
12
13 <TextView android:id="@+id/TextView02" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="column2"></TextView>
14 </TableRow>
15
16 </TableLayout>
17 </ScrollView>
18</LinearLayout>
Nun die zugehörige Klasse (die von mir geänderte Teile des original Tutorial sind "fett" markiert):
1public class testMySQL extends Activity {
2 InputStream is;
3 JSONObject json_data;
4 TableLayout table;
5 TextView columnOneText;
6 TextView columnTwoText;
7 String columnOneString;
8 String columnTwoString;
9
10 @Override
11 public void onCreate(Bundle savedInstanceState) {
12 super.onCreate(savedInstanceState);
13 setContentView(R.layout.layout);
14
15 getData();
16 }
17
18 public void getData() {
19 String result = "";
20 ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
21
22 try {
23 HttpClient httpclient = new DefaultHttpClient();
24 HttpPost httppost = new HttpPost("http://deinehomepage.de/deinephpDatei.php");
25 httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
26 HttpResponse response = httpclient.execute(httppost);
27 HttpEntity entity = response.getEntity();
28 is = entity.getContent();
29 }catch(Exception e){
30 Log.e("log_tag", "Fehler bei der http Verbindung "+e.toString());
31 }
32
33 try {
34 BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
35 StringBuilder sb = new StringBuilder();
36 String line = null;
37 while ((line = reader.readLine()) != null) {
38 sb.append(line + "n");
39 }
40 is.close();
41 result=sb.toString();
42 }catch(Exception e){
43 Log.e("log_tag", "Error converting result "+e.toString());
44 }
45
46 try {
47 JSONArray jArray = new JSONArray(result);
48 for(int i=0;i<jArray.length();i++){
49 json_data = jArray.getJSONObject(i);
50 columnOneString = json_data.get("column1").toString();
51 columnTwoString = json_data.get("column2").toString();
52 fillList();
53 }
54 }catch(JSONException e){
55 Log.e("log_tag", "Error parsing data "+e.toString());
56 }
57 }
58
59 public void fillList() {
60
61 table = (TableLayout) findViewById(R.id.TableLayout01);
62 TableRow row = new TableRow(this);
63 columnOneText = new TextView(this);
64 columnTwoText = new TextView(this);
65 columnOneText.setText(columnOneString);
66 columnTwoText.setText(columnTwoString);
67 row.addView(columnOneText);
68 row.addView(columnTwoText);
69
70 table.addView(row,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
71 }
72}
2 InputStream is;
3 JSONObject json_data;
4 TableLayout table;
5 TextView columnOneText;
6 TextView columnTwoText;
7 String columnOneString;
8 String columnTwoString;
9
10 @Override
11 public void onCreate(Bundle savedInstanceState) {
12 super.onCreate(savedInstanceState);
13 setContentView(R.layout.layout);
14
15 getData();
16 }
17
18 public void getData() {
19 String result = "";
20 ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
21
22 try {
23 HttpClient httpclient = new DefaultHttpClient();
24 HttpPost httppost = new HttpPost("http://deinehomepage.de/deinephpDatei.php");
25 httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
26 HttpResponse response = httpclient.execute(httppost);
27 HttpEntity entity = response.getEntity();
28 is = entity.getContent();
29 }catch(Exception e){
30 Log.e("log_tag", "Fehler bei der http Verbindung "+e.toString());
31 }
32
33 try {
34 BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
35 StringBuilder sb = new StringBuilder();
36 String line = null;
37 while ((line = reader.readLine()) != null) {
38 sb.append(line + "n");
39 }
40 is.close();
41 result=sb.toString();
42 }catch(Exception e){
43 Log.e("log_tag", "Error converting result "+e.toString());
44 }
45
46 try {
47 JSONArray jArray = new JSONArray(result);
48 for(int i=0;i<jArray.length();i++){
49 json_data = jArray.getJSONObject(i);
50 columnOneString = json_data.get("column1").toString();
51 columnTwoString = json_data.get("column2").toString();
52 fillList();
53 }
54 }catch(JSONException e){
55 Log.e("log_tag", "Error parsing data "+e.toString());
56 }
57 }
58
59 public void fillList() {
60
61 table = (TableLayout) findViewById(R.id.TableLayout01);
62 TableRow row = new TableRow(this);
63 columnOneText = new TextView(this);
64 columnTwoText = new TextView(this);
65 columnOneText.setText(columnOneString);
66 columnTwoText.setText(columnTwoString);
67 row.addView(columnOneText);
68 row.addView(columnTwoText);
69
70 table.addView(row,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
71 }
72}
Danke an L3322 für die Verwendung seiner Implementierung.
