2012-02-23 51 views
0

我需要一些幫助與問候的是,以顯示存儲在MySQL數據庫中的一個ListView數據!顯示數據饋送

我的目標有ListView控件類似於此截圖: http://4.bp.blogspot.com/_K6NH2gxxhAc/Swg0hqNDlTI/AAAAAAAAEQU/4qG-YjJRuBM/s1600/Android_ButtonOnListView.png

的想法是創建一個類似於使用數據已存儲在MySQL數據庫中一個Twitter的飼料。但是,我很不清楚如何從表中的數據製作這個「feed」。例如,我在我的mysql表中有兩個名爲Title和Content的字段,如何以類似於該屏幕截圖的方式將這些字段顯示給用戶? ListView是實現這一目標的最佳方式嗎?

到目前爲止,我一直在關注這個教程從數據庫中把數據細分:

http://fahmirahman.wordpress.com/2011/04/21/connection-between-php-server-and-android-client-using-http-and-json/

,我可以張貼到目前爲止我的代碼是否會有所幫助,但我的天堂」提前

感謝牛逼了工作的ListView,所以只能通過打印MySQL的數據是這樣的:[{「又見面了!」「標題」:「世界,你好!」,「內容」}]!

+0

http://www.vogella.de/articles/AndroidListView/article.html#cursor – maebe 2012-02-28 11:53:34

回答

1

如果你有[{"Title":"Hello World!","Content":"Hello again!"}]一個PHP輸出你應該能夠安裝該應用程序檢索數據,分析它,並把它插入到這些行。

下面是Android和PHP頁面之間的HTTP交互的例子:

HttpClient client = new DefaultHttpClient(); 
HttpPost post = new HttpPost("http://www.website.com/page.php"); 

try { 
    List<NameValuePair> pairs = new ArrayList<NameValuePair>(); 
    pairs.add(new BasicNameValuePair("username", username)); //POST method adding username to request 
    pairs.add(new BasicNameValuePair("password", password)); //POST method adding password to request 
    post.setEntity(new UrlEncodedFormEntity(pairs)); 
    HttpResponse response = client.execute(post); 
    ResponseHandler<String> handler = new BasicResponseHandler(); 
    String result = handler.handleResponse(response); 

    JSONArray jArray = new JSONArray(result); 

} catch(Exception e) { 
    e.printStackTrace(); 
} 

從這裏只需通過JSON解析數組,並插入項目到列表中。

如果你有麻煩的是JSONArray的數據到你的列表,請參閱從Android的官方網站本教程:http://developer.android.com/resources/tutorials/notepad/index.html