xamarin.android如何调用sqlserver 数据库呢(或者其他的),很多新手都会有这个疑问。xamarin.android调用远程数据主要有两种方式:

  1. 在Android中保存数据或调用数据库可以利用SQLite,android中提供了几个类来管理SQLite数据库,对数据进行增删改查
  2. 直接调用Asp.net Web API对数据进行增删改查

这两种方式到底选择哪一种方式好一点呢?哪一种方式好不好我不敢确定,市场上大部分app都是调用api来clud的。当然我也推荐大家使用web api来调用远程数据,至少目前来看我们公司都是使用web api来做的。好吧废话不多说,下面就是ListView来调用web api执行增删改查的例子。

在listview上展示的数据是直接调用远程的web api中的数据。主要实现步骤

  1. 新建一个web api的项目,写好要用到的方法。
  2. 在MainActivity.cs中发送请求并将相应的json字符串序列化成List集合。

一、web api

由于我主要介绍下如何在xamarin中使用listview,所以web api我这里不做介绍了,相信聪明的你已经知道如何建立web api并写好方法了。

二、xamarin.android使用listview控件

1.新建xamarin.android项目,这里示例取名为listviewdemo

2.建好的项目如下图,这是可以看到运行按键显示了一个“实时播放器”,为何没有显示出模拟器来呢?由于我安装的是海马玩模拟器,请注意,请先运行模拟器,再打开VS项目,这里没有显示出我的模拟器,这是因为当前项目版本高于模拟器版本,我们只需要更改下项目最低兼容版本就可以了,项目属性--android选项--最低安卓版本选择android4.0,这是看到运行按键显示出来模拟器名称了,后面我们可以通过该模拟器进行调试。

3.我们打开layout文件夹下的main.axml文件,在该页面添加一个按钮,用于打开listview页面,main.axml文件修改如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="25px"
android:minHeight="25px">
<Button
android:text="获取产品信息"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/GetProductInfo" />
</LinearLayout>

4.在layout文件夹新建2个视图文件,即添加新项,选择android布局,创建ProductAdapter和ProductList两个android布局视图文件。

然后开始编辑我们的视图,将下列代码复制并替换到ProductAdapter中:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="25px"
android:minHeight="25px">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="30.5dp"
android:id="@+id/linearLayout1">
<TextView
android:text="产品型号:"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/textView1" />
<TextView
android:text="Text"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/ProductModel" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="30.5dp"
android:id="@+id/linearLayout2">
<TextView
android:text="产品名称:"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/textView2" />
<TextView
android:text="Text"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/ProductName" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/linearLayout3">
<TextView
android:text="产品类型:"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/textView3" />
<TextView
android:text="Text"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/ProductType" />
</LinearLayout>
</LinearLayout>

将下列代码复制并替换到ProductList中:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="25px"
android:minHeight="25px">
<ListView
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/StList" />
</LinearLayout>

5.新建一个Product类,用来定义产品信息实体,该实体容器存放我们需要的数据,于是我们在Product中添加如下代码:

   public class Product
{
public string ProductModel { get; set; }//产品型号
public string ProductName { get; set; }//产品名称
public string ProductType { get; set; }//产品类型
}

6.新建一个ProductAdapter类,用于把我们的实体数据绑定到适配器,并且继承BaseAdapter类,添加以下代码:

public class ProductAdapter : BaseAdapter
{
private List<Product> data;
private Context context;
public override int Count
{
get
{
return data.Count;
}
} public ProductAdapter(List<Product> data, Context context)
{
this.data = data;
this.context = context;
} public override Java.Lang.Object GetItem(int position)
{
return null;
} public override long GetItemId(int position)
{
return position;
} public override View GetView(int position, View convertView, ViewGroup parent)
{
convertView = LayoutInflater.From(context).Inflate(Resource.Layout.ProductAdapter, parent, false);
TextView ProductModel = convertView.FindViewById<TextView>(Resource.Id.ProductModel);
TextView ProductName = convertView.FindViewById<TextView>(Resource.Id.ProductName);
TextView ProductType = convertView.FindViewById<TextView>(Resource.Id.ProductType);
ProductModel.Text = data[position].ProductModel;
ProductName.Text = data[position].ProductName;
ProductType.Text = data[position].ProductType;
return convertView; }
}

