M
Michael68
Neues Mitglied
- 0
Hallo zusammen,
ich bin Anfänger in der App programmierung und habe mir da mal 2 Tutorials zusammen gewürfelt
Ich habe nur noch ein klitze kleines Problemchen.
Es geht darum .....
ich möchte von meiner Datenbank aus dem Internet eine Liste mit den Daten in zwei Spalten augegeben bekommen.
In der Zeit wo die Daten laden soll ein ProgressDialog laufen.
Das einzige Problem ist im Moment das das nachfolgende Script mir nur den letzten Datensatz anzeigt, obwohl es zur Zeit vier sein sollten.
Könnte mir einer etwas helfen ?
Gruß Michael
die xml ist
ich bin Anfänger in der App programmierung und habe mir da mal 2 Tutorials zusammen gewürfelt
Ich habe nur noch ein klitze kleines Problemchen.
Es geht darum .....
ich möchte von meiner Datenbank aus dem Internet eine Liste mit den Daten in zwei Spalten augegeben bekommen.
In der Zeit wo die Daten laden soll ein ProgressDialog laufen.
Das einzige Problem ist im Moment das das nachfolgende Script mir nur den letzten Datensatz anzeigt, obwohl es zur Zeit vier sein sollten.
Könnte mir einer etwas helfen ?
Gruß Michael
PHP:
package de.htmlinside;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.app.ActionBar.LayoutParams;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
public class Anfrage extends Activity implements Runnable{
ProgressDialog progress;
String result=null;
InputStream is;
JSONObject json_data;
TableLayout table;
TextView columnOneText;
TextView columnTwoText;
String columnOneString;
String columnTwoString;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.anfrage);
initThread();
}
public void initThread() {
progress = new ProgressDialog(Anfrage.this);
progress.setTitle("Bitte Gedult");
progress.setMessage("Daten werden geladen...");
progress.show();
Thread thread = new Thread(this);
thread.start();
}
@Override
public void run() {
initView();
handler.sendEmptyMessage(0);
}
@SuppressLint("HandlerLeak")
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg){
progress.dismiss();
fillList();
}
};
private void initView() {
result = doRequest();
}
private String doRequest() {
String result = "";
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.XXXX.de/XXX.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Fehler bei der http Verbindung "+e.toString());
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "n");
}
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
try {
JSONArray jArray = new JSONArray(result);
for(int i=0;i<jArray.length();i++){
json_data = jArray.getJSONObject(i);
columnOneString = json_data.getString("userid").toString();
columnTwoString = json_data.getString("login").toString();
// fillList();
}
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
return result;
}
// Array 'cListArray' wird in die listView geschrieben
@SuppressLint("InlinedApi") public void fillList() {
table = (TableLayout) findViewById(R.id.TableLayout01);
TableRow row = new TableRow(this);
columnOneText = new TextView(this);
columnTwoText = new TextView(this);
columnOneText.setText(columnOneString);
columnTwoText.setText(columnTwoString);
row.addView(columnOneText);
row.addView(columnTwoText);
table.addView(row,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
}
PHP:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ScrollView
android:id="@+id/ScrollView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TableLayout
android:id="@+id/TableLayout01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="0">
<TableRow
android:id="@+id/TableRow01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/TextView01"
android:layout_width="150dp"
android:layout_height="150dp"
android:text="UserID"/>
<TextView
android:id="@+id/TextView02"
android:layout_width="150dp"
android:layout_height="150dp"
android:text="Name"/>
</TableRow>
</TableLayout>
</ScrollView>
</LinearLayout>