drtpbx3606 2016-06-21 21:37
浏览 275

从MySQL数据库中检索数据并将其显示在列表视图中

I am trying to retrieve data from a MySQL databse, which is hosted in Microsoft Azure, and to display it in a listview( in a fragment). I have followed the indications of this post:

Retrieving data from Mysql DB with PHP and JSON in Xamarin Android C#.NET

But I am still getting a blank list. I think the error comes from my PHP as when I run my PHP script I get nothing in my browser. Here is my code:

PHP:

 <?php
  ini_set('display_errors', 1); 
  error_reporting(E_ALL);

  $servername = "eu-cdbr-azure-north-e.cloudapp.net";
  $username = "****************";
  $password = "**********";

 try 
 { 
 $conn = new PDO("mysql:host=$servername;dbname=******",$username,        $password);
 $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
 }
 catch(PDOException $e)
 {
echo "Connection failed: " . $e->getMessage();
 }

  $query = $conn->prepare("SELECT FROM contact_info");
 $rows = array();


 while($row = $query->fetch(PDO::FETCH_ASSOC))
{
  $rows['contact_name'][]= $row;
  $rows['contact_email'][]= $row;
  $rows['contact_password'][]= $row;
  $rows['contact_trip'][]=$row;
}

 header("Content-type: application/json; charset=utf-8");

 echo json_encode($rows);

C#(fragment):

    public class Customers : Android.Support.V4.App.Fragment
{
    List<people> mClient = new List<people>();
    ListView client;
    private WebClient webClient;
    private Uri mUrl;
    private BaseAdapter<people> mAdapter;

    public override void OnActivityCreated(Bundle savedInstanceState)
    {
        base.OnActivityCreated(savedInstanceState);
        client = View.FindViewById<ListView>(Resource.Id.list);

        webClient = new WebClient();
        mUrl = new Uri("http://*********.azurewebsites.net/admin.php");
        webClient.DownloadDataAsync(mUrl);
        webClient.DownloadDataCompleted += webClient_DownloadDataCompleted;

        MyListViewAdapter adapter = new MyListViewAdapter(this.Activity, mClient);
        client.Adapter = adapter;
    }

    private void webClient_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
    {
        Activity.RunOnUiThread(() =>
        {
            string json = Encoding.UTF8.GetString(e.Result);
            mClient = JsonConvert.DeserializeObject<List<people>>(json);
            mAdapter = new MyListViewAdapter(this.Activity, mClient);
            client.Adapter = mAdapter;
        });
    }

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {

        var view = inflater.Inflate(Resource.Layout.Frag1Layout, container, false);
        return view;
       }


    public override string ToString()
    {
        return "Customers";
    }
}

C#(adapter):

   using System;
   using Android.App;
   using Android.Content;
   using Android.Widget;
   using Android.Runtime;
   using System.Collections.Generic;
   using Android.Views;
   using Android.OS;

   namespace Tonio_Pick_Me_Up.Droid
 {
class MyListViewAdapter : BaseAdapter<people>
{
    public List<people> mItems;
    private Context mContext;

    public MyListViewAdapter(Context context, List<people> items)
    {
        mItems = items;
        mContext = context;
    }

    public override int Count
    {
        get { return mItems.Count; }
    }

    public override long GetItemId(int position)
    {
        return position;
    }

    public override people this[int position]
    {
        get { return mItems[position]; }
    }

    public override View GetView(int position, View convertView, ViewGroup parent)
    {
        View row = convertView;

        if (row == null)
        {
            row = LayoutInflater.From(mContext).Inflate(Resource.Layout.contactlist, null, false);
        }

        TextView ContactName = row.FindViewById<TextView>(Resource.Id.ContactName);
        ContactName.Text = mItems[position].Contact_Name;

        TextView ContactEmail = row.FindViewById<TextView>(Resource.Id.ContactEmail);
        ContactName.Text = mItems[position].Contact_Email;

        TextView ContactNextTrip = row.FindViewById<TextView>(Resource.Id.ContactNextTrip);
        ContactName.Text = mItems[position].Contact_Email;

        return row;
    }
      }
   }


   C#(people class):

using System;
using Newtonsoft.Json;

namespace Tonio_Pick_Me_Up.Droid
{

public class people
{
    [JsonProperty("Contact_Name")]
    public string Contact_Name { get; set;}

    [JsonProperty("Contact_Email")]
    public string Contact_Email { get; set; }

    [JsonProperty("Contact_Trip")]
    public string Contact_Trip { get; set; }

}
}

Hope you can help, thanks in advance.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 如何在scanpy上做差异基因和通路富集?
    • ¥20 关于#硬件工程#的问题,请各位专家解答!
    • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
    • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
    • ¥30 截图中的mathematics程序转换成matlab
    • ¥15 动力学代码报错,维度不匹配
    • ¥15 Power query添加列问题
    • ¥50 Kubernetes&Fission&Eleasticsearch
    • ¥15 報錯:Person is not mapped,如何解決?
    • ¥15 c++头文件不能识别CDialog