7.项目添加新建一个活动取名为ProductList,然后我们就可以绑定列表了,将下列代码添加到ProductList活动中,代码中有个url,表示你获取web api服务的IP地址,这里填你实际的web api地址,可以通过花生壳映射内网来操作。

namespace listviewdemo
{
[Activity(Label = "产品信息")]
public class ProductList : Activity
{
public List<Product> item;//定义一个列表
public ListView listview;//定义控件
public ProductAdapter adapter;//定义数据源
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.ProductList);//设置要显示的视图
listview = FindViewById<ListView>(Resource.Id.StList);//找到控件
try
{
string url = "http://192.168.1.126:4479/api/app/CompanyServer/GetProduct";
string content = GetRouteData(url); //接收到响应的json 字符串
List<Product> list = JsonConvert.DeserializeObject<List<Product>>(content); //已经获取到远程数据的List<News>和之前的本地data就是一样的了。
adapter = new ProductAdapter(list, this);
listview.Adapter = adapter;
}
catch (Exception ex)
{
var dlg = new AlertDialog.Builder(this).SetTitle("警告")
.SetMessage(ex.Message);
dlg.Show();
}
} public static string GetRouteData(string url)
{
//构建请求
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.ContentType = "text/json;chartset=UTF-8";
//request.UserAgent = "";
request.Method = "POST";
request.ContentLength = ;//如果调用的API无须传递参数,那么请加上这一句 //接收响应
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream stream = response.GetResponseStream();
StreamReader streamReader = new StreamReader(stream, Encoding.UTF8);
string retString = streamReader.ReadToEnd();
return retString;
}
}
}

由于使用了HttpWebRequest进行web api请求,请添加如下引用:

using System.Net;
using System.IO;

由于使用到了json转换,请在项目引用进行NuGet,进行添加安装Newtonsoft.Json包。然后进行引用。

using Newtonsoft.Json;

8.完成这种步骤,最后在我们的mainactivity活动中重写button的点击事件用来跳转到ProductList就ok了。

namespace listviewdemo
{
[Activity(Label = "listviewdemo", MainLauncher = true)]
public class MainActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState); // Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
Button btnProductInfo = FindViewById<Button>(Resource.Id.GetProductInfo);
btnProductInfo.Click += btnProductInfo_Click;
}
private void btnProductInfo_Click(object sender, EventArgs e)
{
Intent intent = new Intent();
intent.SetClass(this, typeof(ProductList));
StartActivity(intent);
}
}
}

9.运行效果图

以上说明了使用listview展示数据的方法,并通过模拟器进行运行,若进行发布生成APK时,手机却无法使用闪退,有几个地方是需要设置配置下的,请点击

xamarin.android 发布生成APK真机运行闪退问题

