我想添加刻度线或十字线,具体取决于对象属性的状态。这是我在MainActivity中的代码,该代码使用意图传递了列表:
Button callHistoryButton = FindViewById<Button>(Resource.Id.ScanHistoryButton);
callHistoryButton.Click += (sender, e) =>
{
var intent = new Intent(this, typeof(ScanHistoryActivity));
intent.PutStringArrayListExtra("Codes", codes);
StartActivity(intent);
};
这是我在ListActivity中的代码:
public class ScanHistoryActivity : ListActivity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
var codes = Intent.Extras.GetStringArrayList("Codes");
codes.ToList();
ListAdapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItemChecked, codes);
}
}
这是我创建的对象:
[DataContract]
public class Scan
{
[DataMember]
public string ScanValue { get; set; }
[DataMember]
public string Action { get; set; }
public bool Success { get; set; }
}
因此,如果对象成功属性为true,则我想在ScanhistoryActivity中显示一个绿色的勾号,如果为false,则显示一个红色的叉号。我不希望人们能够手动选择或取消选择。
列表活动是否适合此活动?
您必须创建自己的自定义适配器才能起作用。
另外,您还必须创建一个布局,以便使用适配器在列表中填充数据(例如,每行的布局)。
例如:
AdapterRow.axml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/checkBox1"
android:focusable="false"
android:focusableInTouchMode="false" />
<ImageView xmlns:tools="http://schemas.android.com/tools"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:background="#ff997eff"
android:layout_marginLeft="10dp"
android:id="@+id/BusinessImageView" />
Adapter.cs
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Net;
using System.Threading.Tasks;
using System.Linq;
using Android.App;
using Android.Graphics;
using Android.Views;
using Android.Widget;
namespace testProject
{
public class SomeAdapter : BaseAdapter<Staff>
{
private readonly List<Staff> _list;
private readonly Activity _context;
public int position;
public SomeAdapter (Activity context, List<Staff> list)
{
_context = context;
_list = list;
}
public List<Staff> GetList()
{
return _list;
}
public override long GetItemId(int position)
{
return position;
}
public override int Count
{
get { return _list.Count; }
}
public override Staff this[int position]
{
get { return _list[position]; }
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
var view = convertView;
if (view == null) {
view = _context.LayoutInflater.Inflate(Resource.Layout.adapterRow, null);
}
ImageView img1 = view.FindViewById<ImageView> (Resource.Id.BusinessImageView);
CheckBox chk = view.FindViewById<CheckBox> (Resource.Id.checkBox1);
//Do aything you want with the data here using _list[position]
return view;
}
}
}
这是如何使用适配器填充listview的方法
_ListView = FindViewById<ListView>(Resource.Id.ListView);
_ListView .ItemClick += _ListView_OnItemClick;
_staffList = new List<Staff>();
_staffList = //Fill it with data
var adapter = new SomeAdapter (this, _staffList);
_ListView.Adapter = adapter;
adapter.NotifyDataSetChanged();
并选中或取消选中勾号:
private void _ListView_OnItemClick(object sender, AdapterView.ItemClickEventArgs e)
{
{
CheckBox chk = e.View.FindViewById<CheckBox>(Resource.Id.checkBox1);
if (chk.Checked == true)
{
chk.Checked = false;
}
else
{
chk.Checked = true;
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句