2012-02-23 56 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