Xamarin.Android 调用Web Api(通过ListView展示远程获取的数据)的更多相关文章

  1. [置顶] Xamarin android 调用Web Api(ListView使用远程数据)

    xamarin android如何调用sqlserver 数据库呢(或者其他的),很多新手都会有这个疑问.xamarin android调用远程数据主要有两种方式: 在Android中保存数据或调用数 ...

  2. 【ASP.NET Web API教程】3.3 通过WPF应用程序调用Web API(C#)

    原文:[ASP.NET Web API教程]3.3 通过WPF应用程序调用Web API(C#) 注:本文是[ASP.NET Web API系列教程]的一部分,如果您是第一次看本博客文章,请先看前面的 ...

  3. ASP.NET MVC4中调用WEB API的四个方法

    http://tech.it168.com/a2012/0606/1357/000001357231_all.shtml [IT168技术]当今的软件开发中,设计软件的服务并将其通过网络对外发布,让各 ...

  4. 【ASP.NET Web API教程】3.2 通过.NET客户端调用Web API(C#)

    原文:[ASP.NET Web API教程]3.2 通过.NET客户端调用Web API(C#) 注:本文是[ASP.NET Web API系列教程]的一部分,如果您是第一次看本博客文章,请先看前面的 ...

  5. 通过.NET客户端调用Web API(C#)

    3.2 Calling a Web API From a .NET Client (C#) 3.2 通过.NET客户端调用Web API(C#) 本文引自:http://www.asp.net/web ...

  6. WebApi系列~通过HttpClient来调用Web Api接口

    回到目录 HttpClient是一个被封装好的类,主要用于Http的通讯,它在.net,java,oc中都有被实现,当然,我只会.net,所以,只讲.net中的HttpClient去调用Web Api ...

  7. Android调用Web服务

    现在大部分应用程序都把业务逻辑处理,数据调用等功能封装成了服务的形式,应用程序只需要调用这些web服务就好了,在这里就不赘述web服务的优点了.本文总结如何在android中调用Web服务,通过传递基 ...

  8. 通过HttpClient来调用Web Api接口

    回到目录 HttpClient是一个被封装好的类,主要用于Http的通讯,它在.net,java,oc中都有被实现,当然,我只会.net,所以,只讲.net中的HttpClient去调用Web Api ...

  9. Http下的各种操作类.WebApi系列~通过HttpClient来调用Web Api接口

    1.WebApi系列~通过HttpClient来调用Web Api接口 http://www.cnblogs.com/lori/p/4045413.html HttpClient使用详解(java版本 ...

随机推荐

  1. 【转】GPS静态观测网的设计指标

     GPS网的设计指标是指导GPS网设计量化因子,是评价GPS网设计优劣的数值标准.评价GPS网设计的优劣主要从以下三个因素考虑:1.质量(包括精度和可靠性):2.效率:3.费用. 一.GPS网设计的精 ...

  2. TCP之再谈解决服务器TIMEWAIT过多的问题

    原则 TIMEWAIT并不是多余的.在TCP协议被创造,经历了大量的实际场景实践之后,TIMEWAIT出现了,因为TCP主动关闭连接的一方需要TIMEWAIT状态,它是我们的朋友.这是<UNIX ...

  3. 转载-Oracle ORACLE的sign函数和DECODE函数

    原文地址:http://www.cnblogs.com/BetterWF/archive/2012/06/12/2545829.html 转载以备用 比较大小函数 sign 函数语法:sign(n) ...

  4. Python元编程

    简单定义"元编程是一种编写计算机程序的技术,这些程序可以将自己看做数据,因此你可以在运行时对它进行内审.生成和/或修改",本博参考<<Python高级编程>> ...

  5. JDBC(一)

    JDBC(Java DataBase Conectivity)Java数据库连接,是J2SE的一部分,由java.sql和javax.sql组成. package dbTest; import jav ...

  6. NOIP2016提高组初赛(C++语言)试题 个人的胡乱分析

    最近在做历年的初赛题,那我捡几道比较有代表性的题说一下好了 原题可以在这里看:https://wenku.baidu.com/view/10c0eb7ce53a580217fcfede.html?fr ...

  7. python爬虫(1)——urllib包

    人生苦短,我用python! 一.关于爬虫 鉴于我的windos环境使用命令行感觉非常不便,也懒得折腾虚拟机,于是我选择了一个折中的办法--Cmder.它的下载地址是:cmder.net Cmder是 ...

  8. shell实现centos7双网卡修改网卡名eth0,eth1,并设置网络

    #!/bin/bash interface1=`ls /sys/class/net|grep en|awk 'NR==1{print}'` interface2=`ls /sys/class/net| ...

  9. github上fork了别人的项目后,再同步更新别人的提交

    我从github网站和用Git命令两种方式说一下. github网站上操作 打开自己的仓库,进入code下面. 点击new pull request创建.  选择base fork 选择head fo ...

  10. zabbix客户端一键安装脚本(主动模式监控)

    #!/bin/bash basepath=$(cd `dirname $0`; pwd)SHELL_DIR="${basepath}/shell"PACKAGE_DIR=" ...