C# mono ListView实例带按钮事件

在网上找到个JAVA的实例看起来不错 就是没有按钮事件,

现在把他改成VS mono 平台开发,如下,

Layout文件夹下 city_item.axml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="0dip"
    android:paddingLeft="0dip"
    android:background="#9ACD32">
    <ImageView
        android:paddingTop="0dip"
        android:layout_alignParentRight="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/weather_image" />
    <Button
        android:layout_alignParentRight="true"
        android:layout_width="wrap_content"
        android:layout_height="35dp"
        android:layout_marginRight="0px"
        android:layout_marginBottom="0px"
        android:id="@+id/btn_click" />
    <TextView
        android:id="@+id/city"
        android:layout_width="fill_parent"
        android:paddingTop="0dip"
        android:layout_height="35dp"
        android:textColor="#ffffff" />
</RelativeLayout>
Layout文件夹下 content_item.axml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout02"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="0dip"
    android:paddingLeft="0dip"
    android:paddingRight="0dip">
<!--android:background="@drawable/listview_bg"-->
    <ImageView
        android:paddingTop="0dip"
        android:layout_alignParentRight="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/weather_image" />
    <TextView
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:id="@+id/content"
        android:paddingTop="0dip" />
    <TextView
        android:text=""
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_below="@+id/content"
        android:id="@+id/date"
        android:paddingRight="0dip" />
</RelativeLayout>

Layout文件夹下 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="fill_parent"
    android:layout_height="fill_parent">
    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:dividerHeight="1dip"
        android:id="@+id/list_items" />
<!--android:divider="@color/white"-->
</LinearLayout>
Layout文件夹下 layout1.axml

<?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"
    android:minWidth="25px"
    android:minHeight="25px">
   
</LinearLayout>

类文件 Activity1.cs

using System;

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

namespace AndroidApplication6
{
    [Activity(Label = "AndroidApplication6", MainLauncher = true, Icon = "@drawable/icon")]
    public class Activity1 : Activity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            this.SetContentView(Resource.Layout.Main);
           // SetContentView(Resource.Layout.Main);
            ListView list = FindViewById<ListView>(Resource.Id.list_items);

            //ArrayList<java.util.HashMap<string, string>> listItem = new ArrayList<HashMap<string, string>>();
            List<Dictionary<string, string>> listItem = new List<Dictionary<string, string>>();
            //各个城市的天气
            String[] data ={"北京","今天,晴天,22℃,2011-6-28","明天,多云转阵雨,32~23℃,2011-6-29","后天,阴转多云,33~23℃,2011-6-30",
"上海","今天,阵雨转雷阵雨,31~25℃,2011-6-28","明天,雷阵雨转阴,31~26℃,2011-6-29","后天,阵雨转多云,32~27℃,2011-6-30",
"广州","今天,中雨转暴雨,32~25℃,2011-6-28","明天,暴雨转大到暴雨,29~25℃,2011-6-29","后天,暴雨转阵雨,29~25℃,2011-6-30",
"深圳","今天,中雨转暴雨,31~25℃,2011-6-28","明天,暴雨,29~24℃,2011-6-29","后天,大雨转阵雨,28~25℃,2011-6-30"};
            //可以是城市的类型判断或者第几天判断,根据不同的需求构造不同的数组结构适应不同的应用
            int[] type = { 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3 };
            int size = data.Length;
            for (int i = 0; i < size; i++)
            {
                Dictionary<string, string> map = new Dictionary<string, string>();
                //HashMap<string, string> map = new HashMap<string, string>();
                //根据不同需求可以构造更复杂的数据,目前之构造一个数据
                map.Add("data", data[i]);
                // map.put("data", data[i]);
                listItem.Add(map);
            }
            WeatherAdapter listItemAdapter = new WeatherAdapter(this, listItem, type);
            list.Adapter = listItemAdapter;           
        }
        void btn_Click(object sender, EventArgs e)
        {
            AlertDialog.Builder dlg = new AlertDialog.Builder(this);
            dlg.SetTitle("提示");
            dlg.SetMessage(string.Format(((Button)sender).Tag.ToString()));
            dlg.SetPositiveButton("确定", delegate { });
            dlg.Show();
        }
        

    }
  
}

WeatherAdapter.cs

using System;
using System.Collections.Generic;
using System.Text;
using Android.App;
using Android.Content;
using Android.Views;
using Android.Widget;

namespace AndroidApplication6
{
    public class WeatherAdapter : BaseAdapter
    {

//数据源
private List<Dictionary<string,string>> list;
private Context context;
private int []type;

//构造函数
public WeatherAdapter (Context context,List<Dictionary<string,string>> list,
 int[] type){
this.context = context;
this.list = list;
this.type=type;
}

public override int Count {
// TODO Auto-generated method stub
get{return list.Count;}
}

//public List<Section> Sections
//{
//    get;
//    set;
//}


public override Java.Lang.Object GetItem(int position)
{
    return null; // list[position];
}




public override long GetItemId(int position) {
// TODO Auto-generated method stub
return position;
}

public override View GetView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater mInflater = LayoutInflater.From(context);
//产生一个View
View view = null;
//根据type不同的数据类型构造不同的View,也可以根据1,2,3天数构造不同的样式
if(type[position]==0){
//view = mInflater.inflate(R.layout.city_item, null);
    view = mInflater.Inflate(Resource.Layout.city_item, null);
//获取城市名称
string cityName=list[position]["data"];

ImageView image = view.FindViewById<ImageView>(Resource.Id.weather_image);
Button viewBtn = view.FindViewById<Button>(Resource.Id.btn_click);
viewBtn.Text ="详细";
viewBtn.Tag = viewBtn.Id.ToString() + "/" + position.ToString();
view.SetTag(viewBtn.Id, position.ToString()); 
viewBtn.Click += delegate {
    Intent intent = new Intent(context,typeof(layout1));
    Android.OS.Bundle bundle = new Android.OS.Bundle();
    intent.PutExtra("LayoutName", position.ToString() + cityName);
    intent.PutExtras(bundle);
    ((Activity1)context).StartActivityForResult(intent,10);
   // ((Activity1)context).SetContentView(Resource.Layout.layout1);
   // SetContentView(Resource.Layout.Main);
};


if(cityName.Contains("北京")){

//image.SetImageResource(R.drawable.beijing);
}
else if (cityName.Contains("上海"))
{
   // image.SetImageResource(R.drawable.shanghai);

}
else if (cityName.Contains("广州"))
{
   // image.SetImageResource(R.drawable.guangzhou);

}
else if (cityName.Contains("深圳"))
{
   // image.SetImageResource(R.drawable.shenzhen);

}
TextView city = view.FindViewById<TextView>(Resource.Id.city);
//city.setText(cityName);
city.Text = cityName;
}else{
    view = mInflater.Inflate(Resource.Layout.content_item, null);
//获取数据
string content=list[position]["data"];
//分离数据
string []items=content.Split(',');

TextView weather = view.FindViewById<TextView>(Resource.Id.content);
weather.Text=items[0]+"天气: "+items[1]+";温度:  "+items[2];
TextView date = view.FindViewById<TextView>(Resource.Id.date);
date.Text=items[3];

}

return view;
}

}

}

layout1.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;

namespace AndroidApplication6
{
    [Activity(Label = "My Activity")]
    public class layout1 : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.layout1);
          Title=Intent.GetStringExtra("LayoutName");
            // Create your application here
        }
    }
}

只为实现,不为别的 ,运行图如下,




点击详细后转入一个V


源码:http://download.csdn.net/detail/hcf_force/7170377



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值